Hibernate JAVA Spring
Hibernate JAVA Spring
4. Tools needed
Download latest version of Eclipse: http://www.eclipse.org/downloads
Download latest JDK: http://www.oracle.com/technetwork/java/javase/downloads/index.html
Download latest MySQL: http://dev.mysql.com/downloads/mysql
Download latest Hibernate distribution zip: http://sourceforge.net/projects/hibernate/files/hibernate4 (Hibernate 4.1.9 as of
writing)
Let’s presume you’ve unzipped the Hibernate distribution zip to a directory called Hibernate. You would need all the jar
files under Hibernate\lib\required and Hibernate\lib\jpa directories
MySQL Java Connector Jar: http://dev.mysql.com/downloads/connector/j If you want to learn more about Hibernate, find
and read this book: Java Persistence with Hibernate
5. Project Structure
Here we instructed Hibernate to connect to a MySQL database named hibernateTutorial. As you can see, we supplied
database URL, username and password for the connection. We also instructed Hibernate to use MySQLDialect i.e. Hibernate will
optimize the generated SQL statements for MySQL. We also added couple of entities called Person and Address which we’ll
configure later. This configuration will be used to create a Hibernate SessionFactory object.
Create model classes Address.java and map it to the database using annotations as follows:
8. Create Database
Now go to any MySQL query tool (Preferably SQLYog)
As you can see, there is a one-to-many relationship between person and person_address, one-to-many relationship between
address and person_address and as a result of that, many-to-many relationship between person and address tables.
If you are new to Hibernate and want to learn more, read this book: Hibernate Made Easy: Simplified Data Persistence with
Hibernate and JPA (Java Persistence API) Annotations
Ideally, it’s a good practice to create one SessionFactory object per data store at global application level scope. Hence here we
are creating just one static SessionFactory object when the class is first loaded and then we access the same via
getSessionFactory() static method. This is the safest implementation of a singleton object.
Now let’s create a PersonManager class which creates 2 person and 3 address objects and persists them to the database. Note
8 sur 10 24/04/2017 09:43
Getting Started With Hibernate Annotations http://www.codejava.net/frameworks/hibernate/getting-started-with-hi...
that all the addresses have been persisted using Hibernate cascade functionality. Here is code of the PersonManager.java
class:
Just right click in the editor window and run the project as Java application. You should see following output in the console…
Person table
Address table
Person_Address table
Above you can see that in person table, persons Steve and Donald have been saved with auto generated ids 1 and 2
respectively. In address table, addresses San Francisco, Chicago and New York have been saved with auto generated ids 1, 2
and 3 respectively. Also in person_address table person_id 1 is associated with address_id 1 and person_id 2 is
associated with address_ids 2 and 3 respectively.
This tutorial shows how easily you can configure session factory connection details using XML and entities using annotation
configuration respectively in Hibernate and access the database. By using XML, database connection properties can be easily
changed without changing the Java source files which is an added advantage. By using annotations, Java entity classes are more
expressive and you don’t have to refer to another XML file for figuring out the Hibernate-Database mapping.
Above shown code snippets can be downloaded as a full Eclipse project along with the accompanying jar files from the attachment
section.
Attachments:
HibernateAnnotationTutorial.zip [ ] 6267 kB
Gestionnaire de tâches