JPA:对生成的 JoinTable 的引用

JPA : reference to a generated JoinTable

我有两个实体:Product 和 Aisle。 一个产品可以在一个或多个通道中,一个通道可以有一个或多个产品。

@Entity 
public class Product{
   @Id
   private Long id;
   private String name;
   @ManyToMany
   @JoinTable(name = "product_aisle",
            joinColumns = { @JoinColumn(name = "product_id") },
            inverseJoinColumns = { @JoinColumn(name = "aisle_id") })
    private Set<Aisle> aisles = new HashSet<>();
   /* getters, setters, equals and hashcode */
}


@Entity 
public class Aisle{
   @Id
   private Long id;
   private String row;
   private String shelf;
   @ManyToMany(mappedBy="aisles")
   private Set<Product> products = new HashSet<>();
   /* getters, setters, equals and hashcode */
}

我还有最后一个实体:推销员。 销售员负责过道中的产品:

@Entity 
public class Salesman{
   @Id
   private Long id;

   private String name;

   /* ManyToOne to  ProductAisle ?*/

}

问题:如何使用“@ManyToOne”注释将销售员引用到自动创建的连接 table (ProductAisle)?

此致

由于 AisleProduct 相互之间具有双向映射,您可以将它们中的任何一个(甚至两个)连接到 Salesman class,你根本不需要加入服务table。

要表示特定 Aisle 中的 Product,您需要另一个实体。这是一个例子:

@Entity 
public class Product{
   @Id
   private Long id;
   private String name;
   @OneToMany(mappedBy = "product")
   private Set<ProductAisle> productAisle = new HashSet<>;
   /* getters, setters, equals and hashcode */
}


@Entity 
public class Aisle{
   @Id
   private Long id;
   private String row;
   private String shelf;
   @OneToMany(mappedBy = "aisle")
   private Set<ProductAisle> productAisle = new HashSet<>();
   /* getters, setters, equals and hashcode */
}

@Entity 
public class ProductAisle{
   @Id
   private Long id;
   @ManyToOne(fetch = FetchType.LAZY)
   private Product product;
   @ManyToOne(fetch = FetchType.LAZY)
   private Aisle aisle;
   /* getters, setters, equals and hashcode */
}

然后您的 Salesman 将指向 ProductAisle 个实例的集合,该实例将产品映射到过道:

@Entity 
public class Salesman{
   @Id
   private Long id;

   private String name;

   @ManyToOne(fetch = FetchType.LAZY)
   private Set<ProductAisle> productAisle;

}