Hibernate 一对多双向映射 代码
来源:互联网 发布:java字典表 编辑:程序博客网 时间:2024/05/17 06:14
在下多于一对一还好控制,对于一对多和多对多方面配置经常出错,作个代码备份,代码地址:http://www.itneng.com/thread-3693-1-1.html
标签:Hibernate
[1].[文件] HibernateOneToManyBidirectional.part1.rar ~ 4MB 下载(11) 跳至 [1] [2] [3]
文件不存在或者代码语言不存在
[2].[文件] HibernateOneToManyBidirectional.part2.rar ~ 334KB 下载(10) 跳至 [1] [2] [3]
文件不存在或者代码语言不存在
[3].[代码] [Java]代码 跳至 [1] [2] [3]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import
java.io.Serializable;
import
java.util.*;
import
org.hibernate.*;
import
org.hibernate.cfg.*;
import
org.hibernate.criterion.*;
import
org.hibernate.event.*;
import
org.hibernate.event.def.*;
public
class
Main {
public
static
void
main(String[] args)
throws
Exception {
HibernateUtil.setup(
"create table grouptable (id int,name varchar);"
);
HibernateUtil.setup(
"create table story (id int,info varchar,idx int,parent_id int);"
);
Session session = HibernateUtil.currentSession();
Group sp =
new
Group(
"Group Name"
);
HashSet set =
new
HashSet();
Story s =
new
Story(
"A Story"
);
s.setParent(sp);
set.add(s);
sp.setStories(set);
Transaction transaction =
null
;
try
{
transaction = session.beginTransaction();
session.save(sp);
transaction.commit();
}
catch
(Exception e) {
if
(transaction !=
null
) {
transaction.rollback();
throw
e;
}
}
finally
{
session.close();
}
HibernateUtil.checkData(
"select * from grouptable"
);
HibernateUtil.checkData(
"select * from story"
);
}
}
/////////////////////////////////////////////////////////////////////////
import
java.util.*;
public
class
Group {
private
int
id;
private
String name;
private
Set stories;
public
Group(){
}
public
Group(String name) {
this
.name = name;
}
public
void
setId(
int
i) {
id = i;
}
public
int
getId() {
return
id;
}
public
void
setName(String n) {
name = n;
}
public
String getName() {
return
name;
}
public
void
setStories(Set l) {
stories = l;
}
public
Set getStories() {
return
stories;
}
}
/////////////////////////////////////////////////////////////////////////
import
java.util.*;
public
class
Story {
private
int
id;
private
String info;
private
Group parent;
public
void
setParent(Group g) {
parent = g;
}
public
Group getParent() {
return
parent;
}
public
Story(){
}
public
Story(String info) {
this
.info = info;
}
public
void
setId(
int
i) {
id = i;
}
public
int
getId() {
return
id;
}
public
void
setInfo(String n) {
info = n;
}
public
String getInfo() {
return
info;
}
}
/////////////////////////////////////////////////////////////////////////
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE hibernate-mapping
PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<hibernate-mapping>
<
class
name=
"Group"
table=
"grouptable"
>
<id name=
"id"
unsaved-value=
"0"
>
<generator
class
=
"increment"
/>
</id>
<set name=
"stories"
cascade=
"all"
inverse=
"true"
>
<key column=
"parent_id"
/>
<one-to-many
class
=
"Story"
/>
</set>
<property name=
"name"
type=
"string"
/>
</
class
>
<
class
name=
"Story"
table=
"story"
>
<id name=
"id"
unsaved-value=
"0"
>
<generator
class
=
"increment"
/>
</id>
<property name=
"info"
/>
<many-to-one name=
"parent"
column=
"parent_id"
not-
null
=
"true"
/>
</
class
>
</hibernate-mapping>
/////////////////////////////////////////////////////////////////////////
<?xml version=
'1.0'
encoding=
'utf-8'
?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name=
"connection.driver_class"
>org.hsqldb.jdbcDriver</property>
<property name=
"connection.url"
>jdbc:hsqldb:data/tutorial</property>
<property name=
"connection.username"
>sa</property>
<property name=
"connection.password"
></property>
<!-- JDBC connection pool (use the built-in) -->
<property name=
"connection.pool_size"
>
1
</property>
<!-- SQL dialect -->
<property name=
"dialect"
>org.hibernate.dialect.HSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name=
"show_sql"
>
true
</property>
<!-- Mapping files -->
<mapping resource=
"Group.hbm.xml"
/>
</session-factory>
</hibernate-configuration>
/////////////////////////////////////////////////////////////////////////
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.Statement;
import
java.sql.ResultSet;
import
java.sql.ResultSetMetaData;
import
org.hibernate.HibernateException;
import
org.hibernate.Session;
import
org.hibernate.SessionFactory;
import
org.hibernate.cfg.Configuration;
public
class
HibernateUtil {
public
static
final
SessionFactory sessionFactory;
static
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory =
new
Configuration().configure().buildSessionFactory();
}
catch
(Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println(
"Initial SessionFactory creation failed."
+ ex);
throw
new
ExceptionInInitializerError(ex);
}
}
public
static
final
ThreadLocal session =
new
ThreadLocal();
public
static
Session currentSession()
throws
HibernateException {
Session s = (Session) session.get();
// 如果本来没有,打开一个新的Session
if
(s ==
null
) {
s = sessionFactory.openSession();
// 保存
session.set(s);
}
return
s;
}
public
static
void
closeSession()
throws
HibernateException {
Session s = (Session) session.get();
if
(s !=
null
)
s.close();
session.set(
null
);
}
static
Connection conn;
static
Statement st;
public
static
void
setup(String sql) {
try
{
// 加载 JDBC 驱动.
Class.forName(
"org.hsqldb.jdbcDriver"
);
System.out.println(
"Driver Loaded."
);
//建立到数据库的连接
String url =
"jdbc:hsqldb:data/tutorial"
;
conn = DriverManager.getConnection(url,
"sa"
,
""
);
System.out.println(
"Got Connection."
);
st = conn.createStatement();
st.executeUpdate(sql);
}
catch
(Exception e) {
System.err.println(
"Got an exception! "
);
e.printStackTrace();
System.exit(
0
);
}
}
public
static
void
checkData(String sql) {
try
{
HibernateUtil.outputResultSet(st
.executeQuery(sql));
// conn.close();
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
static
void
outputResultSet(ResultSet rs)
throws
Exception{
ResultSetMetaData metadata = rs.getMetaData();
int
numcols = metadata.getColumnCount();
String[] labels =
new
String[numcols];
int
[] colwidths =
new
int
[numcols];
int
[] colpos =
new
int
[numcols];
int
linewidth;
for
(
int
i =
0
; i < numcols; i++) {
labels[i] = metadata.getColumnLabel(i +
1
);
System.out.print(labels[i]+
" "
);
}
System.out.println(
"------------------------"
);
while
(rs.next()) {
for
(
int
i =
0
; i < numcols; i++) {
Object value = rs.getObject(i +
1
);
if
(value ==
null
){
System.out.print(
" "
);
}
else
{
System.out.print(value.toString().trim()+
" "
);
}
}
System.out.println(
" "
);
}
}
}
0 0
- Hibernate 一对多双向映射 代码
- Hibernate单向一对多映射和双向一对多映射
- Hibernate一对多关联双向映射
- hibernate映射一对多双向关联关系
- hibernate学习笔记【一对多双向映射】
- Hibernate一对多双向关联映射
- Hibernate关系映射--双向一对多@OnToMany
- hibernate一对多单向和双向映射
- hibernate实现双向一对多的映射
- hibernate一对多双向映射(set)
- Hibernate双向的一对多关系映射
- Hibernate一对多双向关联映射
- Hibernate学习之双向一对多映射
- hibernate映射总结详解: 映射一对多双向关联关系
- Hibernate 关联映射之---- 一对多双向映射
- Hibernate一对多关系映射(双向映射)
- 双向一对多映射
- 双向一对多映射
- ASP SCRIPT: 计数器(使用GrapShot组件)
- 学以致用 驳“ASP低能论”
- 如何设置规定的用户名注册
- PHP 6 即将发布 更强大的本地化及Web 2.0支持
- poj 2109
- Hibernate 一对多双向映射 代码
- python和rrdtool配合画图
- hibernate查询SQL语句返回自定义实体对象
- sqlserver 2008 安装完后无法为该请求检索数据
- 在groovy中如何使用Hibernate
- node.js httpserver 无法访问
- 给生活来一次傅立叶转换
- [LeetCode] Partition List 分割单链表
- mos的开关特性电压 关系