- 浏览: 899992 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (537)
- Java SE (114)
- Struts (18)
- Hibernate (25)
- Spring (3)
- Page_Tech (41)
- Others (87)
- Database (29)
- Server (24)
- OpenSource_Tools (15)
- IDE_Tool (22)
- Algorithm (28)
- Interview (22)
- Test (28)
- Hardware (1)
- Mainframe (25)
- Web application (4)
- Linux (3)
- PHP (17)
- Android (1)
- Perl (6)
- ubuntu (1)
- Java EE (9)
- Web Analysis (5)
- Node.js (2)
- javascript (2)
最新评论
-
一键注册:
request.getRequestURL()和request.getRequestURI() -
SuperCustomer:
...
SED的暂存空间和模式空间 -
juyo_ch:
讲得挺好理解的,学习了
java 死锁及解决 -
chinaalex:
最后一题答案正确,但是分析有误.按照如下过程,上一行为瓶,下一 ...
zz智力题 -
liaowuxukong:
多谢博主啦,弱弱的了解了一点。
C++/Java 实现多态的方法(C++)
准备找工作,重新整理一下Hibernate,今天做了集合映射的测试,以前是使用MyEclipse自动生成,今天开始手动配置。
在手动配置的过程中还真是出了不少问题,少包,漏写这漏写那,不过还好,很快都把错误排除了。
其中这个问题记录一下:
No CurrentSessionContext configured!" 异常
之前都是getSession()或用spring整合做web,所以没有注意到单独使用hibernate时如果用SessionFactory.getCurrentSession()要配置上这个:
<property name="current_session_context_class">thread</property>
为当前Session指定一个策略。
Set集合测试
build.xml
Hibernate.cfg.xml
Customer.java
Img.java
//manager
//测试类
数据库中添加aaa、bbb字段,把重复的项去了。
List映射
将Customer.java中img注释改成如下
在Img.java中添加一个索引字段ind
OK其它都和Set一样
数据库中添加aaa、bbb、aaa字段。充许添加重复项。
Map映射
和Set差不多,只是加了一个index项作为map的key
Customer.java改动
Img.java
测试:
数据库同样只添加了img4、img5,去除了重复项。
在手动配置的过程中还真是出了不少问题,少包,漏写这漏写那,不过还好,很快都把错误排除了。
其中这个问题记录一下:
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>
- <component
- classname="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>
<?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>
<component
classname="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>
<?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;
- }
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;
- }
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 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);
- }
- }
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;
/**
* @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;
/**
* @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;
/**
* @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);
- }
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,去除了重复项。
发表评论
-
hibernate n+1问题
2010-10-21 11:05 899Hibernate中常会用到set,bag等集合表示1对多的关 ... -
Hibernate 关联关系 总结
2010-09-09 15:27 9391.一对多的单向关联关系 配置单向的一对多关系是 ... -
Hibernate 关联
2010-09-09 15:24 9651、hibernate多对一关联映 ... -
Hibernate一对多(单向)
2010-09-09 14:31 570[正文]: Hibernate一对多关联,例如一个用户有 ... -
Hibernate中No row with the given identifier exists问题的原因及解决
2010-06-23 09:54 910产生此问题的原因: ... -
Hibernate使用count(*)取得表中记录总数(跨Hibernate3.x版本问题)
2010-06-22 17:13 1506Java代码 /** * @T ... -
hibernate继承关系映射
2010-06-13 16:58 888hbn 的继承映射关系有这 ... -
高并发网站的架构
2010-05-07 11:07 697我在CERNET做过拨号接入平台的搭建,而后在Yaho ... -
Hibernate事务和并发控制
2010-05-07 10:21 8881. 事务介绍:1.1. 事务的定义:事务就 ... -
hibernate中lazy的使用
2009-12-18 22:00 754lazy,延迟加载 Lazy的 ... -
Hibernate中代码自动生成功能小结
2009-12-06 15:10 1005Hibernate中需要class和mapping file, ... -
hibernate工具箱—根据映射文件自动建表
2009-12-04 12:08 960public class ExportDB { ... -
关联加载对象时的报错-----a different object with the same identifier value
2009-11-18 16:13 852因为在hibernate中同一个session里面有了两个相同 ... -
update/saveOrUpdate/merge
2009-11-18 15:28 1125通常下面的场景会使用update()或saveOrUpdate ... -
写得很不错的-Hibernate中的实体状态(二)
2009-11-18 15:08 933(2)session.merge ()方法 ... -
写得很不错的-Hibernate中的实体状态(一)
2009-11-18 15:04 1122持久层的解决方案有许 ... -
Hibernate3.x总结
2009-11-18 14:29 729Hibernate不是盏省油的灯 ... -
Hibernate Annotation几种关联映射
2009-11-02 17:01 1054Hibernate Annotation几种关联映射 一对一 ... -
hibernate3的注解映射学习
2009-11-02 16:41 1322注解映射必须满足两大条件:Hibernate3.2以上版本和J ... -
Hibernate 中级联操作 cascade 选项
2009-11-02 16:35 908none :在保存、更新或删除对象时,忽略其他关联的对象。他是 ...
相关推荐
student与Score的Map集合关系 student与class的多对一 一对多映射
hibernate集合映射inverse和cascade详解.txt
NULL 博文链接:https://wintys.iteye.com/blog/426022
hibernate集合的映射 集合的映射 set list array map
适合初学者了解在hibernate中配置set,list,map,bag等集合映射
NULL 博文链接:https://lijunabc.iteye.com/blog/443381
Hibernate映射集合属性List,用于映射集合属性 List Hibernate 自动生成表 不需sql
NULL 博文链接:https://lijunabc.iteye.com/blog/443278
Hibernate常见集合映射(Set,List_Array,Map,Bag)
JAVA培训-HIBERNATE的集合映射.doc
Hibernate关系映射 Hibernate主键策略与属性 Hibernate集合映射
NULL 博文链接:https://lijunabc.iteye.com/blog/443353
hibernate中的集合映射,各种集合类型的使用方法及适用场景
NULL 博文链接:https://lijunabc.iteye.com/blog/443305
第12章 基于annotation注解技术的ssh 2整合开发 12.1 初识annotation522 12.2 亲身体验annotation的威力524 ...12.4.12 hibernate集合映射的annotation注解实现552 12.5 基于annotation的ssh 2整合开发554 12.6 小结563
2,建立表的hibernate支持,生成tb_user类和对应的配置文件 3,修改配置文件使之和相应的类对应, 4,修改tb_user类,创建一个set类型的字段emailes,生成它的set和get方法 5,写测试类,对以上内容进行测试。 二、...