我想创建一个使用Hibernate 4.1管理MYSQL产品的结构。通常有两种产品:SimpleProduct(仅包含描述和价格)和ComplexProduct(包含一个包含产品的额外列表)。
所以我创建了一个抽象类Product和派生类SimpleProduct和ComplexProduct。我认为TABLE_PER_CLASS InheritanceType很方便。
Product.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()+"€";
}
}
SimpleProduct.java
@Entity
public class SimpleProduct extends Product {
public SimpleProduct() {
super();
}
public SimpleProduct(String description, BigDecimal price) {
super(description,price);
}
}
ComplexProduct.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):
线程“main”中的异常org.hibernate.PersistentObjectException:传递给persist的分离实体:Product
那么,它有什么问题?我该如何解决这个问题?
谢谢
答案 0 :(得分:0)
我找到了一个解决方案:如果我使用session.save(o)而不是session.persist(o)它可以工作!