Your Trail database > Hibernate
Keywords JDBC
Copyright Information This article is licensed under
GNU Public License .
Some of the images in this article may have different copyright licenses.
Please follow the URLs of the images for detailed information about their
specific copyright licenses.
Contact
webmasters for more information
on copyright issues.
1 Introduction
Hibernate is a Java persistence layer toolset.
It maps between Java classes to database tables and from Java data types to SQL data types, bidirectionally. Hibernate also provides data query and retrieval facilities.
The whole purpose of Hibernate was to reduce development time otherwise spent with manual data handling in SQL and JDBC.
2 Installation
Download a copy of Hibernate from Hibernate website
run ant to install hibernate
test your hibernate installation
update etc/hibernate.properties according to your system configuration
make sure your JDBC driver is in $HIBERNATE_HOME/lib
run "ant eg"
3 A simple hibernate example
Trust me, hibernate has excellent documentations, you'll see after you try this out.
Following the instructions step by step in Chapter 2
of the hibernate tutorial. It's one of the best documentations I've ever seen. Basically you need to do the following to
try out your first hibernate applicaiton.
create your directory architecture: A src directory for holding Person.java, Event.java, Person's hibernate mapping file Person.hbm.xml,
Event's hibernate mapping file Event.hbm.xml, and the hibernate configuration file hibernate.cfg.xml. A lib directory containing the following list of
.jar files: antlr.jar, cglib-full.jar, asm.jar, asm-attrs.jars, commons-collections.jar, commons-logging.jar, ehcache.jar, hibernate3.jar, jta.jar,
dom4j.jar, log4j.jar, and hsqldb.jar. Note that some jar filenames might be slightly different from the filenames listed here. They may contain version
nubmers etc.
copy the jar files to the lib directory
add java code or xml config elements into the xml and java files
add build.xml into the main directory
final directoy structure
.
+lib
+src
Event.java
Event.hbm.xml
HibernateUtil.java
hibernate.cfg.xml
+data
build.xml
ant compile; ant run -Devent=aaa
4 Hibernate architecture
The following figure shows the basic hibernate architecture[TUTORIAL]
5 Acquisition Attempt Failed!!! Clearing pending acquires
This may have been caused by hibernate not being able to connect to database server.
6 ObjectVisualizer
ObjectVisualizer is a hibernate-based table object viewer toolkit. Here is a demo http://objectvisualizer.com/files/ov-demo.htm .
7 Links
hibernate mapping cheatsheet
hibernate example tutorial
8 NoClassDefFoundError: javax/transaction/Synchronization
Need to include jta.jar in your class path
9 could not initialize proxy - the owning Session was closed
LIE error (lazy initialization error). A dirty fix is to turn off lazy initialization in .hbm.xml. For example:
<many-to-one name="source" column="source" class="Source" lazy="false"/>
10 I lost all my data every time I restart my jboss and hibernate, what's wrong?
Check you hibernate.cfg.xml and see if it has the following line:
<property name="hibernate.hbm2ddl.auto">create</property>
This line tells hibernate to recreate the tables every time it restarts
11 I got weird errors about NonUniqueObjectException
close session, then reopen it, then update()
12 I got errors about failed to initialize hibernate, duplicate xx.hbm.xml
restart you eclipse
13 I have a datetime field, how come I only get to set date?
Even though you set the field to new Date().
Are you using "date" in your hibernate mapping? Use timestamp instead.
14 org.hibernate.exception.JDBCConnectionException: could not execute query
You could have so many connections hanging around that mysql doesnot have more connections to give out.
15 org.hibernate.NonUniqueObjectException
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
The session maintains a unique mapping between persistent identity and object instance, in order to help you avoid data aliasing problems. You cannot simultaneously attach two objects with the same class and identifier to the same session.
The best way to avoid any possibility of this exception is to always call saveOrUpdate() at the beginning of the transaction, when the session is empty.
In Hibernate3, you can try using the merge() operation, which does not reattach the passed instance.
References [TUTORIAL] http://www.hibernate.org/hib_docs/v3/reference/en/html/architecture.html
Hi"ber*nate (?), v. i. [imp. &
p. p. Hibernated (?); p. pr. & vb.
n. Hibernating (?).] [L. hibernare ,
hibernatum , fr. hibernus wintry. See Hibernal .]
To winter; to pass the season of winter in close quarters, in a
torpid or lethargic state, as certain mammals, reptiles, and
insects.
Inclination would lead me to hibernate , during
half the year, in this uncomfortable climate of Great
Britain. Southey.
(search time: 341.148 msec)
hibernate ['haib2neit] ¹ý¶¬,¶¬Ãß,±Üº®
(search time: -914.262 msec)