财色兼收苑咨崆:hibernate入门使用系列 3-- xml关系映射篇(中) - 围墙 - ITeye技...

来源:百度文库 编辑:九乡新闻网 时间:2024/05/05 19:29:19
2008-05-07

hibernate入门使用系列 3-- xml关系映射篇(中)

关键字: hibernate

接上篇 hibernate入门使用系列 2-- xml关系映射篇(上)

上篇讲了1:1,那么这次继续讲1:n和n:1。

这次用到的例子是Father和child之间的关系。一个father可以有n个child,但是1个child只有一个father。这里只说生父。至于其他的继父、养父、干爹等等,不再范围之内。

好吧。还是同前面的一样。现建立实体模型如下:

 

 

根据模型创建数据库。sql脚本如下:

Sql代码  
  1. use HibernateQuickUse;   
  2. drop table if exists Child;   
  3. drop table if exists Father;   
  4.   
  5. create table Father (   
  6.     id varchar(32) primary key,   
  7.     name varchar(32) not null  
  8. );   
  9.   
  10. create table Child (   
  11.     id varchar(32) primary key,   
  12.     name varchar(128) not null,   
  13.     father_id varchar(32) not null,   
  14.     foreign key(father_id) references Father(id)   
  15. );  

 

根据模型创建java对象。

Father.java:

Java代码  
  1. package org.py.hib.relation.one2many;   
  2.   
  3. import java.util.HashSet;   
  4. import java.util.Set;   
  5.   
  6. /**  
  7.  * Father entity.  
  8.  */  
  9.   
  10. @SuppressWarnings("serial")   
  11. public class Father implements java.io.Serializable   
  12. {   
  13.     private String id;   
  14.   
  15.     private String name;   
  16.   
  17.     private Set children = new HashSet(0);   
  18.   
  19.     public Father()   
  20.     {   
  21.     }   
  22.   
  23.     public String getId()   
  24.     {   
  25.         return this.id;   
  26.     }   
  27.   
  28.     public void setId(String id)   
  29.     {   
  30.         this.id = id;   
  31.     }   
  32.   
  33.     public String getName()   
  34.     {   
  35.         return this.name;   
  36.     }   
  37.   
  38.     public void setName(String name)   
  39.     {   
  40.         this.name = name;   
  41.     }   
  42.   
  43.     public Set getChildren()   
  44.     {   
  45.         return children;   
  46.     }   
  47.   
  48.     public void setChildren(Set children)   
  49.     {   
  50.         this.children = children;   
  51.     }   
  52. }  

 

 Child.java:

Java代码  
  1. package org.py.hib.relation.one2many;   
  2.   
  3. /**  
  4.  * Child entity.  
  5.  * @author MyEclipse Persistence Tools  
  6.  */  
  7.   
  8. @SuppressWarnings("serial")   
  9. public class Child implements java.io.Serializable   
  10. {   
  11.     private String id;   
  12.   
  13.     private String name;   
  14.   
  15.     private Father father;   
  16.   
  17.     public Child()   
  18.     {   
  19.     }   
  20.   
  21.     public String getId()   
  22.     {   
  23.         return this.id;   
  24.     }   
  25.   
  26.     public void setId(String id)   
  27.     {   
  28.         this.id = id;   
  29.     }   
  30.   
  31.     public Father getFather()   
  32.     {   
  33.         return this.father;   
  34.     }   
  35.   
  36.     public void setFather(Father father)   
  37.     {   
  38.         this.father = father;   
  39.     }   
  40.   
  41.     public String getName()   
  42.     {   
  43.         return this.name;   
  44.     }   
  45.   
  46.     public void setName(String name)   
  47.     {   
  48.         this.name = name;   
  49.     }   
  50.   
  51. }  

 

映射文件如下:

Father.hbm.xml:

Xml代码  
  1.  version="1.0" encoding="utf-8"?>  
  2. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  3.   
  4.   
  5.      name="org.py.hib.relation.one2many.Father" table="father">  
  6.          name="id" type="java.lang.String" column="id" length="32">  
  7.              class="uuid" />  
  8.           
  9.   
  10.          name="name" type="java.lang.String" column="name" length="32" not-null="true"/>  
  11.            
  12.      name="children" table="child" cascade="all" inverse="true">  
  13.              column="father_id" />  
  14.              class="org.py.hib.relation.one2many.Child" />  
  15.           
  16.       
  17.   

这里要说说 "set" 这个标签里面的内容。

"name"是Father里面的属性的名字。

"table"表示它对应的是数据库中的哪个表。

cascade="all" 表示所有的操作都级联操作。

"inverse"表示关系的维护由谁来执行。true表示不由自己执行,而有对应的另外一方执行。false则相反,表示由自己维护关系。这里设置成 true 是由原因的。如果说把它设置成为false,那么就由他来维护关系了。

这里得说一下inverse属性的问题。在one-to-many中,如果关系由one来维护,那么会很麻烦,性能也会很低。每次对many一方的一条记录进行增、删、改 时都会多一次update操作。原因很简单,因为关系的维护设置在了one这一方,所以对many的每一次操作,one这一方都要维护一次双方的关系。

这个就好像皇帝和老百姓的关系。试问,是来一个老百姓,皇帝就宣布他是我的子民,还是由老百姓直接选择做那个皇帝的子民更加有效率呢?呵呵。不知道这个例子大家有没有明白。关于inverse的更具体的说明,在javaeye上搜一下,就会发现有很多。这里推荐一篇,我认为讲得很明白的:主题:inverse。

"key" 中的 "column" 表示在table(这里的table是child)中, 跟Father关联的字段名称。这里是"father_id"。可以看看开始的sql脚本。

one-to-many 表示father和children的关系。class则表示是同哪个类是这种关系。

 

 Child.hbm.xml:

Java代码  
  1. "1.0" encoding="utf-8"?>   
  2. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
  4.   
  5.   
  6.     <class name="org.py.hib.relation.one2many.Child" table="child">   
  7.         "id" type="java.lang.String" column="id" length="32" >   
  8.             class="uuid" />   
  9.            
  10.         "name" type="java.lang.String" column="name" length="128" not-null="true"/>   
  11.   
  12.         "father" class="org.py.hib.relation.one2many.Father" column="father_id" />   
  13.     class>   
  14.   

 这个里面主要就是多了一个many-to-one,表示child 和 father 的关系是"many-to-one"

 

测试代码如下:

One2ManyTest.java

Java代码  
  1. package org.py.hib.relation.one2many;   
  2.   
  3. import java.util.Set;   
  4.   
  5. import junit.framework.Assert;   
  6. import junit.framework.TestCase;   
  7.   
  8. import org.hibernate.Session;   
  9. import org.hibernate.SessionFactory;   
  10. import org.hibernate.Transaction;   
  11. import org.hibernate.cfg.Configuration;   
  12. import org.junit.After;   
  13. import org.junit.Before;   
  14.   
  15. public class One2ManyTest extends TestCase   
  16. {   
  17.     private SessionFactory factory;   
  18.   
  19.     private static final String[] childname = new String[] { "child_1""child_2""child_3" };   
  20.   
  21.     private static final String[] newchildname = new String[] { "new_child_1""new_child_2""new_child_3" };   
  22.   
  23.     @Before  
  24.     public void setUp() throws Exception   
  25.     {   
  26.         Configuration conf = new Configuration().configure();   
  27.         factory = conf.buildSessionFactory();   
  28.     }   
  29.   
  30.     /**  
  31.      * 测试添加  
  32.      * @throws Exception  
  33.      */  
  34.     public void testSave() throws Exception   
  35.     {   
  36.         System.out.println("\n=== test save ===");   
  37.   
  38.         Father father = new Father();   
  39.         father.setName("Father_1");   
  40.   
  41.         Child child1 = new Child();   
  42.         child1.setName(childname[0]);   
  43.   
  44.         Child child2 = new Child();   
  45.         child2.setName(childname[1]);   
  46.   
  47.         Child child3 = new Child();   
  48.         child3.setName(childname[2]);   
  49.   
  50.         father.getChildren().add(child1);   
  51.         father.getChildren().add(child2);   
  52.         father.getChildren().add(child3);   
  53.   
  54.         child1.setFather(father);   
  55.         child2.setFather(father);   
  56.         child3.setFather(father);   
  57.   
  58.         Session session = null;   
  59.         Transaction tran = null;   
  60.         try  
  61.         {   
  62.             session = factory.openSession();   
  63.             tran = session.beginTransaction();   
  64.             session.save(father);   
  65.   
  66.             tran.commit();   
  67.   
  68.             Assert.assertNotNull(father.getId());   
  69.   
  70.             Assert.assertNotNull(child1.getId());   
  71.             Assert.assertNotNull(child2.getId());   
  72.             Assert.assertNotNull(child3.getId());   
  73.   
  74.         } catch (Exception ex)   
  75.         {   
  76.             tran.rollback();   
  77.             throw ex;   
  78.         } finally  
  79.         {   
  80.             if (session != null)   
  81.             {   
  82.                 try  
  83.                 {   
  84.                     session.close();   
  85.                 } catch (Exception ex)   
  86.                 {   
  87.                     // nothing to do   
  88.                 } finally  
  89.                 {   
  90.                     if (session != null)   
  91.                         session = null;   
  92.                 }   
  93.             }   
  94.         }   
  95.     }   
  96.   
  97.     private boolean isChildrenName(String name)   
  98.     {   
  99.         for (String n : childname)   
  100.         {   
  101.             if (n.equals(name))   
  102.                 return true;   
  103.         }   
  104.   
  105.         return false;   
  106.     }   
  107.   
  108.     private boolean isNewChildrenName(String name)   
  109.     {   
  110.         for (String n : newchildname)   
  111.         {   
  112.             if (n.equals(name))   
  113.                 return true;   
  114.         }   
  115.   
  116.         return false;   
  117.     }   
  118.   
  119.     /**  
  120.      * 测试查询  
  121.      * @throws Exception  
  122.      */  
  123.     public void testFind() throws Exception   
  124.     {   
  125.         System.out.println("\n=== test find ===");   
  126.         Session session = null;   
  127.         try  
  128.         {   
  129.             session = factory.openSession();   
  130.             Father father = (Father) session.createQuery("from Father").list().get(0);   
  131.   
  132.             Assert.assertNotNull(father.getId());   
  133.             Assert.assertEquals("Father_1", father.getName());   
  134.   
  135.             Set children = father.getChildren();   
  136.             for (Child child : children)   
  137.             {   
  138.                 Assert.assertEquals(child.getFather(), father);   
  139.   
  140.                 Assert.assertNotNull(child.getId());   
  141.   
  142.                 Assert.assertTrue(isChildrenName(child.getName()));   
  143.             }   
  144.         } catch (Exception ex)   
  145.         {   
  146.             throw ex;   
  147.         } finally  
  148.         {   
  149.             if (session != null)   
  150.             {   
  151.                 try  
  152.                 {   
  153.                     session.close();   
  154.                 } catch (Exception ex)   
  155.                 {   
  156.                     // nothing to do   
  157.                 } finally  
  158.                 {   
  159.                     if (session != null)   
  160.                         session = null;   
  161.                 }   
  162.             }   
  163.         }   
  164.     }   
  165.   
  166.     /**  
  167.      * 测试修改  
  168.      * @throws Exception  
  169.      */  
  170.     public void testModify() throws Exception   
  171.     {   
  172.         System.out.println("\n=== test modify ===");   
  173.         Session session = null;   
  174.         Transaction tran = null;   
  175.         try  
  176.         {   
  177.             session = factory.openSession();   
  178.             tran = session.beginTransaction();   
  179.   
  180.             Father father = (Father) session.createQuery("from Father").list().get(0);   
  181.             father.setName("Father_2"); // 修改用户名 = m_name2.(原来用户名= m_name)   
  182.   
  183.             Set children = father.getChildren();   
  184.             int i = 0;   
  185.             for (Child child : children)   
  186.             {   
  187.                 child.setName(newchildname[i++]);   
  188.             }   
  189.   
  190.             tran.commit();   
  191.   
  192.         } catch (Exception ex)   
  193.         {   
  194.             throw ex;   
  195.         } finally  
  196.         {   
  197.             if (session != null)   
  198.             {   
  199.                 try  
  200.                 {   
  201.                     session.close();   
  202.                 } catch (Exception ex)   
  203.                 {   
  204.                     // nothing to do   
  205.                 } finally  
  206.                 {   
  207.                     if (session != null)   
  208.                         session = null;   
  209.                 }   
  210.             }   
  211.         }   
  212.   
  213.         /*  
  214.          * 修改后再查询  
  215.          */  
  216.         System.out.println("\n=== test find after modify ===");   
  217.         try  
  218.         {   
  219.             session = factory.openSession();   
  220.             Father father = (Father) session.createQuery("from Father").list().get(0);   
  221.   
  222.             Assert.assertNotNull(father.getId());   
  223.             Assert.assertEquals("Father_2", father.getName());   
  224.   
  225.             Set children = father.getChildren();   
  226.   
  227.             for (Child child : children)   
  228.             {   
  229.                 Assert.assertEquals(child.getFather(), father);   
  230.   
  231.                 Assert.assertNotNull(child.getId());   
  232.   
  233.                 Assert.assertTrue(isNewChildrenName(child.getName()));   
  234.             }   
  235.   
  236.         } catch (Exception ex)   
  237.         {   
  238.             throw ex;   
  239.         } finally  
  240.         {   
  241.             if (session != null)   
  242.             {   
  243.                 try  
  244.                 {   
  245.                     session.close();   
  246.                 } catch (Exception ex)   
  247.                 {   
  248.                     // nothing to do   
  249.                 } finally  
  250.                 {   
  251.                     if (session != null)   
  252.                         session = null;   
  253.                 }   
  254.             }   
  255.         }   
  256.     }   
  257.   
  258.     /**  
  259.      * 测试删除  
  260.      * @throws Exception  
  261.      */  
  262.     public void testDelete() throws Exception   
  263.     {   
  264.         System.out.println("\n=== test delete ===");   
  265.         Session session = null;   
  266.         Transaction tran = null;   
  267.         try  
  268.         {   
  269.             session = factory.openSession();   
  270.             tran = session.beginTransaction();   
  271.   
  272.             Father father = (Father) session.createQuery("from Father").list().get(0);   
  273.             session.delete(father);   
  274.             tran.commit();   
  275.   
  276.         } catch (Exception ex)   
  277.         {   
  278.             throw ex;   
  279.         } finally  
  280.         {   
  281.             if (session != null)   
  282.             {   
  283.                 try  
  284.                 {   
  285.                     session.close();   
  286.                 } catch (Exception ex)   
  287.                 {   
  288.                     // nothing to do   
  289.                 } finally  
  290.                 {   
  291.                     if (session != null)   
  292.                         session = null;   
  293.                 }   
  294.             }   
  295.         }   
  296.   
  297.         /*  
  298.          * 删除后再查询  
  299.          */  
  300.         System.out.println("\n=== test find after delete ===");   
  301.         try  
  302.         {   
  303.             session = factory.openSession();   
  304.   
  305.             Integer num = (Integer) session.createQuery("from Father").list().size();   
  306.             Assert.assertEquals(0, num.intValue());   
  307.   
  308.             num = (Integer) session.createQuery("from Child").list().size();   
  309.             Assert.assertEquals(0, num.intValue());   
  310.   
  311.         } catch (Exception ex)   
  312.         {   
  313.             throw ex;   
  314.         } finally  
  315.         {   
  316.             if (session != null)   
  317.             {   
  318.                 try  
  319.                 {   
  320.                     session.close();   
  321.                 } catch (Exception ex)   
  322.                 {   
  323.                     // nothing to do   
  324.                 } finally  
  325.                 {   
  326.                     if (session != null)   
  327.                         session = null;   
  328.                 }   
  329.             }   
  330.         }   
  331.     }   
  332.   
  333.     /**  
  334.      *   
  335.      */  
  336.     @After  
  337.     public void tearDown() throws Exception   
  338.     {   
  339.         factory.close();   
  340.     }   
  341.   
  342. }  
 

这里不得不再重申以下 one-to-many 中 inverse 关系的维护问题。 在one-to-many中,把inverse放到many中来维护是一个好的习惯。大家可以把上面的inverse改成false,看看会发生什么情况。

在inverse=true的时候,输出结果如下:

Sql代码  
  1. === test save ===   
  2. Hibernate: insert into father (name, id) values (?, ?)   
  3. Hibernate: insert into child (name, father_id, id) values (?, ?, ?)   
  4. Hibernate: insert into child (name, father_id, id) values (?, ?, ?)   
  5. Hibernate: insert into child (name, father_id, id) values (?, ?, ?)   
  6.   
  7. === test find ===   
  8. Hibernate: select father0_.id as id13_, father0_.name as name13_ from father father0_   
  9. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id14_0_, children0_.name as name14_0_, children0_.father_id as father3_14_0_ from child children0_ where children0_.father_id=?   
  10.   
  11. === test modify ===   
  12. Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_   
  13. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=?   
  14. Hibernate: update father set name=? where id=?   
  15. Hibernate: update child set name=?, father_id=? where id=?   
  16. Hibernate: update child set name=?, father_id=? where id=?   
  17. Hibernate: update child set name=?, father_id=? where id=?   
  18.   
  19. === test find after modify ===   
  20. Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_   
  21. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=?   
  22.   
  23. === test delete ===   
  24. Hibernate: select father0_.id as id33_, father0_.name as name33_ from father father0_   
  25. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id34_0_, children0_.name as name34_0_, children0_.father_id as father3_34_0_ from child children0_ where children0_.father_id=?   
  26. Hibernate: delete from child where id=?   
  27. Hibernate: delete from child where id=?   
  28. Hibernate: delete from child where id=?   
  29. Hibernate: delete from father where id=?   
  30.   
  31. === test find after delete ===   
  32. Hibernate: select father0_.id as id33_, father0_.name as name33_ from father father0_   
  33. Hibernate: select child0_.id as id34_, child0_.name as name34_, child0_.father_id as father3_34_ from child child0_  
 

而改成 inverse=false后,testDelete()是没法通过的。输出如下:

Sql代码  
  1. log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).   
  2. log4j:WARN Please initialize the log4j system properly.   
  3.   
  4. === test save ===   
  5. Hibernate: insert into father (name, id) values (?, ?)   
  6. Hibernate: insert into child (name, father_id, id) values (?, ?, ?)   
  7. Hibernate: insert into child (name, father_id, id) values (?, ?, ?)   
  8. Hibernate: insert into child (name, father_id, id) values (?, ?, ?)   
  9. Hibernate: update child set father_id=? where id=?   
  10. Hibernate: update child set father_id=? where id=?   
  11. Hibernate: update child set father_id=? where id=?   
  12.   
  13. === test find ===   
  14. Hibernate: select father0_.id as id13_, father0_.name as name13_ from father father0_   
  15. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id14_0_, children0_.name as name14_0_, children0_.father_id as father3_14_0_ from child children0_ where children0_.father_id=?   
  16.   
  17. === test modify ===   
  18. Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_   
  19. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=?   
  20. Hibernate: update father set name=? where id=?   
  21. Hibernate: update child set name=?, father_id=? where id=?   
  22. Hibernate: update child set name=?, father_id=? where id=?   
  23. Hibernate: update child set name=?, father_id=? where id=?   
  24.   
  25. === test find after modify ===   
  26. Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_   
  27. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=?   
  28.   
  29. === test delete ===   
  30. Hibernate: select father0_.id as id33_, father0_.name as name33_ from father father0_   
  31. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id34_0_, children0_.name as name34_0_, children0_.father_id as father3_34_0_ from child children0_ where children0_.father_id=?   
  32. Hibernate: update child set father_id=null where father_id=?  

 错误信息如下:

具体的出错原因是:违反了非空约束。

得修改sql脚本,把Child的建表脚本中的:

                     father_id varchar(32) not null, 修改成为:father_id varchar(32),

才能通过。这个时候输出的结果是:

Sql代码  
  1. log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).   
  2. log4j:WARN Please initialize the log4j system properly.   
  3.   
  4. === test save ===   
  5. Hibernate: insert into father (name, id) values (?, ?)   
  6. Hibernate: insert into child (name, father_id, id) values (?, ?, ?)   
  7. Hibernate: insert into child (name, father_id, id) values (?, ?, ?)   
  8. Hibernate: insert into child (name, father_id, id) values (?, ?, ?)   
  9. Hibernate: update child set father_id=? where id=?   
  10. Hibernate: update child set father_id=? where id=?   
  11. Hibernate: update child set father_id=? where id=?   
  12.   
  13. === test find ===   
  14. Hibernate: select father0_.id as id13_, father0_.name as name13_ from father father0_   
  15. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id14_0_, children0_.name as name14_0_, children0_.father_id as father3_14_0_ from child children0_ where children0_.father_id=?   
  16.   
  17. === test modify ===   
  18. Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_   
  19. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=?   
  20. Hibernate: update father set name=? where id=?   
  21. Hibernate: update child set name=?, father_id=? where id=?   
  22. Hibernate: update child set name=?, father_id=? where id=?   
  23. Hibernate: update child set name=?, father_id=? where id=?   
  24.   
  25. === test find after modify ===   
  26. Hibernate: select father0_.id as id23_, father0_.name as name23_ from father father0_   
  27. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id24_0_, children0_.name as name24_0_, children0_.father_id as father3_24_0_ from child children0_ where children0_.father_id=?   
  28.   
  29. === test delete ===   
  30. Hibernate: select father0_.id as id33_, father0_.name as name33_ from father father0_   
  31. Hibernate: select children0_.father_id as father3_1_, children0_.id as id1_, children0_.id as id34_0_, children0_.name as name34_0_, children0_.father_id as father3_34_0_ from child children0_ where children0_.father_id=?   
  32. Hibernate: update child set father_id=null where father_id=?   
  33. Hibernate: delete from child where id=?   
  34. Hibernate: delete from child where id=?   
  35. Hibernate: delete from child where id=?   
  36. Hibernate: delete from father where id=?   
  37.   
  38. === test find after delete ===   
  39. Hibernate: select father0_.id as id33_, father0_.name as name33_ from father father0_   
  40. Hibernate: select child0_.id as id34_, child0_.name as name34_, child0_.father_id as father3_34_ from child child0_  
 

所以,inverse的设置是很重要的一个事情。

附件中包含了源代码。这里说明一下。源代码中没有包含数据库驱动的jar包和hibernate3.1的jar包。需要大家自己添加。

 

  • 大小: 75 KB
  • HibernateQuickUse.zip (12.7 KB)
  • 下载次数: 164
  • 大小: 3.1 KB
  • 查看图片附件
hibernate入门使用系列 4-- xml关系映射篇 ... | hibernate入门使用系列 2-- xml关系映射篇 ...
  • 14:40
  • 浏览 (1157)
  • 论坛浏览 (5229)
  • 评论 (9)
  • 分类: hibernate
  • 收藏
  • 相关推荐
评论
9 楼 xiaojiit 2008-12-04   引用 能不能写一个用DetachedCriteria进行查询的例子呢?谢谢了!8 楼 流浪者A 2008-11-05   引用 这个例子经常见到,很容易理解,很好的例子~·7 楼 rmn190 2008-10-30   引用 一个问题:
   现在你例子中是双向关联, 也就是说child类有对father类的引向, 这样在Child.hbm.xml中就有了, 这里的inverse是默认为false的. 这时两表间的引用由child类的setFather触发.

  若业务要求为单向关联,也就是说在child这边不再有father的引用, 这样一来两表间的关联关系只能由father类来维护了, 当然在里的inverse默认为false的, 可这样做效率上是受影响的:  当加child集合时hibernate得去检查看哪个child与father的关系有改变, 而假若由child这边来维护的话,就不会有这样效率方面的问题.

  可在child的配置里若没有对father的引用后,我们也就无从下手了,不能两全其美.6 楼 LuckyAngel 2008-10-24   引用 Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList
我的程序中出现这个错误,是什么意思?请教一下
我用的是一对多关系映射,一的这方用的是List集合5 楼 RyanPoy 2008-06-17   引用 wm920 写道
我要是实现自定义表的字段···怎么实现咯 ··


不明白什么叫自定义表的字段。能否举一例?

wm920 写道
他们的数据能进行查进去 么 ,因为表的关联是一下子取(通SET与GET)全部的数据···  会报SQL语句的异常

表的关联可以不一下子全取。hibernate提供了 延迟处理。3版本后默认为lazy。

关于lazy的用法参看这个,http://www.hibernate.org/162.html

4 楼 RyanPoy 2008-06-17   引用 e3002 写道你好。最近遇到个问题。也是一对多双向关联的问题,举例
一个题目可以有a。b。c。d四个选项。题目类:subject 选项类 chooseItem
用户第一次建立题目是 可能是这样、
那个省人口最多
A.河南
B.河北
C.湖北
D.湖南
ok。保存入库,但用户下次修改时
可能如下
那个省人口最多
A.河南
B.河北
注意:只剩下两个选项 到此,此时的操作应该包括删除和修改多方的操作,这个hibernate能自动处理吗?该怎么处理?请赐教



完全可以。看帖子里面的测试代码。里面有完整的增删该查。你可以仿照从而写出你的业务逻辑。3 楼 wm920 2008-06-16   引用 请问下 ···

我要是实现自定义表的字段···怎么实现咯 ··
他们的数据能进行查进去 么 ,因为表的关联是一下子取(通SET与GET)全部的数据···  会报SQL语句的异常···2 楼 e3002 2008-05-27   引用 你好。最近遇到个问题。也是一对多双向关联的问题,举例
一个题目可以有a。b。c。d四个选项。题目类:subject 选项类 chooseItem
用户第一次建立题目是 可能是这样、
那个省人口最多
A.河南
B.河北
C.湖北
D.湖南
ok。保存入库,但用户下次修改时
可能如下
那个省人口最多
A.河南
B.河北
注意:只剩下两个选项 到此,此时的操作应该包括删除和修改多方的操作,这个hibernate能自动处理吗?该怎么处理?请赐教

1 楼 hantsy 2008-05-10   引用

在这种one to many 的双向关联中,按照需求,更符合Hibernate的作法是还要在你的 Child 映射文件的关联中加上not-null="true"的约束。

如果是单向的one to many 关联(没有Child 到Parent 的关联),这个约束可以加在Parent的映射文件的set定义中去。