[英]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.