我想创建一个结构来使用 Hibernate 4.1 在 MYSQL 中管理产品。通常有两种产品:SimpleProduct(仅包含描述和价格)和 ComplexProduct(包含再次包含产品的额外列表)。
所以我创建了一个抽象类 Product 以及派生类 SimpleProduct 和 ComplexProduct。我认为 TABLE_PER_CLASS InheritanceType 会很方便。
产品.java
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Product {
private long id;
private String description;
private BigDecimal price;
private Product parent;
protected Product() {
}
protected Product(String description, BigDecimal price) {
this.description = description;
this.price = price;
}
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name="ID")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name="DESCRIPTION")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name="PRICE", precision=2, columnDefinition="DECIMAL(10,2)")
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
@Override
public String toString() {
return getDescription()+", "+getPrice()+"€";
}
}
简单产品.java
@Entity
public class SimpleProduct extends Product {
public SimpleProduct() {
super();
}
public SimpleProduct(String description, BigDecimal price) {
super(description,price);
}
}
复杂产品.java
@Entity
public class ComplexProduct extends Product {
private List<Product> containedProducts;
public ComplexProduct() {
}
public ComplexProduct(String description, BigDecimal price, List<Product> containedProducts) {
super(description,price);
if (containedProducts == null) {
this.containedProducts = new ArrayList<Product>();
} else {
this.containedProducts = containedProducts;
}
}
public ComplexProduct(String description, BigDecimal price) {
super(description,price);
this.containedProducts = new ArrayList<Product>();
}
@OneToMany(targetEntity=Product.class, cascade=CascadeType.ALL, fetch=FetchType.LAZY)
public List<Product> getContainedProducts() {
return containedProducts;
}
public void setContainedProducts(List<Product> containedProducts) {
this.containedProducts = containedProducts;
}
public void addToProductList(Product p) {
this.containedProducts.add(p);
}
}
然后我尝试执行一个Teststructure:
SimpleProduct sp1 = new SimpleProduct("SimpleProduct1",new BigDecimal("2.99"));
SimpleProduct sp2 = new SimpleProduct("SimpleProduct2",new BigDecimal("4.99"));
SimpleProduct sp3 = new SimpleProduct("SimpleProduct3",new BigDecimal("3.99"));
ComplexProduct cp1 = new ComplexProduct("ComplexProduct1",new BigDecimal("8.99"));
cp1.addToProductList(sp2);
cp1.addToProductList(sp3);
HibernateUtil.persist(sp1);
HibernateUtil.persist(sp2);
HibernateUtil.persist(sp3);
HibernateUtil.persist(cp1);
HibernateUtil.shutdown();
这是持久方法:
public static void persist(Object o) {
Session session = getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.persist(o);
tx.commit();
}
我知道,这个测试结构的持久方法不是很干净,但它只是为了测试。
问题是当程序执行HibernateUtil.persist(cp1)时出现异常:
线程“主”org.hibernate.PersistentObjectException 中的异常:传递给持久化的分离实体:产品
那么,它有什么问题呢?我该如何解决这个问题?
谢谢