这是我的mysql db
user.java
@Entity
@Table(name="user")
public class user
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int uid;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy ="user")
private Set<order> orders = new HashSet<order>();
//
setter ,getter ,and other
}
order.java
@Entity
@Table(name="`order`")
public class order
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int oid;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="oid", referencedColumnName="oid" )
private Set<orderitem> orderitems = new HashSet<orderitem>();
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="uid")
private user user ;
/////
setter ,getter and other
}
orderitem.java
@Entity
@Table(name="orderitem")
public class orderitem
{
@EmbeddedId
private orderitem_PK PK; //composite key
@ManyToOne
@JoinColumn(name="oid", referencedColumnName="oid" ,insertable = false, updatable = false)
private order order;
@ManyToOne
@JoinColumn(name="pid", referencedColumnName="pid", insertable = false, updatable = false)
private product products ;
////
setter,getter and other
}
orderitem_PK.java
@Embeddable
public class orderitem_PK implements Serializable
{
private int oid;
private int pid;
public orderitem_PK() {}
public orderitem_PK(int oid, int pid) {
this.oid = oid;
this.pid = pid;
}
// equals, hashCode
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
return super.equals(obj);
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode();
}
和上一次product.java
@Entity
@Table(name="product")
public class product
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int pid;
@OneToMany(cascade = CascadeType.ALL )
@JoinColumn(name="pid", referencedColumnName="pid" )
private Set<orderitem> orderitems = new HashSet<orderitem>();
//////////
setter,getter and other
}
这是一个在线购物页面
我正在尝试通过此代码插入数据
order order = new order();
order.setUser(user);
order.setOrdertotal(product.getPrice());
Date dt = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowTime = df.format(dt);
java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime);
order.setOrderdate(buydate);
/// delete this part will work
orderitem orderitem = new orderitem();
orderitem.setOrder(order);
orderitem.setProducts(product);
orderitem.setQuantity(product.getQuantity());
orderitem.setPrice(product.getPrice());
////
session.save(order);
session.save(orderitem);
遇到一些错误。
org.hibernate.id.IdentifierGenerationException:为类生成的null id:class Model.orderitem
当我只保存用户和订购部分时代码正常工作。
添加save orderitem代码后,问题就出现了。
我认为复合键映射外键是错误的。
但我不知道如何解决它。
有人能发现我做错了吗?
感谢。
答案 0 :(得分:0)
您需要使用 @MapsId在您的EmbeddedId中映射oid和pid。 @MasId提供EmbeddedId主键或父实体的简单主键的映射。 在orderItem实体中进行以下更改。
@Entity
@Table(name="orderitem")
public class orderitem
{
@EmbeddedId
private orderitem_PK PK; //composite key
@ManyToOne
@MapsId("oid") // maps the order id attribute of embedded id
@JoinColumn(name="oid", referencedColumnName="oid" ,insertable = false, updatable = false)
private order order;
@ManyToOne
@MapsId("pid") // maps the product id attribute of embedded id
@JoinColumn(name="pid", referencedColumnName="pid", insertable = false, updatable = false)
private product products ;
////
setter,getter and other
}