浏览 4631 次
锁定老帖子 主题:有关一对多的关联
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2004-02-10
net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info); setter of its.hibernate.WorkFlow.setTcardbasicinfos at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:216); at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2137); at net.sf.hibernate.loader.Loader.doQuery(Loader.java:239); at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:132); at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:830); at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:850); at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:57); at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:49); at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:420); at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2044); at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1918); at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1880); at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:68); at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:215); at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2132); at net.sf.hibernate.loader.Loader.doQuery(Loader.java:239); at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:132); at net.sf.hibernate.loader.Loader.doList(Loader.java:949); at net.sf.hibernate.loader.Loader.list(Loader.java:940); at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:833); at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1475); at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39); at its.zp.main(zp.java:57); Caused by: net.sf.cglib.beans.BulkBeanException at its.hibernate.WorkFlow$$BulkBeanByCGLIB$$7a8e29ee.setPropertyValues(<generated>); at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:211); ... 22 more Caused by: java.lang.ClassCastException ... 24 more Exception in thread "main" 测试类用的代码如下 SessionFactory sf = new Configuration();.configure();.buildSessionFactory();; Session session = sf.openSession();; Transaction tx = session.beginTransaction();; Session ss = ITSDB.getSession();; String queryString = "from Drawing as Drawing"; Query query = session.createQuery(queryString);; List l = query.list();; System.out.println(((Drawing);l.get(0););.getDrawingid(););; DRAWING类是一个Hibernate的类测试过XML也通过的,想请问一下,我的错误在哪 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2004-02-10
类文件的内容
package its.hibernate; import java.io.Serializable; import java.lang.*; public class Drawing implements Serializable{ private int drawingid; private String drawingname; private String drawingtype; private String drawingdesc; private String drawingaddr; private int tworkflow; private int tcardbasicid; public Drawing() { } public Drawing(String drawingname,String drawingtype,int tworkflow,int tcardbasicid) { this.drawingname=drawingname; this.drawingtype=drawingtype; this.tworkflow =tworkflow; this.tcardbasicid =tcardbasicid; } public Drawing(String drawingname,String drawingtype,String drawingdesc,String drawingaddr,int tworkflow,int tcardbasicid) { this.drawingname=drawingname; this.drawingtype =drawingtype; this.drawingdesc =drawingdesc; this.drawingaddr=drawingaddr; this.tworkflow=tworkflow; this.tcardbasicid =tcardbasicid; } public int getDrawingid() { return drawingid; } public void setDrawingid(int drawingid) { this.drawingid = drawingid; } public String getDrawingname() { return drawingname; } public void setDrawingname(String drawingname) { this.drawingname = drawingname; } public String getDrawingtype() { return drawingtype; } public void setDrawingtype(String drawingtype) { this.drawingtype = drawingtype; } public String getDrawingdesc() { return drawingdesc; } public void setDrawingdesc(String drawingdesc) { this.drawingdesc = drawingdesc; } public String getDrawingaddr() { return drawingaddr; } public void setDrawingaddr(String drawingaddr) { this.drawingaddr = drawingaddr; } public int getTworkflow() { return tworkflow; } public void setTworkflow(int tworkflow) { this.tworkflow = tworkflow; } public int getTcardbasicid() { return tcardbasicid; } public void setTcardbasicid(int tcardbasicid) { this.tcardbasicid = tcardbasicid; } } |
|
返回顶楼 | |
发表时间:2004-02-10
ymm100 写道 我的错误是
net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info); setter of its.hibernate.WorkFlow.setTcardbasicinfos at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:216); 错误可能出现在Workflow中的setTcardbasicinfos(),检查以一下。贴出AbstractEntityPersister.java |
|
返回顶楼 | |
发表时间:2004-02-11
昨天我用QQ问过yehs220,他说应当是这样定义
/** * @hibernate.many-to-one * column="MATE_ID" */ public Cat getMate() { return mate; } void setMate(Cat mate) { this.mate = mate; } 说实话文档看过,我还是有一点没明白,我二个表一个主键一个外键都是INT而且一定都是非空的,这样也要定义另一个类来保存他们吗? 请大家告我,我现在越来越不明白(我没看明白文档) |
|
返回顶楼 | |
发表时间:2004-02-11
...............人气好少.是我的问题太容易你们都不高兴回答吗?
|
|
返回顶楼 | |
发表时间:2004-02-11
你的做法虽然没问题,但建议不要用传统的关系数据库思想来思考面向对象的问题.
不是要定义类来保存主键和外键。假设有parent表和child表. parent表是child外键表,child表中的外键为parentid. 传统是你的写法。但面向对象的做法如yehs220的例子.在这里是: child.java /** * @hibernate.many-to-one * column="parentid" */ public Parent getParent() { return parent; } void setMate(Parent parent) { this.parent = parent; } 传统做法是: public int getParentId() { return parentid; } void setParentId(int parentid) { this.parentid = parentid; } 如果这样,在hbm.xml文件中就不需要影射many-to-one的关系了. |
|
返回顶楼 | |
发表时间:2004-02-12
我想问一下,你的意思是说如果我用INT就不用在XML里写many-to-one的关系了
今天我同事有一种方法,一对多的用Set ,一对一和多对一的,用对象,也不报错了,可以请教一下这是怎么回事吗?是你说的新方法吗? |
|
返回顶楼 | |
发表时间:2004-02-12
用你同事的做法是正确的做法。我的意思是如果你不想按照你同事的做法,在映射文件中将将所有非主键都影射成普通属性,只能在程序中实现表和表之间关联了。那么实际表之间也必须去掉关联。也就是说,如果数据库表之间必须建立外键关联的话,用你同事的方法。
|
|
返回顶楼 | |
发表时间:2004-02-13
3Q,明白啦
我用工具生成出来类看了一下,嘻嘻 |
|
返回顶楼 | |