-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathexample_weblog.xml
353 lines (278 loc) · 9.08 KB
/
example_weblog.xml
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<chapter id="example-weblog">
<title>Example: Weblog Application</title>
<sect1 id="example-weblog-classes">
<title>Persistent Classes</title>
<para>
The persistent classes represent a weblog, and an item posted
in a weblog. They are to be modelled as a standard parent/child
relationship, but we will use an ordered bag, instead of a set.
</para>
<programlisting><![CDATA[using System;
using System.Collections.Generic;
namespace Eg
{
public class Blog
{
public virtual long Id { get; set;}
public virtual IList<BlogItem> Items { get; set;}
public virtual string Name { get; set;}
}
}]]></programlisting>
<programlisting><![CDATA[using System;
namespace Eg
{
public class BlogItem
{
public virtual Blog Blog { get; set;}
public virtual DateTime DateTime { get; set;}
public virtual long Id { get; set;}
public virtual string Text { get; set;}
public virtual string Title { get; set;}
}
}]]></programlisting>
</sect1>
<sect1 id="example-weblog-mappings">
<title>NHibernate Mappings</title>
<para>
The XML mappings should now be quite straightforward.
</para>
<programlisting><![CDATA[<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Eg" namespace="Eg">
<class
name="Blog"
table="BLOGS"
lazy="true">
<id
name="Id"
column="BLOG_ID">
<generator class="native"/>
</id>
<property
name="Name"
column="NAME"
not-null="true"
unique="true"/>
<bag
name="Items"
inverse="true"
lazy="true"
order-by="DATE_TIME"
cascade="all">
<key column="BLOG_ID"/>
<one-to-many class="BlogItem"/>
</bag>
</class>
</hibernate-mapping>]]></programlisting>
<programlisting><![CDATA[<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Eg" namespace="Eg">
<class
name="BlogItem"
table="BLOG_ITEMS"
dynamic-update="true">
<id
name="Id"
column="BLOG_ITEM_ID">
<generator class="native"/>
</id>
<property
name="Title"
column="TITLE"
not-null="true"/>
<property
name="Text"
column="TEXT"
not-null="true"/>
<property
name="DateTime"
column="DATE_TIME"
not-null="true"/>
<many-to-one
name="Blog"
column="BLOG_ID"
not-null="true"/>
</class>
</hibernate-mapping>]]></programlisting>
</sect1>
<sect1 id="example-weblog-code">
<title>NHibernate Code</title>
<para>
The following class demonstrates some of the kinds of things
we can do with these classes, using NHibernate.
</para>
<programlisting><![CDATA[using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace Eg
{
public class BlogMain
{
private ISessionFactory _sessions;
public void Configure()
{
_sessions = new Configuration().Configure()
.BuildSessionFactory();
}
public void ExportTables()
{
var cfg = new Configuration().Configure();
new SchemaExport(cfg).Create(true, true);
}
public Blog CreateBlog(string name)
{
var blog = new Blog
{
Name = name,
Items = new List<BlogItem>()
};
using (var session = _sessions.OpenSession())
using (var tx = session.BeginTransaction())
{
session.Save(blog);
tx.Commit();
}
return blog;
}
public BlogItem CreateBlogItem(Blog blog, string title, string text)
{
var item = new BlogItem
{
Title = title,
Text = text,
Blog = blog,
DateTime = DateTime.Now
};
blog.Items.Add(item);
using (var session = _sessions.OpenSession())
using (var tx = session.BeginTransaction())
{
session.Update(blog);
tx.Commit();
}
return item;
}
public BlogItem CreateBlogItem(long blogId, string title, string text)
{
var item = new BlogItem
{
Title = title,
Text = text,
DateTime = DateTime.Now
};
using (var session = _sessions.OpenSession())
using (var tx = session.BeginTransaction())
{
var blog = session.Load<Blog>(blogId);
item.Blog = blog;
blog.Items.Add(item);
tx.Commit();
}
return item;
}
public void UpdateBlogItem(BlogItem item, string text)
{
item.Text = text;
using (var session = _sessions.OpenSession())
using (var tx = session.BeginTransaction())
{
session.Update(item);
tx.Commit();
}
}
public void UpdateBlogItem(long itemId, string text)
{
using (var session = _sessions.OpenSession())
using (var tx = session.BeginTransaction())
{
var item = session.Load<BlogItem>(itemId);
item.Text = text;
tx.Commit();
}
}
public IList<object[]> ListAllBlogNamesAndItemCounts(int max)
{
IList<object[]> result;
using (var session = _sessions.OpenSession())
using (var tx = session.BeginTransaction())
{
var q = session.CreateQuery(
"select blog.id, blog.Name, count(blogItem) " +
"from Blog as blog " +
"left outer join blog.Items as blogItem " +
"group by blog.Name, blog.id " +
"order by max(blogItem.DateTime)"
);
q.SetMaxResults(max);
result = q.List<object[]>();
tx.Commit();
}
return result;
}
public Blog GetBlogAndAllItems(long blogId)
{
Blog blog = null;
using (var session = _sessions.OpenSession())
using (var tx = session.BeginTransaction())
{
var q = session.CreateQuery(
"from Blog as blog " +
"left outer join fetch blog.Items " +
"where blog.id = :blogId"
);
q.SetParameter("blogId", blogId);
blog = q.UniqueResult<Blog>();
tx.Commit();
}
return blog;
}
public IList<object[]> ListBlogsAndRecentItems()
{
IList<object[]> result = null;
using (var session = _sessions.OpenSession())
using (var tx = session.BeginTransaction())
{
var q = session.CreateQuery(
"from Blog as blog " +
"inner join blog.Items as blogItem " +
"where blogItem.DateTime > :minDate"
);
var date = DateTime.Now.AddMonths(-1);
q.SetDateTime("minDate", date);
result = q.List<object[]>();
tx.Commit();
}
return result;
}
}
}]]></programlisting>
<para>
It requires some configuration settings in <literal>web.config</literal>, such as:
</para>
<programlisting><![CDATA[<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Add this element -->
<configSections>
<section
name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<!-- Add this element -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
<property name="connection.connection_string">
Server=localhost\SQLEXPRESS;initial catalog=Eg;Integrated Security=True
</property>
<mapping assembly="Eg" />
</session-factory>
</hibernate-configuration>
<!-- Leave the other sections unchanged -->
<system.web>
...
</system.web>
</configuration>]]></programlisting>
</sect1>
</chapter>