使用复合键(具有IdClass)

时间:2016-07-06 19:02:32

标签: java hibernate jpa ejb

OrderItem有两个键orderid和itemid。我正在传递一个OrderItem对象。但它抛出错误:字段'orderItem_orderid'没有默认值。 以下是代码的一部分:

@Entity
@Table(name="OrderItem")
@IdClass(value=OrderItemPK.class)
public class OrderItem {

    @Id
    private String orderid;
    @Id
    private String itemid;

    @Column
    private String material; 

     @Column
    private String quantity; 

    public String getOrderid() {
        return orderid;
    }

    public void setOrderid(String orderid) {
        this.orderid = orderid;
    }


    public void getItemid() {
        return itemid;
    }

    public void setItemid(String orderid) {
        this.itemid = itemid;
    }

    public void getMaterial() {
        return material;
    }

    public void setMaterial(String material) {
        this.material = material;
    }


    public void getQuantity() {
        return quantity;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }



}

DAO:

public class InfoManager<T> implements InfoManagerRemote<T> {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public T create(T t) throws Exception {
            entityManager.persist(t);
            return t;
    }
}

呼叫:

OrderItem orderItem = new OrderItem();
orderItem.setOrderid("ORD000000001");
orderItem.setItemid("1");
orderItem.setMaterial("Mat1");
orderItem.setQuantity("2");
infoManagerRemote.create(orderItem);

我在创建Order时工作正常。不适用于orderItem。

请帮助解决这个问题。我被困在这3天了。

0 个答案:

没有答案