[英]JPA Mapping @EmbeddedId with ManyToOne relationship
所以我在互联网上搜索了我的问题的答案,但没有找到有帮助的东西,基本上需要在两个类之间建立多对一关系,其中一个类具有 EmbeddedId,我将留下代码这里和它给出的错误消息(我正在使用wildfly运行服务器)。
公共 class InventoryPK 实现可序列化 {
@ManyToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private Item itemId;
@ManyToOne
@JoinColumn(name="CD_EMPRESA")
private Company company;
}
@Entity @Table(name = "inventario", schema = "mxnextmob")
公共 class 库存扩展 BaseModel {
@EmbeddedId
private InventoryPK id;
@SequenceGenerator(schema = "mxnextmob", name = "inventory_sequence", sequenceName = "inventory_sequence", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "inventory_sequence")
private Integer inventory;
@Column
private BigDecimal quantity;
@Column
private BigDecimal weight;
}
公共 class 公司扩展 BaseModel {
@Id
@SequenceGenerator(schema = "mxnextmob", name = "company_sequence", sequenceName = "company_sequence", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "company_sequence")
private Integer code;
@Column
private String name;
@OneToMany(mappedBy = "company")
private List<UserSeller> userSeller;
@OneToMany(mappedBy = "id.company")
private List<Inventory> inventories;
}
错误如下:
service jboss.persistenceunit."mxnext-mobile.war#mxnextmobileDS": org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: br.com.maxiconsystems.mobile.model.Inventory.company in br.com.maxiconsystems.mobile .model.Company.inventory
map 有几种方法可以使您看起来像一张桌子,但我建议将库存更改为:
public class Inventory extends BaseModel {
@Id
@SequenceGenerator(schema = "mxnextmob", name = "inventory_sequence", sequenceName = "inventory_sequence", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "inventory_sequence")
private Integer inventory;
@Embedded
private InventoryPK alternateKey;
@Column
private BigDecimal quantity;
@Column
private BigDecimal weight;
}
这允许您使用库存 Integer 作为其主键; 这简化了您可能需要添加到 Inventory 的任何未来引用,因为在 JPA 中需要外键来引用其所有 ID 列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.