sinat_36363615 2017-03-28 03:31 采纳率: 0%
浏览 840

hibernate4.2.21 延迟加载异常 了解的麻烦看一下

是哪里出问题了?
实体类 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)

请问为什么异常?谢谢了

  • 写回答

2条回答 默认 最新

  • It_is_easy 2017-03-28 04:50
    关注

    你实体类里面有没有重写toString方法?延迟加载的时候生成的是代理对象,不是真正的java对象...

    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致