4

这里是数据库模式

CREATE TABLE Products
(
    id          INT NOT NULL AUTO_INCREMENT,
    category_id  INT NOT NULL,
    description VARCHAR(100),
    price       DECIMAL(10, 2) NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (category_id) REFERENCES Categories(id)
) ENGINE = INNODB;

CREATE TABLE Orders
(
    id           INT NOT NULL AUTO_INCREMENT,
    customer_id  INT NOT NULL,
    status       VARCHAR(20) NOT NULL,
    date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    FOREIGN KEY (customer_id) REFERENCES Customers(id)
) ENGINE = INNODB;

CREATE TABLE OrderDetails
(
    product_id INT NOT NULL,
    order_id   INT NOT NULL,
    quantity   INT NOT NULL,
    subtotal   DECIMAL(10, 2) NOT NULL,
    PRIMARY KEY (product_id, order_id),
    FOREIGN KEY (product_id) REFERENCES Products(id),
    FOREIGN KEY (order_id)   REFERENCES Orders(id)
) ENGINE = INNODB;

模型

@Embeddable
public class OrderDetailPK
{
    private Product product;
    private Order order;

    public OrderDetailPK() {}

    public OrderDetailPK(Product product, Order order)
    {
        this.product = product;
        this.order   = order;
    }
}

public class OrderDetail {
    @EmbeddedId
    private OrderDetailPK id;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="product_id", insertable=false, updatable=false)
    private Product product;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="order_id", insertable=false, updatable=false)
    private Order order;

    private int quantity;
    private double subtotal;

    public OrderDetail() {}

    public OrderDetail(OrderDetailPK id, int quantity, double subtotal)
    {
        this.product  = id.getProduct();
        this.order    = id.getOrder();
        this.quantity = quantity;
        this.subtotal = subtotal;
    }
    // getters, setters
}

public class Product {
    @Id
    private int id;

    private String description;
    private double price;

    @ManyToOne
    @JoinColumn(name="category_id")
    private Category category;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "Products")
    private List<OrderDetail> orderDetail;
}

public class Order {
    @Id
    private int id;

    @ManyToOne
    @JoinColumn(name="customer_id")
    private Customer customer;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "Orders")
    private List<OrderDetail> orderDetail;
}

由于某些原因,我不断收到错误

Concrete type "class models.OrderDetail" with application identity does not declare any primary key fields.

谁能指出问题出在哪里?谢谢

4

3 回答 3

2

当我之前这样做时(如this question and answer中所述),我在可嵌入ID原语中制作了字段(对应于所引用实体的ID字段),然后@MapsId在实体中使用。我相信这是满足所有要求的最简单的(我敢说是正确的):实体中的字段是关系,ID 类中的字段是原始的,每一列都只映射一次(这些@MapsId字段不是真的是映射,但有点别名)。

将其应用于您的案例,ID 类如下所示:

@Embeddable
public class OrderDetailPK {
    private final int productId;
    private final int orderId;

    public OrderDetailPK(int productId, int orderId) {
        this.productId = productId;
        this.orderId = orderId;
    }
}

实体类看起来像:

public class OrderDetail {
    @EmbeddedId
    private OrderDetailPK id;

    @ManyToOne(cascade = CascadeType.ALL)
    @MapsId("productId")
    private Product product;

    @ManyToOne(cascade = CascadeType.ALL)
    @MapsId("orderId")
    private Order order;

    private int quantity;
    private double subtotal;

    public OrderDetail(Product product, Order order, int quantity, double subtotal) {
        this.id = new OrderDetailPK(product.getId(), order.getId());
        this.product = product;
        this.order = order;
        this.quantity = quantity;
        this.subtotal = subtotal;
    }

    protected OrderDetail() {}
}
于 2013-02-28T08:44:12.153 回答
0

首先OrderDetailPK得落实Serializable

其次,请指定您要使用的 ID,因为您已指定列product_idorder_idas insertable=false, updatable=false(只读)。

因此,您需要尝试以下方法:

@EmbeddedId
@AttributeOverrides({
        @AttributeOverride(name = "product_id",column = @Column(name = "product_id")),
        @AttributeOverride(name = "listingId",column= @Column(name = "order_id"))
})
private OrderDetailPK id;

您可以在这里找到更多信息:

http://docs.oracle.com/javaee/6/api/javax/persistence/EmbeddedId.html

http://docs.oracle.com/javaee/6/api/javax/persistence/AttributeOverride.html

于 2013-02-28T08:28:40.120 回答
0

EmbeddedIdjavadoc:

不支持在嵌入式 id 类中定义的关系映射。

所以你不能这样做。我不认为 JPA 1 指定了实现这一点的标准方法(在 JPA 2 中有@MapsId但我从未尝试过),但这是我通常做的,大多数实现(我认为至少 Hibernate、EclipseLink 和 OpenJPA)都支持它:

使用原始类型声明您的主键类:

@Embeddable
public class OrderDetailPK implements Serializable
{
    private int product;
    private int order;

    public OrderDetailPK() {}

    ...
}

@IdClass使用相同名称但所需类型来注释您的实体并声明字段:

@Entity
@IdClass(OrderDetailPK.class)
public class OrderDetail {
    @Id
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="product_id", insertable=false, updatable=false)
    private Product product;

    @Id
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="order_id", insertable=false, updatable=false)
    private Order order;

    ...
}

(我一直保留@Id实体中的字段,但我没有重新检查它们是否是强制性的)

于 2013-02-28T08:42:17.807 回答