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>
<persistence-unit name="hello-world" transaction-type="RESOURCE_LOCAL">
<class>com.foo.Greeting</class>
<properties>
<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"/>
</properties>
</persistence-unit>
</persistence>

* 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%\hibernate-entitymanager.jar;%LIB%\hibernate3.jar;%LIB%\jboss-common.jar;
%LIB%\dom4j-1.6.1.jar;%LIB%\hibernate-annotations.jar;%LIB%\commons-logging-1.0.4.jar;
%LIB%\cglib-2.1.3.jar;%LIB%\javassist.jar;%LIB%\commons-collections-2.1.1.jar;
%LIB%\mysql-connector-java-5.0.3-bin.jar;%LIB%\ehcache-1.2.jar;%LIB%\asm.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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s