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. Web应用主动侦测工具Skipfish

    Web应用主动侦测工具Skipfish   Skipfish是Kali Linux附带的一个主动Web应用侦测工具.该工具会首先尽可能获取所有网站路径,进行访问,然后根据返回的内容,检测是否存在漏洞. ...

  2. [Codeforces #190] Tutorial

    Link: Codeforces #190 传送门 A: 明显答案为$n+m-1$且能构造出来 #include <bits/stdc++.h> using namespace std; ...

  3. python基础之递归,匿名,内置函数

    递归函数: 什么是递归函数? 函数递归调用:在调用一个函数的过程中,又直接或间接地调用了该函数本身. 递归必须要有两个明确的阶段: ①递推:一层一层递归调用下去,强调:每进入下一层问题规模减少 ②回溯 ...

  4. maven打包出现找不到java.lang包的问题

    修改pom文件中的bootclasspth路径为${java.home}/lib/rt.jar;${java.home}/lib/jce.jar windows为分号 linux为冒号

  5. 2016.4.3 动态规划NOI专练 王老师讲课整理

    1.6049:买书 总时间限制:  1000ms 内存限制:  65536kB 描述 小明手里有n元钱全部用来买书,书的价格为10元,20元,50元,100元. 问小明有多少种买书方案?(每种书可购买 ...

  6. [转]Android的ADT与SDK的区别

      adt只是一个eclipse的插件,里面可以设置sdk路径     SDK(Software Development Kit): 一般是一些被软件工程师用于为特定的软件包.软件框架.硬件平台.操作 ...

  7. 《windows内核安全与驱动开发》ctrl2cap中的ObReferenceObjectByName疑问

    国内有关于windows内核驱动这块的书籍实在是甚少,不过好在<windows内核安全与驱动开发>这本书还算不错(内容方面),但是不得不说这本书在许多地方存在着一些细节上的问题.比如我今天 ...

  8. 如何在sublime3项目设置中设置python模块的搜索路径?ImportError: No module named *的解决办法

    问题:之前使用pycharm,用的挺溜,但是电脑配置实在不争气,pycharm启动久了,耗去大量内存,导致运行起来越来越慢,于是转向使用sublime text. 把项目从pycharm切换到subl ...

  9. [转]Using the Microsoft Connector for Oracle by Attunity with SQL Server 2008 Integration Services

    本文转自:http://technet.microsoft.com/en-us/library/ee470675(v=sql.100).aspx SQL Server Technical Articl ...

  10. python scikit-learn选择正确估算器

    下图摘自官方文档 链接 http://scikit-learn.org/stable/tutorial/machine_learning_map/index.html