是哪里出问题了?
实体类 Customer
package cn.hiber.entity.m2o;
public class Customer {
private Integer customerId;
private String customerName;
省略get()、set()方法......
}
实体类 Order
package cn.hiber.entity.m2o;
public class Order {
private Integer orderId;
private String orderName;
private Customer customer;
省略get()、set()方法......
}
映射文件 Customer.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<property name="customerName" column="CUSTOMER_NAME"></property>
</class>
映射文件 Order.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<property name="orderName" column="ORDER_NAME"></property>
<!-- 单向多对一 -->
<many-to-one name="customer" class="Customer" column="CUSTOMER_ID"></many-to-one>
</class>
核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- 数据库配置信息(必须) -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hiber</property>
<property name="connection.username">zhou</property>
<property name="connection.password">123456</property>
<!-- hibernate配置信息(可选) -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property> <!-- 生成表的策略,无则创建,有则更新 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 方言 -->
<!-- 设置hibernate的事务隔离级别 -->
<!-- <property name="connection.isolation">2</property> -->
<!-- 删除对象后,使其OID置为null -->
<property name="use_identifier_rollback">true</property>
<!-- 配置c3p0数据源 -->
<property name="c3p0.max_size">10</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.acquire_increment">2</property> <!-- 连接对象被消耗完时,一次获取多少个数据库连接 -->
<property name="c3p0.timeout">2000</property> <!-- 连接对象多长时间没被使用后,就该被销毁 -->
<property name="c3p0.idle_test_period">2000</property> <!-- 表示连接池检测线程多长时间检测一次连接池内连接对象是否超时 -->
<property name="c3p0.max_statements">10</property> <!-- 缓存statement 对象的数量 -->
<!-- 引入映射文件 -->
<mapping resource="cn/hiber/entity/m2o/Customer.hbm.xml"/>
<mapping resource="cn/hiber/entity/m2o/Order.hbm.xml"/>
</session-factory>
测试类
package cn.hiber.entity.m2o;
public class hiberDaoTest {
static SessionFactory sessionFactory = null;
static Session session = null;
static Transaction transaction = null;
@Before
public void init() {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void destory() {
transaction.commit();
session.close();
sessionFactory.close();
}
//使用get()方法或设置lazy=“false”时,无异常
@Test
public void testLoad() {
Customer customer = (Customer) session.load(Customer.class, 1);
};
}
请问为什么异常?谢谢了
异常
java.lang.ClassCastException: cn.hiber.entity.m2o.Customer_$$_javassist_0 cannot be cast to javassist.util.proxy.Proxy
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:148)
at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:73)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:758)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4419)
at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:334)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1053)
at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:173)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.getReference(SessionImpl.java:2426)
at org.hibernate.internal.SessionImpl.load(SessionImpl.java:941)
at cn.hiber.entity.m2o.hiberDaoTest.testLoad(hiberDaoTest.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
请问为什么异常?谢谢了