简体   繁体   English

JPA 映射@EmbeddedId 与多对一关系

[英]JPA Mapping @EmbeddedId with ManyToOne relationship

so i searched for the answers for my problem on the internet but didn't find something that helped, basically a need to have a ManyToOne Relationship between two classes, of which one of them has a EmbeddedId, i'm going to leave the code here and the error message that it gives (i'm using wildfly to run the server).所以我在互联网上搜索了我的问题的答案,但没有找到有帮助的东西,基本上需要在两个类之间建立多对一关系,其中一个类具有 EmbeddedId,我将留下代码这里和它给出的错误消息(我正在使用wildfly运行服务器)。

public class InventoryPK implements Serializable {公共 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") @Entity @Table(name = "inventario", schema = "mxnextmob")

public class Inventory extends BaseModel {公共 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;

} }

public class Company extends BaseModel {公共 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;

} }

and the error is as follows:错误如下:

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

There are a few ways to map what you seem to have as a table, but I'd recommend Inventory be changed to something like: 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;
}

This allows you to use the inventory Integer as its primary key;这允许您使用库存 Integer 作为其主键; this simplifies any future references you may need to add to Inventory, as foreign keys would be required in JPA to reference all its ID columns.这简化了您可能需要添加到 Inventory 的任何未来引用,因为在 JPA 中需要外键来引用其所有 ID 列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM