|
|
准备找工作,重新整理一下Hibernate,今天做了集合映射的测试,以前是使用MyEclipse自动生成,今天开始手动配置。
在手动配置的过程中还真是出了不少问题,少包,漏写这漏写那,不过还好,很快都把错误排除了。
其中这个问题记录一下:
No CurrentSessionContext configured!" 异常
之前都是getSession()或用spring整合做web,所以没有注意到单独使用hibernate时如果用SessionFactory.getCurrentSession()要配置上这个:
<property name="current_session_context_class">thread</property>
为当前Session指定一个策略。
Set集合测试
build.xml
<?xml version="1.0"?><project default="main" basedir="."><property name="src" value="src"/> <property name="bin" value="bin"/><property name="lib" value="lib"/><property name="xdoclet.home" value="D:/xdoclet-plugins-1.0.3"/><path id="xdoclet.task.classpath"><fileset dir="${xdoclet.home}/lib"><include name="**/*.jar"/></fileset><fileset dir="${xdoclet.home}/plugins"><include name="**/*.jar"/></fileset></path><taskdef name="xdoclet"classname="org.xdoclet.ant.XDocletTask"classpathref="xdoclet.task.classpath"/><target name="生成配置文件"><xdoclet><fileset dir="${src}/pojo/"><include name="**/*.java"/></fileset><componentclassname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin"destdir="${src}"version="3.0"hbm2ddlauto="update"jdbcurl="jdbc:mysql://localhost/hib"jdbcdriver="com.mysql.jdbc.Driver"jdbcusername="root"jdbcpassword="520"dialect="org.hibernate.dialect.MySQLDialect"showsql="true"/></xdoclet></target><target name="生成映射文件"><xdoclet><fileset dir="${src}/pojo"><include name="**/*.java"/></fileset><component classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"version="3.0"destdir="${src}"/></xdoclet></target></project>
Hibernate.cfg.xml
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost/hib</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">520</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="current_session_context_class">thread</property> <mapping resource="pojo/Customer.hbm.xml"/> <mapping resource="pojo/Img.hbm.xml"/> </session-factory></hibernate-configuration>
Customer.java
package pojo;import java.util.Date;import java.util.HashSet;import java.util.Set;/** * @author Administrator *@hibernate.class *table=T_Customer */public class Customer {/** * @hibernate.id * column="cid" * generator-class="native" */private int id;/** * @hibernate.property * column="name" */private String name;/** * @hibernate.property * column="data" */private Date data;/** * @hibernate.property * column="address" */private String address;/** * @hibernate.set * table="T_Img" * @hibernate.key * column="cid" * @hibernate.element * column="name" * type="string" */private Set img;}
Img.java
package pojo;/** * * @author Administrator *@hibernate.class *table="T_Img" */public class Img {/** * @hibernate.id * column="img_id" * generator-class="native" */private int id;/** * @hibernate.property * column="name" */private String name;}
//manager
public class CustomerManagerImpl extends BaseManager implements CustomerManager {public void addCustomer(Customer customer) {Session session=getSession(); Transaction tr = session.beginTransaction();session.save(customer);tr.commit();closeSession(session);}public void delCustomer(int id) {Session session=getSession(); Transaction tr = session.beginTransaction(); Customer customer=(Customer)session.load(Customer.class, id);session.delete(customer);tr.commit();closeSession(session);}}
//测试类
public class CustomerManagerImplTest extends TestCase {CustomerManagerImpl m=new CustomerManagerImpl();public void testAddCustomer() {Customer c=new Customer();c.setName("死亡骑士");c.setAddress("五一九路");c.setData(new Date());Set img=new HashSet();img.add("aaa");img.add("bbb");img.add("aaa");c.setImg(img);m.addCustomer(c);}public void testDelCustomer() {m.delCustomer(2);}}
数据库中添加aaa、bbb字段,把重复的项去了。
List映射
将Customer.java中img注释改成如下
/** * @hibernate.list * table="T_Img" * @hibernate.key * column="cid" * @hibernate.list-index * column="ind" * @hibernate.element * type="string" * column="name" */private List img;
在Img.java中添加一个索引字段ind
/** * @hibernate.property * column="ind" */private int ind;
OK其它都和Set一样
数据库中添加aaa、bbb、aaa字段。充许添加重复项。
Map映射
和Set差不多,只是加了一个index项作为map的key
Customer.java改动
/** * @hibernate.map * table="T_Img" * @hibernate.key * column="cid" * @hibernate.index * column="imgname" * type="string" * @hibernate.element * type="int" * column="size" */private Map img;
Img.java
/** * @hibernate.property * column="imgname" *///键private String imgname;/** * @hibernate.property * column="size" *///值private int size;
测试:
public void testAddCustomer() {Customer c=new Customer();c.setName("李白");c.setAddress("五一九路");c.setData(new Date());Map img=new HashMap();img.put("img4",32);img.put("img5",123);img.put("img4",97);c.setImg(img);m.addCustomer(c);}
数据库同样只添加了img4、img5,去除了重复项。 |
|