Hibernate的异常PropertyAccessException这是怎么回事?

wzrlover 2004-10-08 03:14:07
数据库表:T_users ,T_groups ,T_modules
对应的XML:Tuser.hbm.xml,Tgroup.hbm.xml,Tmodule.hbm.xml
关系:user<—>group 是一对一的关系,group<—>module是一对多的关系
即,一个用户从属于一个群组,一个群组下包含多个功能模块

Tuser.hbm.xml的部分配置:

<!-- Associations -->
<one-to-one
name="group"
class="com.wzr.spring.bean.Group"
cascade="none"
outer-join="auto"
constrained="false"
/>

Tgroup.hbm.xml的部分配置:

<!-- Associations -->
<set
name="modules"
table="t_modules"
lazy="false"
inverse="true"
cascade="all"
sort="unshorted"
order-by="module_order asc"
>
<key
column="group_id"
/>
<one-to-many
class="com.wzr.spring.bean.Module"/>
</set>

Tmodule.hbm.xml的部分配置:

<!-- Associations -->
<many-to-one
name="group"
class="com.wzr.spring.bean.Group"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="group_id"
not-null="true"
/>

使用Middlegen生成POJO,后在Junit中的测试代码如下:

public void testInsert()throws Exception{
Transaction tx=null;
String hql="from Group where group_id='G000000001'";
try{
tx = session.beginTransaction();
List groupList=session.find(hql);
Group group=(Group)groupList.get(0);
Module module=new Module();
module.setModuleId("M000000001");
module.setModuleName("测试模块1");
module.setModuleOrder(1);
module.setMemo("备注");
module.setGroup(group);//将模块关联群组(group_id)

group.getModules().add(module);//群组1添加模块1

session.save(group);

tx.commit();
}
catch(Exception he){
if (tx!=null) tx.rollback();
he.printStackTrace();
Assert.fail(he.getMessage());
}
finally{
session.close();
}
}

运行后出现如下错误信息:

net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.wzr.spring.bean.Group.setModules
at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyvalues(AbstractEntityPersister.java:213)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2174)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:240)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:950)
at net.sf.hibernate.loader.Loader.list(Loader.java:941)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:834)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1512)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1491)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1483)
at com.wzr.spring.test.HibernateTest.testInsert(HibernateTest.java:84)
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:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:397)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:281)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:173)
Caused by: net.sf.cglib.beans.BulkBeanException
at com.wzr.spring.bean.Group$$BulkBeanByCGLIB$$27b99fa1.setPropertyvalues(<generated>)
at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyvalues(AbstractEntityPersister.java:208)
... 25 more
Caused by: java.lang.ClassCastException
... 27 more


错误提示应该是由类型转换引起的,不知是哪个属性配置有问题?
错误信息中的:at com.wzr.spring.test.HibernateTest.testInsert(HibernateTest.java:84)
是在List groupList=session.find(hql);这句?

另,在数据库(如SQLSERVER2000)中建立表关系图,通过Middlegen就可以直接配置好它们的关联关系
,而不用手动去配置one-to-one 或one-to-many,但不知道是否可靠?
...全文
248 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
umbrella1984 2004-10-08
  • 打赏
  • 举报
回复
List groupList=session.find(hql);
session.flush();
Group group=(Group)groupList.get(0);

异常的困难问题

net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class hibernate.Hello_Bean


如果出现这行错误说明你的xml文件里设置的字段名name的值与Hello_Bean.java类里的getXXX或setXXX方法不一致。


更多请看
http://www.phpx.com/happy/thr83472.html
wzrlover 2004-10-08
  • 打赏
  • 举报
回复
感谢UP,继续UP
yangzuyu12 2004-10-08
  • 打赏
  • 举报
回复
up

67,516

社区成员

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

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