小编典典

JPA ManyToMany联接表具有所有属性,如PK

hibernate

我正在使用Hibernate
3.3.1,并正在对该示例表结构进行建模,但是在创建具有额外属性的联接表时遇到了麻烦。

OrderProduct表之间的多对多关系。联接表就是该Order Detail表。我遵循这里提到的方法。

现在我有了实体

@Entity
@Table(name = "Orders")
public class Order {
    @OneToMany(mappedBy="order")
    private List<OrderDetail> orderItems;
}

@Entity
@Table(name="PRODUCTS")
public class Product {
    @OneToMany(mappedBy="product")
    private List<OrderDetail> orderItems;
}

@Entity
@IdClass(OrderDetail.class)
@Table(name = "ORDER_DETAIL")
public class OrderDetail implements Serializable {
    @Id
    @Column(name="ORDER_ID")
    private Long orderId;
    @Id
    @Column(name="PRODUCT_ID")
    private Long productId;

    @Column(name = "PRICE")
    private double price;

    @Column(name = "LAST_UPDATED_TIME")
    private Date lastUpdatedTime;

    @ManyToOne
    @JoinColumn(name = "ORDER_ID")
    private Order order;

    @ManyToOne
    @JoinColumn(name = "PRODUCT_ID")
    private Product product;
}

public class OrderDetailId implements Serializable {
    private Long orderId;
    private Long productId;
}

我使用Apache Derby进行测试,但是在生成的表结构上遇到了麻烦。

CREATE TABLE ORDER_DETAIL (
        PRODUCT_ID BIGINT NOT NULL,
        ORDER_ID BIGINT NOT NULL,
        LAST_UPDATED_TIME TIMESTAMP NOT NULL,
        PRICE DOUBLE NOT NULL
    );

CREATE INDEX SQL120323142938020 ON ORDER_DETAIL (PRODUCT_ID ASC);

CREATE UNIQUE INDEX SQL120323142937810 ON ORDER_DETAIL (PRODUCT_ID ASC, ORDER_ID ASC, LAST_UPDATED_TIME ASC, PRICE ASC);

ALTER TABLE ORDER_DETAIL ADD CONSTRAINT SQL120323142937810 PRIMARY KEY (PRODUCT_ID, ORDER_ID, LAST_UPDATED_TIME, PRICE);

ALTER TABLE ORDER_DETAIL ADD CONSTRAINT FK4A94AA82CC6D989A FOREIGN KEY (PRODUCT_ID)
    REFERENCES PRODUCTS (PROD_ID);

看来它已经创建了我所有的列作为主键。为什么会这样呢?


阅读 335

收藏
2020-06-20

共1个答案

小编典典

您可以使用实体的类作为IdClass的参数。那是不对的。应该使用Id类别。另外,不需要连接实体中ID的单独字段。

在下面查找类似代码的内容。我不能保证它可以在这样的旧版本的Hibernate中工作,但是可以肯定地在任何一个版本中都不能工作。值得尝试。如果要使用JPA
2.0功能,则至少升级到3.5.X版本(甚至是更新的版本)不会有什么坏处。构造函数/等式等被删除以节省空间。

@Entity
@Table(name = "Orders")
public class Order {
    @Id Long id;
    @OneToMany(mappedBy="order")
    private List<OrderDetail> orderItems;
}

@Entity
@Table(name="PRODUCTS")
public class Product {
    @Id Long id;
    @OneToMany(mappedBy="product")
    private List<OrderDetail> orderItems;
}

@Entity
@IdClass(OrderDetailId.class)
@Table(name = "ORDER_DETAIL")
public class OrderDetail implements Serializable {
    @Id @ManyToOne @JoinColumn(name = "ORDER_ID")
    private Order order;

    @Id @ManyToOne @JoinColumn(name = "PRODUCT_ID")
    private Product product;

    @Column(name = "PRICE") private double price;
    //Maybe you also want to use @TemporalType here
    @Column(name = "LAST_UPDATED_TIME") private Date lastUpdatedTime;
}

public class OrderDetailId implements Serializable {
    private Long order;
    private Long product;
}

2017年8月15日更新在JPA 2.1及更高版本中,您无需为复合ID添加类,您可以像这样进行操作:

@Entity
@Table(name = "ORDER_DETAIL")
public class OrderDetail implements Serializable {
    @Id @ManyToOne @JoinColumn(name = "ORDER_ID")
    private Order order;

    @Id @ManyToOne @JoinColumn(name = "PRODUCT_ID")
    private Product product;

    @Column(name = "PRICE") private double price;
    //Maybe you also want to use @TemporalType here
    @Column(name = "LAST_UPDATED_TIME") private Date lastUpdatedTime;
}
2020-06-20