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

Categories: Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s