浏览 4917 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2003-12-17
配置文件:hibernate.connection.driver_class = oracle.jdbc.driver.OracleDriver hibernate.connection.url = jdbc:oracle:thin:@saturn.dragontec:1521:qjfdb hibernate.connection.username = system hibernate.connection.password = manager hibernate.dialect = net.sf.hibernate.dialect.OracleDialect hibernate.show_sql true 对象关系: B_husband , B_wife,B_ child, husband和wife是一对一的关系, husband 和 child 是一对多的关系. public class B_husband { private int id; private String name; private List childs; private B_wife wife; public B_husband() { } public void setId(int t){ id = t; } public int getId(){ return id; } public void setName(String t){ name = t; } public String getName(){ return name; } public void setChilds(List t){ childs = t; } public List getChilds(){ return childs; } public void setWife(B_wife t){ wife = t; } public B_wife getWife(){ return wife; } } /******************************************************/ public class B_wife { private int id; private String name; public B_wife() { } public void setId(int t) { id = t; } public int getId() { return id; } public void setName(String t) { name = t; } public String getName() { return name; } } /*********************************************************/ public class B_child { private int id; private String name; private char sex; private B_husband b_husband; public B_child() { } public void setId(int t){ id = t; } public int getId(){ return id; } public void setName(String t){ name = t; } public String getName(){ return name; } public void setSex(char t){ sex = t; } public char getSex(){ return sex; } public void setB_husband(B_husband t){ b_husband = t; } public B_husband getB_husband(){ return b_husband; } } /****************************************************/ 以下是处理并出错的class: public class Test { private SessionFactory _sessions; public Test() { } public static void main(String[] args) { Test test1 = new Test(); try{ test1.configure(); test1.exportTables(); /******************************************************************/ B_wife wife = test1.createWife("wife"); B_husband husband = test1.createHusband("husband", wife); test1.createChild("child", 'm', husband); } catch(Exception e){ e.printStackTrace(); } } public void configure() throws HibernateException { _sessions = new Configuration() .addClass(B_husband.class) .addClass(B_child.class) .addClass(B_wife.class) .buildSessionFactory(); } public void exportTables() throws HibernateException { Configuration cfg = new Configuration() .addClass(B_husband.class) .addClass(B_child.class) .addClass(B_wife.class); new SchemaExport(cfg).create(true, true); } public B_husband createHusband(String name, B_wife wife) throws HibernateException{ B_husband husband = new B_husband(); husband.setName(name); husband.setChilds( new ArrayList() ); husband.setWife( wife ); Session session = _sessions.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(husband); tx.commit(); } catch (HibernateException he) { if (tx!=null) tx.rollback(); throw he; } finally { session.close(); } return husband; } public B_child createChild(String name, char t, B_husband husband) throws HibernateException{ B_child child = new B_child(); child.setName( name ); child.setSex( t ); child.setB_husband( husband ); husband.getChilds().add( child ); Session session = _sessions.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(child); tx.commit(); } catch (HibernateException he) { if (tx!=null) tx.rollback(); throw he; } finally { session.close(); } return child; } public B_wife createWife(String name) throws HibernateException{ B_wife wife = new B_wife(); wife.setName( name ); Session session = _sessions.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(wife); tx.commit(); } catch (HibernateException he) { if (tx!=null) tx.rollback(); throw he; } finally { session.close(); } return wife; } } 出错信息如下: java.lang.ClassCastException at net.sf.hibernate.type.SetType.wrap(SetType.java:27) at net.sf.hibernate.impl.SessionImpl.wrap(SessionImpl.java:2496) at net.sf.hibernate.impl.SessionImpl.wrap(SessionImpl.java:2436) at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:723) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:620) at test.Test.createHusband(Test.java:75) at test.Test.main(Test.java:39) 配置xml如下: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="test.B_husband" table="B_HUSBANDS" proxy="test.B_husband"> <id name="id" column="husband_id"> <generator class="assigned"/> </id> <property name="name" column="name" not-null="true" unique="true"/> <one-to-one name="wife" class="test.B_wife" constrained="true" cascade="all"/> <set name="childs" inverse="true" lazy="true" order-by="child_id" cascade="all"> <key column="husband_id"/> <one-to-many class="test.B_child"/> </set> </class> </hibernate-mapping> /*******************************************/ <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="test.B_wife" table="B_WIFES"> <id name="id" column="wife_id"> <generator class="native"/> </id> <property name="name"/> </class> </hibernate-mapping> /***************************************************/ <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="test.B_child" table="B_CHILDS" dynamic-update="true"> <id name="id" column="child_id"> <generator class="native"/> </id> <property name="name" column="name" not-null="true"/> <property name="sex" column="SEX" not-null="true"/> <many-to-one name="b_husband" column="husband_id" not-null="true"/> </class> </hibernate-mapping> 哪位老大帮帮忙告诉我阿, 很急! 在线等, 谢了! 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2003-12-17
不好意思, 版主, 怎么对代码code格式化阿, 我选择那段并且用了哪个code按钮, 可是好像没有用阿.
|
|
返回顶楼 | |
发表时间:2003-12-17
如图
|
|
返回顶楼 | |
发表时间:2003-12-17
B_wife wife = test1.createWife("wife");
B_husband husband = test1.createHusband("husband", wife); 这里写错了。 应该是: B_wife wife = (B_wife)test1.createWife("wife"); .... |
|
返回顶楼 | |
发表时间:2003-12-17
我是用了哪个按钮, 而且还多点了两次, 就怕没用,怎么回事阿?
|
|
返回顶楼 | |
发表时间:2003-12-17
不好意思阿, 刚刚改了按照版主的用法, 不过还是一样的, 怎么回事阿? wife表插入了一条记录, husband出了一样的错, 昨天就是这样的. 还是要麻烦版主了.
java.lang.ClassCastException at net.sf.hibernate.type.SetType.wrap(SetType.java:27) at net.sf.hibernate.impl.SessionImpl.wrap(SessionImpl.java:2496) at net.sf.hibernate.impl.SessionImpl.wrap(SessionImpl.java:2436) at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:723) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:620) at test.Test.createHusband(Test.java:77) at test.Test.main(Test.java:38) |
|
返回顶楼 | |
发表时间:2003-12-17
private List childs; <set name="childs" inverse="true" lazy="true" order-by="child_id" cascade="all"> <key column="husband_id"/> <one-to-many class="test.B_child"/> </set> List,set? |
|
返回顶楼 | |
发表时间:2003-12-17
谢谢, 我改过了之后可以正常运行了.
如果使用Set Class 在 .hbm.xml 中要用 <Set/> , 如果使用List Class 在 .hbm.xml 中要用 <Bag/> , 虽然不知道为什么, 我会再仔细看看文档的, 刚刚用Hibernate 两天, 所以有点不知道该怎么调试, 而且它的Exception信息很奇怪, 都是他内部的类出错, 我又不能跟踪进去看, 不知道这种情况下有什么办法可以较快的调试吗? |
|
返回顶楼 | |
发表时间:2003-12-17
有必要再多说两句, 我之所以会犯这个错误, 是 因为我在只用
两个Class测试一对多的时候, List Class 使用 <set/>元素是可以正常存取的, 所以在加了一个一对一之后我就没有在意这个是否匹配, 还是不知道为什么会出现这样的情况, 可能是 Hibernate内部的处理多个Class的时候, 没有把这个情况处理掉,提醒一下, 还有谢谢解答的朋友. |
|
返回顶楼 | |