0

所以我在互联网上搜索了我的问题的答案,但没有找到有帮助的东西,基本上需要在两个类之间有一个 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 引用一个未知的目标实体属性:br.com.maxiconsystems.mobile.model.Inventory.company in br.com.maxiconsystems.mobile .model.Company.inventory

4

1 回答 1

0

有几种方法可以将您似乎拥有的内容映射为表格,但我建议将 Inventory 更改为以下内容:

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

于 2022-02-04T15:00:58.100 回答