转载 Hibernate集合映射---XDoclet应用收藏

重新整理一下Hibernate,做了集合映射的测试,以前是使用MyEclipse自动生成,开始手动配置。在手动配置的过程中还真是出了不少问题,少包,漏写这漏写那,不过还好,很快都把错误排除了。其中这个问题记录一下: No CurrentSessionContext configured!" 异常,之前都是getSession()或用spring整合做web,所以没有注意到单独使用hibernate时如果用SessionFactory.getCurrentSession()要配置上这个: <property name="current_session_context_class">thread</property> 为当前Session指定一个策略。Set集合测试

 

build.xml


  1. <?xml version="1.0"?>  
  2. <project default="main" basedir=".">  
  3.     <property name="src" value="src"/>  
  4.     <property name="bin" value="bin"/>  
  5.     <property name="lib" value="lib"/>  
  6.     <property name="xdoclet.home" value="D:/xdoclet-plugins-1.0.3"/>  
  7.     <path id="xdoclet.task.classpath">  
  8.         <fileset dir="${xdoclet.home}/lib">  
  9.             <include name="**/*.jar"/>  
  10.         </fileset>  
  11.         <fileset dir="${xdoclet.home}/plugins">  
  12.             <include name="**/*.jar"/>  
  13.         </fileset>  
  14.     </path>  
  15.        
  16.     <taskdef    
  17.         name="xdoclet"  
  18.         classname="org.xdoclet.ant.XDocletTask"  
  19.         classpathref="xdoclet.task.classpath"  
  20.     />  
  21.     <target name="生成配置文件">  
  22.         <xdoclet>  
  23.             <fileset dir="${src}/pojo/">  
  24.                 <include name="**/*.java"/>  
  25.             </fileset>  
  26.             <component  
  27.                 classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin"  
  28.                     destdir="${src}"  
  29.                     version="3.0"  
  30.                     hbm2ddlauto="update"  
  31.                     jdbcurl="jdbc:mysql://localhost/hib"  
  32.                     jdbcdriver="com.mysql.jdbc.Driver"  
  33.                     jdbcusername="root"  
  34.                     jdbcpassword="520"  
  35.                     dialect="org.hibernate.dialect.MySQLDialect"  
  36.                     showsql="true"     
  37.             />  
  38.         </xdoclet>  
  39.     </target>    
  40.        
  41.     <target name="生成映射文件">  
  42.         <xdoclet>  
  43.             <fileset dir="${src}/pojo">  
  44.                 <include name="**/*.java"/>  
  45.             </fileset>  
  46.             <component    
  47.                 classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"  
  48.                 version="3.0"  
  49.                 destdir="${src}"  
  50.             />  
  51.         </xdoclet>  
  52.     </target>  
  53. </project>  

 

Hibernate.cfg.xml


  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  3.   
  4. <hibernate-configuration>  
  5.   <session-factory>  
  6.     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  7.     <property name="hibernate.connection.url">jdbc:mysql://localhost/hib</property>  
  8.     <property name="hibernate.connection.username">root</property>  
  9.     <property name="hibernate.connection.password">520</property>  
  10.     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  11.     <property name="hibernate.show_sql">true</property>  
  12.     <property name="hibernate.hbm2ddl.auto">update</property>  
  13.     <property name="current_session_context_class">thread</property>  
  14.     <mapping resource="pojo/Customer.hbm.xml"/>  
  15.     <mapping resource="pojo/Img.hbm.xml"/>  
  16.   </session-factory>  
  17. </hibernate-configuration>  

 

Customer.java


  1. package pojo;   
  2. import java.util.Date;   
  3. import java.util.HashSet;   
  4. import java.util.Set;   
  5. /**  
  6.  * @author Administrator  
  7.  *@hibernate.class  
  8.  *  table=T_Customer  
  9.  */  
  10. public class Customer {   
  11.     /**  
  12.      * @hibernate.id  
  13.      * column="cid"  
  14.      *  generator-class="native"  
  15.      */  
  16.     private int id;   
  17.     /**  
  18.      * @hibernate.property  
  19.      *  column="name"  
  20.      */  
  21.     private String name;   
  22.     /**  
  23.      * @hibernate.property  
  24.      *  column="data"  
  25.      */  
  26.     private Date data;   
  27.     /**  
  28.      * @hibernate.property  
  29.      *  column="address"  
  30.      */  
  31.     private String address;   
  32.     /**  
  33.      * @hibernate.set   
  34.      * table="T_Img"  
  35.      * @hibernate.key  
  36.      *  column="cid"  
  37.      * @hibernate.element  
  38.      * column="name"  
  39.      * type="string"  
  40.      */  
  41.     private Set img;   
  42.     }  


Img.java


  1. package pojo;   
  2. /**  
  3.  *   
  4.  * @author Administrator  
  5.  *@hibernate.class  
  6.  *  table="T_Img"  
  7.  */  
  8. public class Img {   
  9.     /**  
  10.      * @hibernate.id  
  11.      * column="img_id"  
  12.      *  generator-class="native"  
  13.      */  
  14.     private int id;   
  15.     /**  
  16.      * @hibernate.property  
  17.      *  column="name"  
  18.      */  
  19.     private String name;   
  20. }  

 

//manager


  1. public class CustomerManagerImpl extends BaseManager implements CustomerManager {      
  2.     public void addCustomer(Customer customer) {   
  3.         Session session=getSession();   
  4.         Transaction tr = session.beginTransaction();   
  5.         session.save(customer);   
  6.         tr.commit();   
  7.         closeSession(session);   
  8.     }   
  9.     public void delCustomer(int id) {   
  10.         Session session=getSession();   
  11.         Transaction tr = session.beginTransaction();   
  12.         Customer customer=(Customer)session.load(Customer.class, id);   
  13.         session.delete(customer);   
  14.         tr.commit();   
  15.         closeSession(session);   
  16.     }   
  17. }  

 

//测试类


  1. public class CustomerManagerImplTest extends TestCase {    
  2.     CustomerManagerImpl m=new CustomerManagerImpl();   
  3.     public void testAddCustomer() {   
  4.         Customer c=new Customer();   
  5.         c.setName("死亡骑士");   
  6.         c.setAddress("五一九路");   
  7.         c.setData(new Date());   
  8.         Set img=new HashSet();   
  9.         img.add("aaa");   
  10.         img.add("bbb");   
  11.         img.add("aaa");   
  12.         c.setImg(img);   
  13.         m.addCustomer(c);   
  14.     }   
  15.   
  16.     public void testDelCustomer() {   
  17.         m.delCustomer(2);   
  18.     }   
  19. }  

 

数据库中添加aaa、bbb字段,把重复的项去了。
List映射
将Customer.java中img注释改成如下


  1. /**  
  2.  * @hibernate.list   
  3.  * table="T_Img"  
  4.  * @hibernate.key  
  5.  *  column="cid"  
  6.  * @hibernate.list-index  
  7.  *  column="ind"  
  8.  * @hibernate.element  
  9.  *  type="string"  
  10.  *  column="name"  
  11.  */  
  12. private List img;  

 

在Img.java中添加一个索引字段ind


  1. /**  
  2.  * @hibernate.property  
  3.  * column="ind"  
  4.  */  
  5. private int ind; 

 

 
OK其它都和Set一样
数据库中添加aaa、bbb、aaa字段。充许添加重复项。

Map映射
和Set差不多,只是加了一个index项作为map的key
Customer.java改动

  1. /**  
  2.  * @hibernate.map   
  3.  * table="T_Img"  
  4.  * @hibernate.key  
  5.  *  column="cid"  
  6.  * @hibernate.index  
  7.  *  column="imgname"  
  8.  *  type="string"  
  9.  * @hibernate.element  
  10.  *  type="int"  
  11.  *  column="size"  
  12.  */  
  13. private Map img;

 

Img.java


  1. /**  
  2.      * @hibernate.property  
  3.      *  column="imgname"  
  4.      */  
  5. //键   
  6.     private String imgname;   
  7.     /**  
  8.      * @hibernate.property  
  9.      * column="size"  
  10.      */  
  11. //值   
  12.     private int size; 

 

测试:


  1. public void testAddCustomer() {   
  2.     Customer c=new Customer();   
  3.     c.setName("李白");   
  4.     c.setAddress("五一九路");   
  5.     c.setData(new Date());   
  6.     Map img=new HashMap();   
  7.     img.put("img4",32);   
  8.     img.put("img5",123);   
  9.     img.put("img4",97);   
  10.     c.setImg(img);   
  11.     m.addCustomer(c);   
  12. }  

 

数据库同样只添加了img4、img5,去除了重复项。

发表于 @ 2008年07月20日 17:08:00|评论(loading...)|收藏

新一篇: 基于Hibernate的持久层的组成 | 旧一篇: Ajax实现二级联动菜单

Csdn Blog version 3.1a
Copyright © xxx405