Background
This article demonstrates how to use Hibernate Entity Manager 4.3.7 in a simple application.
Software Environment
- Windows 7 Professional SP1
- Eclipse – Kepler Release
- Java 1.7 (1.7.0_67 – Windows x86)
- MySQL 5.6.16 – Community Server (GPL)
- MySQL Java Connector 5.1.34
- Hibernate 4.3.7 (Entity Manager)
Create a Maven Project
First, create a maven project. You may follow the steps to do so in this article How to create Maven project in Eclipse.
Create a MySQL Database
Run the following DDL to create against an existing MySQL database. In this article, the database schema is named “turretadb.”
1 2 3 4 5 6 7 8 | CREATE TABLE IF NOT EXISTS Student( id int(11) NOT NULL AUTO_INCREMENT, lastname varchar(30) NOT NULL, firstname varchar(30) NOT NULL, middlename varchar(30) NOT NULL, version int(11) NULL, PRIMARY KEY (id) ) ENGINE=InnoDB; |
Maven pom.xml Dependencies
Add the following dependencies in your pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.turreta.jpa.hibernate</groupId> <artifactId>TurretaJPAHibernate</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.7.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> </dependencies> </project> |
The persistence.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="jpahibernate" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <class>com.turreta.jpa.hibernate.entities.Student</class> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpahibernate" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="karlsangabriel" /> <property name="hibernate.show_sql" value="true" /> </properties> </persistence-unit> </persistence> |
Sample JPA Application
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.turreta.jpa.hibernate; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import com.turreta.jpa.hibernate.entities.Student; public class MainClass { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpahibernate"); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); Student student = new Student(); student.setLastName("SGl"); student.setMiddleName("M"); student.setFirstName("KL"); em.getTransaction().commit(); em.close(); emf.close(); } } |
Download the Project
https://www.dropbox.com/s/y2bh5icnjoepndw/TurretaJPAHibernate.zip?dl=0