Hibernate级联删除问题

WHW1984 2007-12-02 03:16:21
Customer类
package com.customerandorder;
import java.io.Serializable;
public class Customer implements Serializable{
private Long id;
private String customerName;
private Set orders= new HashSet();
public Set getOrders() {
return orders;
}
public void setOrders(Set orders) {
this.orders = orders;
}
public Customer(Long id, String customerName, Set orders){
this.id = id;
this.customerName = customerName;
this.orders = orders;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}

}

order类
package com.customerandorder;
import java.io.Serializable;
public class Order implements Serializable{
private Long id;
private String orderName;
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}

}


Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.customerandorder.Customer" table="CUSTOMER">
<id name="id" type="long" column="COUSTOMER_ID">
<generator class="increment"></generator>
</id>
<property name="customerName" type="string">
<column name="COUSTOMER_NAME" length="32"></column>
</property>
<set name="orders" cascade="delete" inverse="true">
<key column="CUSTOMER_ID"></key>
<one-to-many class="com.customerandorder.Order"/>
</set>
<!-- save-update- -->
</class>
</hibernate-mapping>



Order.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.customerandorder.Order" table="ORDERS">
<id name="id" type="long" column="ID">
<generator class="increment"></generator>
</id>
<property name="orderName" type="string">
<column name="ORDER_NUMBER" length="32"></column>
</property>
<many-to-one name="customer"
column="CUSTOMER_ID"
class="com.customerandorder.Customer"

not-null="false">
<!-- cascade="delete" -->
</many-to-one>
</class>
</hibernate-mapping>


TestCustomerandOrder类:
package com.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.customerandorder.Customer;
import com.customerandorder.Order;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;

public class TestCustomerandOrder {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
long start = System.currentTimeMillis();
Customer customer=(Customer)session.load(Customer.class, new Long(13));
session.delete(customer);
transaction.commit();
long end=System.currentTimeMillis();
long total=end-start;
System.out.println("It execute time is: "+total);
session.close();
sessionFactory.close();


}

}
数据在数据库中是有的。
出现了错误:
org.hibernate.HibernateException: CGLIB Enhancement failed: com.customerandorder.Customer
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxy(CGLIBLazyInitializer.java:104)
at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.getProxy(CGLIBProxyFactory.java:49)
at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3232)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:237)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:174)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:871)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:788)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:781)
at com.test.TestCustomerandOrder.main(TestCustomerandOrder.java:37)
Caused by: java.lang.InstantiationException: com.customerandorder.Customer$$EnhancerByCGLIB$$b47a1da9
at java.lang.Class.newInstance0(Class.java:293)
at java.lang.Class.newInstance(Class.java:261)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxy(CGLIBLazyInitializer.java:101)
... 10 more
Exception in thread "main"

帮忙看下。顶者给分
...全文
137 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
nijiangli 2007-12-09
  • 打赏
  • 举报
回复
如果是当前类中自已不写pubic 构造函数是没有问题的,在实例化时会调和一个默认的构造函数,但这个默认的构造函数不是说是
public 类型的
WHW1984 2007-12-08
  • 打赏
  • 举报
回复
要在两个类中建立一个public的构造函数就可以了
public Customer(){}

public Order(){}

结果问题解决了。

而且是一对多的类写个构造函数就可以了。就是说 Order这个构造函数都无需写
DiligencyMan 2007-12-03
  • 打赏
  • 举报
回复
在Customer的hbm文件中将orders的lazy属性设置为"true" 试下看效果怎么样?
hxj1225 2007-12-02
  • 打赏
  • 举报
回复
呵呵,我也遇到了级联删除的问题。
帮忙顶一下。学习!
Leo1734 2007-12-02
  • 打赏
  • 举报
回复
试试在Customer的hbm文件中设置orders的lazy="true"
WHW1984 2007-12-02
  • 打赏
  • 举报
回复
我在两个类中加了个共有构造函数就搞顶了。
很奇怪

public Customer(){}
public Order(){}

有人知道原因吗?

网上也看了。是说不能有一个private的无参构造函数
但是一个类不是有一个自带public的无参构造函数吗??

求教中

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧