Tuesday, September 30, 2008

Using Maven with Hibernate Annotations

We've decided to use purely Maven2 and have created a step-by-step tutorial to do so. The tutorial covers both Netbeans and Eclipse, using their respective Maven plugins.

Here is the video link.

The tutorial is as follows.

  1. Install the maven2 plugin for netbeans


  2. create the maven project using the following command.
    • mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=edu.wpi.hibernate.test -DartifactId=HibernateTestApp


  3. Import the project into NetBeans
  4. Add the maven compiler plugin by following the directions on their website here.
  5. Add the following dependencies in this order (groupId, artifactID, version)
    • (org.hibernate, hibernate, 3.2.6.ga)
    • (org.hibernate, hibernate-annotations, 3.3.1.GA)
    • (mysql, mysql-connector-java, 5.1.6) [This may vary depending on which DB you choose]
  6. Create the hibernate.cfg.xml file in "src/main/resources" with the follwing...
    • Change the following symbols { and } to <>

    • {hibernate-configuration}
      {session-factory}
      {property name="connection.url"}jdbc:mysql://hobonator/hibernate{/property}
      {property name="connection.username"}root{/property}
      {property name="connection.password"}root{/property}
      {property name="connection.driver_class"}com.mysql.jdbc.Driver{/property}
      {property name="dialect"}org.hibernate.dialect.MySQLDialect{/property}

      {property name="show_sql"}true{/property}

      {property name="format_sql"}true{/property}
      {property name="hbm2ddl.auto"}create{/property}

      {!-- JDBC connection pool (use the built-in) --}
      {property name="connection.pool_size"}1{/property}
      {property name="current_session_context_class"}thread{/property}

      {/session-factory}
  7. Change the connections settings to your MySQL server settings
  8. Add HibernateUtil class like the one shown in the Hibernate tutorial here
  9. Add a class with Hibernate Annotations
  10. Add a inside the node in hibernate.cfg.xml
  11. Run the project, you should see the table and rows have been created in your mySQL DB.
  12. Run "mvn eclipse:eclipse" in your project folder (assuming that you have maven2 installed correctly in your commandline)
  13. Install the eclipse maven plugin from here
  14. Import the project with eclipse
  15. Run the project with eclipse

No comments: