1、对于set类型,如果集合中的元素是简单地类型,如字符串型,set使用另外一种映射方式:

team类:

 import java.util.HashSet;
import java.util.Set; public class Team
{
private String id; private String teamName; private Set students = new HashSet(); public String getId()
{
return id;
} public void setId(String id)
{
this.id = id;
} public String getTeamName()
{
return teamName;
} public void setTeamName(String teamName)
{
this.teamName = teamName;
} public Set getStudents()
{
return students;
} public void setStudents(Set students)
{
this.students = students;
}
}

映射文件Team.hbm.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.cdtax.hibernate.Team" table="team"> <id name="id" column="id" type="string">
<generator class="uuid">
</generator>
</id> <property name="teamName" column="teamname" type="string"></property> <set name="students" table="student">
<key column="team_id"></key>
<element column="name" type="string"></element>
</set>
</class> </hibernate-mapping>

使用set标签,一般要对应另外一张表,对于set元素是简单类型的,使用element子标签。

表的创建:

 create table student (team_id varchar(255) not null, name varchar(255))
create table team (id varchar(255) not null, teamname varchar(255), primary key (id))
alter table student add index FK8FFE823BB04F9E7 (team_id), add constraint FK8FFE823BB04F9E7 foreign key (team_id) references team (id)

测试类:

 import java.util.Map;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; public class HibernateTest
{
private static SessionFactory sessionFactory; static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Exception ex)
{
ex.printStackTrace();
}
} public static void main(String[] args)
{ Session session = sessionFactory.openSession();
Transaction tx = null; try
{
tx = session.beginTransaction(); Team team = new Team();
team.setTeamName("team"); team.getStudents().add("zhangsan");
team.getStudents().add("lisi");
team.getStudents().add("wangwu"); session.save(team); tx.commit();
}
catch(Exception ex)
{
if(null != tx)
{
tx.rollback();
}
ex.printStackTrace();
}
finally
{
session.close();
}
}
}

运行结果:

hibernate: insert into team (teamname, id) values (?, ?)
Hibernate: insert into student (team_id, name) values (?, ?)
Hibernate: insert into student (team_id, name) values (?, ?)
Hibernate: insert into student (team_id, name) values (?, ?)

map与set标签中的element子标签映射的是原子类型(String,date,int,long。。。),即能够直接映射到数据库表字段上的类型,而one-to-many映射的则是实体类型,指的是无法映射到表的某个字段,而是要映射到整张表的类型。

2、List的映射,list元素可以重复,而且是有顺序的。

举例:

 import java.util.ArrayList;
import java.util.List; public class Team
{
private String id; private String teamName; private List students = new ArrayList(); public String getId()
{
return id;
} public void setId(String id)
{
this.id = id;
} public String getTeamName()
{
return teamName;
} public void setTeamName(String teamName)
{
this.teamName = teamName;
} public List getStudents()
{
return students;
} public void setStudents(List students)
{
this.students = students;
} }
 public class Student
{
private String id; private String cardId; private String name; private int age; private Team team; public String getId()
{
return id;
} public void setId(String id)
{
this.id = id;
} public String getCardId()
{
return cardId;
} public void setCardId(String cardId)
{
this.cardId = cardId;
} public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
} public int getAge()
{
return age;
} public void setAge(int age)
{
this.age = age;
} public Team getTeam()
{
return team;
} public void setTeam(Team team)
{
this.team = team;
} }

对应的映射文件:

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.cdtax.hibernate.Team" table="team"> <id name="id" column="id" type="string">
<generator class="uuid">
</generator>
</id> <property name="teamName" column="teamname" type="string"></property> <list name="students" table="student" cascade="all">
<key column="team_id"></key>
<index column="index_"></index>
<one-to-many class="com.cdtax.hibernate.Student"/>
</list>
</class> </hibernate-mapping>

对于Team,多了一个list标签,其中的子标签index来指出数据库表中的一列来维护list的顺序,因为list是有序的,而数据库中表是没有顺序的,只能用一个字段来维护。

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.cdtax.hibernate.Student" table="student"> <id name="id" column="id" type="string">
<generator class="uuid">
</generator>
</id>
<property name="cardId" column="card_id" type="string"></property>
<property name="name" column="name" type="string"></property>
<property name="age" column="age" type="integer"></property> <many-to-one name="team" column="team_id" class="com.cdtax.hibernate.Team" cascade="none" fetch="join"></many-to-one>
</class>
</hibernate-mapping>

产生表的语句:

create table student (id varchar(255) not null, card_id varchar(255), name varchar(255), age integer, team_id varchar(255), index_ integer, primary key (id))
create table team (id varchar(255) not null, teamname varchar(255), primary key (id))

重点关注index_这一字段。

测试类:

 import java.util.Map;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; public class HibernateTest
{
private static SessionFactory sessionFactory; static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Exception ex)
{
ex.printStackTrace();
}
} public static void main(String[] args)
{ Session session = sessionFactory.openSession();
Transaction tx = null; try
{
tx = session.beginTransaction(); Team team = new Team();
team.setTeamName("team1"); Team team2= new Team();
team2.setTeamName("team2"); Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
Student s4 = new Student();
Student s5 = new Student();
Student s6 = new Student(); s1.setName("zhangsan");
s2.setName("lisi");
s3.setName("wangwu");
s4.setName("zhaoliu");
s5.setName("5zhaoliu");
s6.setName("6zhaoliu"); team.getStudents().add(s1);
team.getStudents().add(s2); team2.getStudents().add(s3);
team2.getStudents().add(s4);
team2.getStudents().add(s5);
team2.getStudents().add(s6); session.save(team);
session.save(team2); tx.commit();
}
catch(Exception ex)
{
if(null != tx)
{
tx.rollback();
}
ex.printStackTrace();
}
finally
{
session.close();
}
}
}

Hibernate: insert into team (teamname, id) values (?, ?)

Hibernate: insert into student (card_id, name, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into student (card_id, name, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into team (teamname, id) values (?, ?)
Hibernate: insert into student (card_id, name, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into student (card_id, name, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into student (card_id, name, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into student (card_id, name, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: update student set team_id=?, index_=? where id=?
Hibernate: update student set team_id=?, index_=? where id=?
Hibernate: update student set team_id=?, index_=? where id=?
Hibernate: update student set team_id=?, index_=? where id=?
Hibernate: update student set team_id=?, index_=? where id=?
Hibernate: update student set team_id=?, index_=? where id=?

Hibernate映射set与List的更多相关文章

  1. hibernate映射文件

    Hibernate的持久化类和关系数据库之间的映射通常是用一个XML文档来定义的.该文档通过一系列XML元素的配置,来将持久化类与数据库表之间建立起一一映射.这意味着映射文档是按照持久化类的定义来创建 ...

  2. Hibernate映射文件如何配置触发器

    Hibernate映射文件之触发器生成(generated属性.database-object元素) (2013-02-27 12:28:49) 转载▼ 标签: it 分类: JAVA学习笔记 这里分 ...

  3. Spring集成Hibernate映射文件的4种方式

    概要: 在Spring的applicationContext.xml中集成Hibernate映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少 ...

  4. hibernate映射的 关联关系:有 一对多关联关系,一对一关联关系,多对多关联关系,继承关系

    hibernate环境配置:导包.... 单向n-1:单向 n-1 关联只需从 n 的一端可以访问 1 的一端 <many-to-one> 元素来映射组成关系: name: 设定待映射的持 ...

  5. Hibernate注解映射sequence时出现无序增长问题+hibernate 映射 oracle ID自动增长:

    Hibernate注解映射sequence时出现无序增长问题+hibernate 映射 oracle ID自动增长: 通过Hibernate注解的方式映射oracel数据库的sequence主键生成器 ...

  6. oracle ORA-01747(系统保留关键字)user.table.column, table.column 或列说明无效 hibernate映射oracle保留关键字

    1.查询系统关键 select * from v$reserved_words 确认你使用的是否为关键字: select * from v$reserved_words w where w.KEYWO ...

  7. Hibernate映射类型对照表

    Hibernate映射类型对照表 java类型  Hibernate映射类型  SQL类型 java.math.BigDecimal big_decimal numeric byte[] binary ...

  8. 02.Hibernate映射基础

    前言:Hibernate的核心功能是根据数据库到实体类的映射,自动从数据库绑定数据到实体类.使我们操作实体类(Java对象)就能对数据库进行增.删.查.改,而不用调用JDBC API使数据操作变得简单 ...

  9. hibernate Java 时间和日期类型的 Hibernate 映射

    基础知识: 在 Java 中, 代表时间和日期的类型包含: java.util.Date 和 java.util.Calendar. 此外, 在 JDBC API 中还提供了 3 个扩展了 java. ...

  10. [转]Hibernate映射的基本操作

       ++YONG原创,转载请注明http://blog.csdn.net/qjyong/article/details/1829672          Hibernate映射主要是通过对象关系映射 ...

随机推荐

  1. 排序小记【2】对 struct 的排序

    有了前面的内容,对于一般的排序已经没有问题了,但是有时候排序的要求可能会有点刁... 举个简单的例子,应该是NOIP2009的分数线划定,差不多算是一个比较高级的排序(吧). 多关键字排序(?) 我一 ...

  2. BZOJ 4589 Hard Nim(FWT+博弈论+快速幂)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4589 [题目大意] 有n堆石子,每堆都是m以内的质数,请问后手必胜的局面有几种 [题解 ...

  3. 【递推】【组合计数】UVA - 11401 - Triangle Counting

    http://blog.csdn.net/highacm/article/details/8629173 题目大意:计算从1,2,3,...,n中选出3个不同的整数,使得以它们为边长可以构成三角形的个 ...

  4. html基础知识介绍

    1 前端概要 前端三大利器 1.html  赤裸裸的人 2.css   穿上华丽的衣服 3.js    让人生动起来 2 HTML本质及在web程序中的作用 2.1 介绍 HTML 1.一套规则,浏览 ...

  5. kong结合consul

    早期版本 ./etcdctl ls / --recursive /name /name1 /name1/wyc /name1/wu /name1/chao /name1/chao/age ------ ...

  6. [转]web.xml中webAppRootKey

    web.xml中webAppRootKey ------------------------------------------------------------------------------ ...

  7. GitHub 给已存在的仓库增加开原协议LICENSE

    如果你创建仓库时没有设置开原协议LICENSE,后续可以按下面方法增加开原协议LICENSE. 1.点击Create new file,如下图 2.文件名填LICENSE.md,右边就会出现choos ...

  8. andriod 获得时间

    import java.text.SimpleDateFormat;import java.util.Date; public static String getCurrentTime() { Sim ...

  9. iOS:UIView的CALayer基本演练

    UIView的CALayer基本演练的属性和注意事项: 在UIView中创建一个按钮UIButton,然后设置UIButton的Layer属性 –圆角.边框.阴影及3D形变属性 注意: 1.在UIVi ...

  10. Oracle imp关于fromuser 和 touser的用法

    fromuser就是把当前的dmp文件中的某一个用户下的数据取出.touser就是把现在dmp文件中的数据导入到目标库的指定user下.具体命令这样.exp userid=system/manager ...