JPA 映射 @EmbeddedId 与 ManyToOne 关系

JPA Mapping @EmbeddedId with ManyToOne relationship

所以我在互联网上搜索了我的问题的答案,但没有找到有用的东西,基本上需要在两个 类 之间建立 ManyToOne 关系,其中一个有 EmbeddedId,我将在这里留下代码和它给出的错误消息(我正在使用 wildfly 到 运行 服务器)。

public class InventoryPK implements Serializable {

    @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")

public class Inventory extends 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 {

    @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

有几种方法可以将您似乎拥有的东西映射为 table,但我建议将库存更改为:

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

这允许您使用库存整数作为其主键;这简化了您将来可能需要添加到 Inventory 的任何引用,因为 JPA 中需要外键来引用其所有 ID 列。