HelloWorld with JPA, Hibernate and MySql

I love Hello Worlds. So when I read the blog HelloWorld with JPA, TopLink and MySql I’ve quickly copied/pasted the code, downloaded everyhting and made it work. Good. But because I’m more a Hibernate user, I thought, let’s give it a try with the same example but using Hibernate EntityManager instead of Toplink.

So here is the same simple example of standalone java application using Java Persistence API (JPA), Hibernate, and MySql 5. Here is what you have to do :

* First, download Hibernate Core 3.2.CR2, Hibernate Annotations 3.2.0 CR1 and Hibernate EntityManager 3.2.0 CR1
* Write the META-INF/persistence.xml file

<persistence-unit name="hello-world" transaction-type="RESOURCE_LOCAL">
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

* Use the same Entity class and Main class than the one written in the HelloWorld with JPA, TopLink and MySql blog
* Start MySQL
* Compile and run the project with the folowing classpath

java cp ..\classes;%LIB%\ejb3-persistence.jar;
%LIB%\jta.jar com.foo.HelloWorld

Leave a Reply