gpt4 book ai didi

java - JPA : reference to a generated JoinTable

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:29 27 4
gpt4 key购买 nike

我有两个实体:产品和过道。一种产品可以位于一个或多个过道中,并且一个过道可以有一个或多个产品。

@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”注释将销售员引用到自动创建的联接表 (ProductAisle)?

问候

最佳答案

要表示位于特定 channel 中的产品,您需要另一个实体。这是一个例子:

@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;

}

关于java - JPA : reference to a generated JoinTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52030272/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com