Skip to main content
summaryrefslogtreecommitdiffstats
blob: fc62098cc8aa8f215c2d3c6cfd96604770aab4de (plain) (blame)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="Apache Forrest" name="Generator">
<meta name="Forrest-version" content="0.7">
<meta name="Forrest-skin-name" content="elver4">
<style type="text/css">
          /*  */
          @import "../../skin/tigris.css";
          @import "../../skin/quirks.css";
          @import "../../skin/inst.css";
         /*   */
        </style>
<link media="print" href="../../skin/print.css" type="text/css" rel="stylesheet">
<link href="../../skin/forrest.css" type="text/css" rel="stylesheet">
<link rel="shortcut icon" href="../../">
<script type="text/javascript" src="../../skin/tigris.js"></script><script src="../../skin/menu.js" language="javascript" type="text/javascript"></script>
<title>Step 2: Create and store EMF Objects</title>
<meta content="text/css" http-equiv="Content-style-type">
</head>
<body class="composite" onload="focus()">
<div id="banner">
<table width="100%" cellpadding="8" cellspacing="0" border="0">
<tr>
<td align="left">
<div>
<a href="http://www.elver.org"><img class="logoImage" alt="Elver Store" src="../../images/ELV2.gif"></a>
</div>
<span class="alt">Elver Store</span></td><td align="center">
<div>
<a href="http://www.elver.org/"><img class="logoImage" alt="Elver" src="../../images/empty.gif"></a>
</div>
</td><td valign="top" align="right">
<div class="right" align="right" id="login">
<form target="_blank" action="http://www.google.com/search" method="get">
<select name="as_sitesearch"><option value="">Search...</option><option value="www.elver.org">The elver site</option><option value="">The web</option></select> for
		      <input size="15" name="as_q" id="query" type="text"><input name="Search" value="Go" type="submit">
</form>
</div>
</td>
</tr>
</table>
</div>
<div id="toptabs" class="tabs">
<table border="0" cellspacing="0" cellpadding="4">
<tr>
<td><a class="base-selected" href="../../index.html">Home</a></td><th><a class="base-selected" href="../../hibernate/index.html">EMF Hibernate</a></th><td><a class="base-selected" href="../../jpox/index.html">EMF JDO/JPOX</a></td><td><a class="base-selected" href="../../services/index.html">Services</a></td>
</tr>
</table>
</div>
<table width="100%" border="0" cellpadding="0" cellspacing="0" id="breadcrumbs">
<tr>
<td></td><td>
<div class="published" align="right">
<script type="text/javascript" language="JavaScript"><!--
                 document.write("Published: " + document.lastModified);
                 //  --></script>
</div>
</td>
</tr>
</table>
<table id="main" width="100%" cellpadding="4" cellspacing="0" border="0">
<tr valign="top">
<td style="padding: 0px" width="20%" id="leftcol">
<table width="100%" class="menuarea" cellspacing="0" cellpadding="0">
<tr>
<td width="6px" valign="top">
<table border="0" cellpadding="0" cellspacing="0" class="leftpagemargin">
<tr>
<td class="subborder trail">&nbsp;</td>
</tr>
</table>
</td><td class="dialog">
<div class="menu">
<div onclick="SwitchMenu('menu_1.1')" id="menu_1.1Title" class="menutitle">Home</div>
<div id="menu_1.1" class="menuitemgroup">
<div class="menuitem">
<a href="../../hibernate/index.html">Introduction</a>
</div>
<div class="menuitem">
<a href="../../hibernate/status.html">Status</a>
</div>
<div class="menuitem">
<a href="../../hibernate/features.html">Features</a>
</div>
<div class="menuitem">
<a href="../../hibernate/overview.html">Overview</a>
</div>
<div class="menuitem">
<a href="../../hibernate/changelog.html">Changelog</a>
</div>
<div class="menuitem">
<a href="../../hibernate/installation.html">Download &amp; Install</a>
</div>
<div class="menuitem">
<a href="../../hibernate/knownissues.html">Known Issues</a>
</div>
<div class="menuitem">
<a href="../../hibernate/quick_tutorial.html">Quick Start</a>
</div>
<div class="menuitem">
<a href="../../hibernate/license.html">License</a>
</div>
</div>
<div onclick="SwitchMenu('menu_selected_1.2')" id="menu_selected_1.2Title" class="menutitle">Library Tutorial</div>
<div id="menu_selected_1.2" class="selectedmenuitemgroup">
<div class="menuitem">
<a href="../../hibernate/tutorialone/tutorial1_intro.html">Introduction</a>
</div>
<div class="menuitem">
<a href="../../hibernate/tutorialone/tutorial1_1.html">Setup Environment</a>
</div>
<div class="menupage">
<div class="menupagetitle">Create and Store EMF Object</div>
</div>
<div class="menuitem">
<a href="../../hibernate/tutorialone/tutorial1_3.html">Retrieve EMF Objects</a>
</div>
<div class="menuitem">
<a href="../../hibernate/tutorialone/tutorial1_4.html">Query EMF Objects</a>
</div>
<div class="menuitem">
<a href="../../hibernate/tutorialone/tutorial1_5.html">Using EMF/JPOX Resources</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.3')" id="menu_1.3Title" class="menutitle">Library Editor Tutorial</div>
<div id="menu_1.3" class="menuitemgroup">
<div class="menuitem">
<a href="../../hibernate/tutorialtwo/tutorial2_intro.html">Introduction</a>
</div>
<div class="menuitem">
<a href="../../hibernate/tutorialtwo/tutorial2_1.html">Initialize the Library Editor</a>
</div>
<div class="menuitem">
<a href="../../hibernate/tutorialtwo/tutorial2_2.html">Run the Library Editor</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.4')" id="menu_1.4Title" class="menutitle">GMF Editor Tutorial</div>
<div id="menu_1.4" class="menuitemgroup">
<div class="menuitem">
<a href="../../hibernate/gmftutorial/tutorial1.html">Introduction</a>
</div>
<div class="menuitem">
<a href="../../hibernate/gmftutorial/tutorial2.html">Environment Setup</a>
</div>
<div class="menuitem">
<a href="../../hibernate/gmftutorial/tutorial3.html">Running the editor</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.5')" id="menu_1.5Title" class="menutitle">Details</div>
<div id="menu_1.5" class="menuitemgroup">
<div class="menuitem">
<a href="../../hibernate/dynamic.html">Dynamic EMF Tutorial</a>
</div>
<div class="menuitem">
<a href="../../hibernate/inheritance.html">Inheritance Mapping</a>
</div>
<div class="menuitem">
<a href="../../hibernate/hibernate_relations.html">Modeling Associations</a>
</div>
<div class="menuitem">
<a href="../../hibernate/hbdatastore.html">HbDataStore</a>
</div>
<div class="menuitem">
<a href="../../hibernate/options.html">Options</a>
</div>
<div class="menuitem">
<a href="../../hibernate/hibernate_details.html">EMF - Hibernate Details</a>
</div>
<div class="menuitem">
<a href="../../hibernate/hibernateresources.html">EMF Hibernate Resources</a>
</div>
<div class="menuitem">
<a href="../../hibernate/resource_utility.html">Resource Utility</a>
</div>
<div class="menuitem">
<a href="../../hibernate/troubleshooting.html">Troubleshooting</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.6')" id="menu_1.6Title" class="menutitle">Annotations (JPA/EJB3)</div>
<div id="menu_1.6" class="menuitemgroup">
<div class="menuitem">
<a href="../../hibernate/ejb3_format.html">Format</a>
</div>
<div class="menuitem">
<a href="../../hibernate/ejb3_examples.html">Examples</a>
</div>
<div class="menuitem">
<a href="../../hibernate/ejb3_features.html">JPA/EJB3</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.7')" id="menu_1.7Title" class="menutitle">XML Schema</div>
<div id="menu_1.7" class="menuitemgroup">
<div class="menuitem">
<a href="../../hibernate/features_details.html">XML Schema Support</a>
</div>
<div class="menuitem">
<a href="../../hibernate/schema_list.html">XML Schema Examples</a>
</div>
<div class="menuitem">
<a href="../../hibernate/featuremap.html">Feature Map/Mixed Content</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.8')" id="menu_1.8Title" class="menutitle">Support</div>
<div id="menu_1.8" class="menuitemgroup">
<div class="menuitem">
<a href="../../hibernate/upgrading.html">Upgrading</a>
</div>
<div class="menuitem">
<a href="../../hibernate/mailinglist.html">Newsgroup</a>
</div>
<div class="menuitem">
<a href="../../hibernate/emfhibsupport.html">EMF/Hibernate Support</a>
</div>
</div>
<div onclick="SwitchMenu('menu_1.9')" id="menu_1.9Title" class="menutitle">Developer</div>
<div id="menu_1.9" class="menuitemgroup">
<div class="menuitem">
<a href="../../hibernate/svn.html">CVS</a>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td></td><td>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="border bottom-left"></td><td class="border bottomborder"></td><td class="border bottom-right"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" height="10"></td>
</tr>
</table>
<table></table>
<table></table>
<div class="strut">&nbsp;</div>
</td><td>
<div class="content">
<div id="bodycol">
<div id="apphead">
<h2>
<em>Step 2: Create and store EMF Objects</em>
</h2>
</div>
<div class="app" id="projecthome">
<ul class="minitoc">
<li>
<a href="#Hibernate+database+configuration">Hibernate database configuration</a>
</li>
<li>
<a href="#Initialize+runtime+layer">Initialize runtime layer</a>
</li>
<li>
<a href="#Obtain+a+SessionFactory+and+start+a+Transaction.">Obtain a SessionFactory and start a Transaction.</a>
</li>
<li>
<a href="#Create+and+populate+Library">Create and populate Library</a>
</li>
<li>
<a href="#Commit+Transaction+and+close+Session">Commit Transaction and close Session</a>
</li>
<li>
<a href="#Next+Step">Next Step</a>
</li>
</ul>
		
<p>
			The complete code for this tutorial can be found here: <a href="../samples/Tutorial1.java">Tutorial1.java</a>.
		</p>
		
<a name="N10015"></a><a name="Hibernate+database+configuration"></a>
<div class="h3">
<h3>Hibernate database configuration</h3>
			
			
<p>To let Hibernate access your database, you must specify the JDBC driver, database URL, username/password and the
				database-specific dialect to use. These settings are specified using the standard Properties mechanism.</p>
			
<p>Teneo allows you to configure Hibernate in two ways: </p>
			
<ul>
				
<li>Automatically: by placing the properties in the file "hibernate.properties" in a root directory of the classpath. We
					use this method in this tutorial.</li>
				
<li>Programmatically: by creating a Properties instance and using HbDataStore.setHibernateProperties().</li>
			
</ul>
			
<p>A sample hibernate.properties file is shown below:</p>

<pre class="code">hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc:hsqldb:hsql://127.0.0.1/library
hibernate.connection.username=sa
hibernate.connection.password= hibernate.dialect=org.hibernate.dialect.HSQLDialect
</pre>
			
<p> Example hibernate.properties: </p>
			
<ul>
				
<li>
					
<a href="mysql/hibernate.properties">hibernate.properties</a> for
					<a class="external" href="http://www.mysql.com/">MySQL</a>
</li>
				
<li>
					
<a href="hsqldb/hibernate.properties">hibernate.properties</a> for
					<a class="external" href="http://www.hsqldb.org/">HSQLDB</a>
</li>
			
</ul>
			
<p>Note that Hibernate has many more configuration properties. For more information see the
				<a class="external" href="http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html">Hibernate
					reference documentation</a>.</p>
		
</div>
		
		
<a name="N10053"></a><a name="Initialize+runtime+layer"></a>
<div class="h3">
<h3>Initialize runtime layer</h3>
			
		
<p>Create and initialize the runtime Hibernate EMF layer as follows:</p>
		
<pre class="code">
// Create the DataStore.
final String dataStoreName = "LibraryDataStore";
final HbDataStore dataStore = HbHelper.INSTANCE.createRegisterDataStore(dataStoreName);

// Configure the EPackages used by this DataStore.
dataStore.setEPackages(new EPackage[] { LibraryPackage.eINSTANCE });

// Initialize the DataStore. This sets up the Hibernate mapping and
// creates the corresponding tables in the database.
dataStore.initialize();</pre>
		
</div>
				
		
<a name="N10061"></a><a name="Obtain+a+SessionFactory+and+start+a+Transaction."></a>
<div class="h3">
<h3>Obtain a SessionFactory and start a Transaction.</h3>
			
			
<p>The SessionFactory, Session and Transaction are the familiar Hibernate entry points:</p>
			
<pre class="code">
final SessionFactory sessionFactory = dataStore.getSessionFactory();

// Open a new Session and start Transaction.
Session session = sessionFactory.openSession();
session.beginTransaction();</pre>

</div>
		
<a name="N1006F"></a><a name="Create+and+populate+Library"></a>
<div class="h3">
<h3>Create and populate Library</h3>
			
	
<pre class="code">
// Create a Library.
Library library = LibraryFactory.eINSTANCE.createLibrary();
library.setName("My Library");
// Make it persistent.
session.save(library);
		
// Create a writer...
Writer writer = LibraryFactory.eINSTANCE.createWriter();
writer.setName("JRR Tolkien");

// ...and one of his books.
Book book = LibraryFactory.eINSTANCE.createBook();
book.setAuthor(writer);
book.setPages(305);
book.setTitle("The Hobbit");
book.setCategory(BookCategory.SCIENCE_FICTION_LITERAL);

// Add the Writer and Book to the Library. They are made
// persistent automatically because the Library is itself
// already persistent.
library.getWriters().add(writer);
library.getBooks().add(book);</pre>
	
</div>
		
	
<a name="N1007A"></a><a name="Commit+Transaction+and+close+Session"></a>
<div class="h3">
<h3>Commit Transaction and close Session</h3>
		
		
<p>The Library is not stored in the database until you commit the Transaction:</p>
		
<pre class="code">session.getTransaction().commit();
session.close();</pre>
	
</div>
		
	
<a name="N10088"></a><a name="Next+Step"></a>
<div class="h3">
<h3>Next Step</h3>
		
		
<p>In the next step of this tutorial we retrieve the Library from the database and add a new Book and Writer.</p>
		
<p>
<a href="tutorial1_3.html">Click here</a> to go to the next step.</p>
	
</div>	
	
</div>
</div>
</div>
</td>
</tr>
</table>
<div width="100%" id="footer">
<table width="100%" cellpadding="4" cellspacing="0" border="0">
<tr>
<td class="footer"><a href="license.html">
          Copyright &copy; 2007 The Elver Project</a>
      - All rights reserved.
      </td>
</tr>
</table>
</div>
</body>
</html>

Back to the top