做项目的过程中,对数据表建立一对多的关联关系时,以前都是用MyEclipse自动生成配置文件,数据集合都是使用默认生成的Set, Set是无序的,保存数据没有按顺序存储,而我需要把数据按保存时的顺序读取出来时,数据的顺序全乱了,看来想偷懒都不行,呵呵,修改hbm.xml配置文件,把Set换成list.
数据表语句,
create table student(
id int primary key,
name varchar(30),
age int,
sex varchar(8));
create table course(
id int primary key,
cid int ,
cname varchar(20),
idx int,
student_id int,
foreign key course(student_id) references student(id)
);映射文件与持久类
Student.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 Mapping file autogenerated by MyEclipse Persistence Tools
6 -->
7 <hibernate-mapping>
8 <class name="com.dragon.datamodel.Student" table="student" >
9 <id name="id" type="java.lang.Integer">
10 <column name="id" />
11 <generator class="increment" />
12 </id>
13 <property name="name" type="java.lang.String">
14 <column name="name" length="30" not-null="true" />
15 </property>
16 <property name="age" type="java.lang.Integer">
17 <column name="age" />
18 </property>
19 <property name="sex" type="java.lang.String">
20 <column name="sex" length="8" />
21 </property>
22
23 <list name="courses" table="course" cascade="all">
24 <key column="student_id" ></key>
25 <!-- idx 字段用来记录 保存的数据的顺序 -->
26 <index column="idx" type="java.lang.Integer"></index>
27 <one-to-many class="com.dragon.datamodel.Course" />
28 </list>
29
30 </class>
31 </hibernate-mapping>
32 Student.java
1 package com.dragon.datamodel;
2
3
4
5 import java.util.ArrayList;
6
7 import java.util.List;
8
9
10
11 /**
12
13 * Student generated by MyEclipse Persistence Tools
14
15 */
16
17
18
19 public class Student implements java.io.Serializable {
20
21
22
23 // Fields
24
25
26
27 /**
28
29 *
30
31 */
32
33 private static final long serialVersionUID = 1L;
34
35
36
37 private Integer id;
38
39
40
41 private String name;
42
43
44
45 private Integer age;
46
47
48
49 private String sex;
50
51
52
53 private List courses = new ArrayList();
54
55
56
57 // Constructors
58
59
60
61 /** default constructor */
62
63 public Student() {
64
65 }
66
67
68
69 /** minimal constructor */
70
71 public Student(String name) {
72
73 this.name = name;
74
75 }
76
77
78
79 /** full constructor */
80
81 public Student(String name, Integer age, String sex) {
82
83 this.name = name;
84
85 this.age = age;
86
87 this.sex = sex;
88
89 }
90
91
92
93 // Property accessors
94
95
96
97 public Integer getId() {
98
99 return this.id;
100
101 }
102
103
104
105 public void setId(Integer id) {
106
107 this.id = id;
108
109 }
110
111
112
113 public String getName() {
114
115 return this.name;
116
117 }
118
119
120
121 public void setName(String name) {
122
123 this.name = name;
124
125 }
126
127
128
129 public Integer getAge() {
130
131 return this.age;
132
133 }
134
135
136
137 public void setAge(Integer age) {
138
139 this.age = age;
140
141 }
142
143
144
145 public String getSex() {
146
147 return this.sex;
148
149 }
150
151
152
153 public void setSex(String sex) {
154
155 this.sex = sex;
156
157 }
158
159
160
161 public List getCourses() {
162
163 return courses;
164
165 }
166
167
168
169 public void setCourses(List courses) {
170
171 this.courses = courses;
172
173 }
174
175
176
177 public void addCourses(Course course){
178
179 this.courses.add(course);
180
181 }
182
183
184
185
186
187 }Course.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 Mapping file autogenerated by MyEclipse Persistence Tools
6 -->
7 <hibernate-mapping>
8 <class name="com.dragon.datamodel.Course" table="course" >
9 <id name="id" type="java.lang.Integer">
10 <column name="id" />
11 <generator class="increment" />
12 </id>
13 <property name="cid" type="java.lang.Integer">
14 <column name="cid" />
15 </property>
16 <property name="cname" type="java.lang.String">
17 <column name="cname" length="20" />
18 </property>
19
20 </class>
21 </hibernate-mapping>
22 Course.java
1 package com.dragon.datamodel;
2
3 /**
4 * Course generated by MyEclipse Persistence Tools
5 */
6
7 public class Course implements java.io.Serializable {
8
9 // Fields
10
11 /**
12 *
13 */
14 private static final long serialVersionUID = 1L;
15
16 private Integer id;
17
18 private Integer cid;
19
20 private String cname;
21
22 // private Integer studentId;
23
24 // Constructors
25
26
27 /** default constructor */
28 public Course() {
29 }
30
31 /** full constructor */
32 public Course(Integer cid, String cname, Integer studentId) {
33 this.cid = cid;
34 this.cname = cname;
35 // this.studentId = studentId;
36 }
37
38 // Property accessors
39
40 public Integer getId() {
41 return this.id;
42 }
43
44 public void setId(Integer id) {
45 this.id = id;
46 }
47
48 public Integer getCid() {
49 return this.cid;
50 }
51
52 public void setCid(Integer cid) {
53 this.cid = cid;
54 }
55
56 public String getCname() {
57 return this.cname;
58 }
59
60 public void setCname(String cname) {
61 this.cname = cname;
62 }
63
64 // public Integer getStudentId() {
65 // return this.studentId;
66 // }
67 //
68 // public void setStudentId(Integer studentId) {
69 // this.studentId = studentId;
70 // }
71
72 }测试类
TestList.java
1 /**
2 *
3 */
4 package com.test;
5
6 import java.util.List;
7
8 import org.hibernate.Session;
9 import org.hibernate.Transaction;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13
14 import com.dragon.datamodel.Course;
15 import com.dragon.datamodel.HibernateSessionFactory;
16 import com.dragon.datamodel.Student;
17
18 /**
19 * @author dragon
20 *
21 */
22 public class TestList {
23
24 private Session session;
25 private Transaction tx;
26
27 @Before
28 public void setup(){
29 session = HibernateSessionFactory.getSession();
30 tx = session.beginTransaction();
31 }
32
33 // @Test
34 public void saveStundetInfo(){
35 Student stu = new Student();
36 stu.setName("dragon");
37 stu.setAge(23);
38 stu.setSex("男");
39 String k ;
40
41 Course c1 = new Course();
42 c1.setCid(3);
43 c1.setCname("j2ee 应用");
44
45 Course c2 = new Course();
46 c2.setCid(21);
47 c2.setCname("C# 基础");
48
49 Course c3 = new Course();
50 c3.setCid(22);
51 c3.setCname("软件工程");
52
53 stu.addCourses(c1);
54 stu.addCourses(c2);
55 stu.addCourses(c3);
56
57 session.save(stu);
58
59
60
61 }
62
63
64 @Test
65 public void LoadStudentInfo(){
66 List list = session.createQuery("from Student").list();
67
68 for (int i =0; i < list.size(); i++){
69 Student stu = (Student) list.get(i);
70 System.out.println(stu.getName());
71 List courses = stu.getCourses();
72 for (int j = 0; j < courses.size(); j++){
73 Course cors = (Course) courses.get(j);
74 System.out.print(" "+cors.getCname());
75 }
76 System.out.println();
77 }
78
79 }
80
81
82
83 @After
84 public void closeSession(){
85 tx.commit();
86 session.close();
87 }
88
89 }
90
posted on 2007-07-04 18:17
javadragon 阅读(7809)
评论(3) 编辑 收藏