标签:
team类:
1 import java.util.HashSet;
2 import java.util.Set;
3
4 public class Team
5 {
6 private String id;
7
8 private String teamName;
9
10 private Set students = new HashSet();
11
12 public String getId()
13 {
14 return id;
15 }
16
17 public void setId(String id)
18 {
19 this.id = id;
20 }
21
22 public String getTeamName()
23 {
24 return teamName;
25 }
26
27 public void setTeamName(String teamName)
28 {
29 this.teamName = teamName;
30 }
31
32 public Set getStudents()
33 {
34 return students;
35 }
36
37 public void setStudents(Set students)
38 {
39 this.students = students;
40 }
41 }
映射文件Team.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
5 <hibernate-mapping>
6
7 <class name="com.cdtax.hibernate.Team" table="team">
8
9 <id name="id" column="id" type="string">
10 <generator class="uuid">
11 </generator>
12 </id>
13
14 <property name="teamName" column="teamname" type="string"></property>
15
16 <set name="students" table="student">
17 <key column="team_id"></key>
18 <element column="name" type="string"></element>
19 </set>
20 </class>
21
22 </hibernate-mapping>
使用set标签,一般要对应另外一张表,对于set元素是简单类型的,使用element子标签。
表的创建:
1 create table student (team_id varchar(255) not null, name varchar(255))
2 create table team (id varchar(255) not null, teamname varchar(255), primary key (id))
3 alter table student add index FK8FFE823BB04F9E7 (team_id), add constraint FK8FFE823BB04F9E7 foreign key (team_id) references team (id)
测试类:
1 import java.util.Map;
2
3 import org.hibernate.Session;
4 import org.hibernate.SessionFactory;
5 import org.hibernate.Transaction;
6 import org.hibernate.cfg.Configuration;
7
8 public class HibernateTest
9 {
10 private static SessionFactory sessionFactory;
11
12 static
13 {
14 try
15 {
16 sessionFactory = new Configuration().configure().buildSessionFactory();
17 }
18 catch(Exception ex)
19 {
20 ex.printStackTrace();
21 }
22 }
23
24 public static void main(String[] args)
25 {
26
27 Session session = sessionFactory.openSession();
28 Transaction tx = null;
29
30 try
31 {
32 tx = session.beginTransaction();
33
34 Team team = new Team();
35 team.setTeamName("team");
36
37 team.getStudents().add("zhangsan");
38 team.getStudents().add("lisi");
39 team.getStudents().add("wangwu");
40
41 session.save(team);
42
43 tx.commit();
44 }
45 catch(Exception ex)
46 {
47 if(null != tx)
48 {
49 tx.rollback();
50 }
51 ex.printStackTrace();
52 }
53 finally
54 {
55 session.close();
56 }
57 }
58 }
运行结果:
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映射的则是实体类型,指的是无法映射到表的某个字段,而是要映射到整张表的类型。
举例:
1 import java.util.ArrayList;
2 import java.util.List;
3
4 public class Team
5 {
6 private String id;
7
8 private String teamName;
9
10 private List students = new ArrayList();
11
12 public String getId()
13 {
14 return id;
15 }
16
17 public void setId(String id)
18 {
19 this.id = id;
20 }
21
22 public String getTeamName()
23 {
24 return teamName;
25 }
26
27 public void setTeamName(String teamName)
28 {
29 this.teamName = teamName;
30 }
31
32 public List getStudents()
33 {
34 return students;
35 }
36
37 public void setStudents(List students)
38 {
39 this.students = students;
40 }
41
42 }
1 public class Student
2 {
3 private String id;
4
5 private String cardId;
6
7 private String name;
8
9 private int age;
10
11 private Team team;
12
13 public String getId()
14 {
15 return id;
16 }
17
18 public void setId(String id)
19 {
20 this.id = id;
21 }
22
23 public String getCardId()
24 {
25 return cardId;
26 }
27
28 public void setCardId(String cardId)
29 {
30 this.cardId = cardId;
31 }
32
33 public String getName()
34 {
35 return name;
36 }
37
38 public void setName(String name)
39 {
40 this.name = name;
41 }
42
43 public int getAge()
44 {
45 return age;
46 }
47
48 public void setAge(int age)
49 {
50 this.age = age;
51 }
52
53 public Team getTeam()
54 {
55 return team;
56 }
57
58 public void setTeam(Team team)
59 {
60 this.team = team;
61 }
62
63 }
对应的映射文件:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
5 <hibernate-mapping>
6
7 <class name="com.cdtax.hibernate.Team" table="team">
8
9 <id name="id" column="id" type="string">
10 <generator class="uuid">
11 </generator>
12 </id>
13
14 <property name="teamName" column="teamname" type="string"></property>
15
16 <list name="students" table="student" cascade="all">
17 <key column="team_id"></key>
18 <index column="index_"></index>
19 <one-to-many class="com.cdtax.hibernate.Student"/>
20 </list>
21 </class>
22
23 </hibernate-mapping>
对于Team,多了一个list标签,其中的子标签index来指出数据库表中的一列来维护list的顺序,因为list是有序的,而数据库中表是没有顺序的,只能用一个字段来维护。
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
5 <hibernate-mapping>
6
7 <class name="com.cdtax.hibernate.Student" table="student">
8
9 <id name="id" column="id" type="string">
10 <generator class="uuid">
11 </generator>
12 </id>
13 <property name="cardId" column="card_id" type="string"></property>
14 <property name="name" column="name" type="string"></property>
15 <property name="age" column="age" type="integer"></property>
16
17 <many-to-one name="team" column="team_id" class="com.cdtax.hibernate.Team" cascade="none" fetch="join"></many-to-one>
18 </class>
19 </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_这一字段。
测试类:
1 import java.util.Map;
2
3 import org.hibernate.Session;
4 import org.hibernate.SessionFactory;
5 import org.hibernate.Transaction;
6 import org.hibernate.cfg.Configuration;
7
8 public class HibernateTest
9 {
10 private static SessionFactory sessionFactory;
11
12 static
13 {
14 try
15 {
16 sessionFactory = new Configuration().configure().buildSessionFactory();
17 }
18 catch(Exception ex)
19 {
20 ex.printStackTrace();
21 }
22 }
23
24 public static void main(String[] args)
25 {
26
27 Session session = sessionFactory.openSession();
28 Transaction tx = null;
29
30 try
31 {
32 tx = session.beginTransaction();
33
34 Team team = new Team();
35 team.setTeamName("team1");
36
37 Team team2= new Team();
38 team2.setTeamName("team2");
39
40 Student s1 = new Student();
41 Student s2 = new Student();
42 Student s3 = new Student();
43 Student s4 = new Student();
44 Student s5 = new Student();
45 Student s6 = new Student();
46
47 s1.setName("zhangsan");
48 s2.setName("lisi");
49 s3.setName("wangwu");
50 s4.setName("zhaoliu");
51 s5.setName("5zhaoliu");
52 s6.setName("6zhaoliu");
53
54 team.getStudents().add(s1);
55 team.getStudents().add(s2);
56
57 team2.getStudents().add(s3);
58 team2.getStudents().add(s4);
59 team2.getStudents().add(s5);
60 team2.getStudents().add(s6);
61
62 session.save(team);
63 session.save(team2);
64
65 tx.commit();
66 }
67 catch(Exception ex)
68 {
69 if(null != tx)
70 {
71 tx.rollback();
72 }
73 ex.printStackTrace();
74 }
75 finally
76 {
77 session.close();
78 }
79 }
80 }
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=?
标签:
原文地址:http://www.cnblogs.com/caoyc/p/5597122.html