Hibernate Setup
Description
This is based on previously setting up and documenting the addition of Hibernate to Java projects.
- First attempt is under the name Hibernate & MySQL.
- Second attempt is under the name Hibernate Based Web Service.
This attempt is part of exploring GWT's Request Factory.
Goals
- Have a domain object I want to persist to a MySQL database.
- The PointOfInterestSource is where much of the code will be held for supplying the PointOfInterestService.
- I'd like to use the Hibernate Tool for Eclipse where possible.
- I'd like to generate the DDL and table from an annotated class.
Steps
- Prepare Maven for Hibernate dependencies. It looks like I can follow the Maven Guide for Hibernate.
- Includes Maven Install (which I have)
- I'm using Java 6 (there used to be a restriction to use Java 5).
- Pasted in the recommended changes to the settings.xml (where is this now?) that allow the Hibernate plugins and repositories to be found.
- Manually added the following dependencies to the pom.xml for the project. Note that as of Dec 2011, these are the latest 3.x versions and not the 4.x versions.
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.9.Final</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>3.6.9.Final</version> <type>jar</type> <scope>runtime</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.6.9.Final</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> <type>jar</type> <scope>runtime</scope> </dependency>
- Added the Index to the JBoss (Hibernate) Repository //I'm leaving this step from a previous attempt, but I don't know how exactly how this is done. //
- Bring in Hibernate Tools for Eclipse unchecked because I already have these loaded.
- Using Eclipse to maintain versions of the Hibernate Tools (use Help -> Software Updates …) from within Eclipse and add a site for JBoss/Hibernate.
- Update Site is http://download.jboss.org/jbosstools/updates/stable
- Built up "Persistence" class (a bean)
- Created this shell of a class and added '@Entity" annotation.
- When resolving the dependency, I chose the javax.persistence.Entity package instead of the hibernate annotations package.
import javax.persistence.Entity; import com.jettmarks.appname.server.PointOfInterestService; /** * These are the nodes on a Clue Route where a Key can be found to unlock the * Lock on the next set of clues. * * @author jett */ @Entity public class PointOfInterest {
- The full section of annotated code is below
/** Database representation of this entity. */ @Id @GeneratedValue @Column Integer id; /** A word or two identifying the Point of Interest. */ @Column String name; /** Longer description of the Point of Interest. */ @Column String description; /** GeoLocation */ @Column double lat, lon;
Setup a DB connection
- Easier when done here instead of as part of the Hibernate configuration.
- Described on this page: Configure Hibernate Datasource
Generate a Configuration using the Hibernate Tool under Eclipse
- Open Hibernate Configurations window (Window -> Show View -> Other -> Hibernate)
- Right Click and then select "Add Configuration"
- Change name to mySql
- Type is Annotations
- Browse for project (listTags for initial pass)
- Create new Connection Create Eclipse DB Connection
Attempt to generate DDL
- Should be able to use hbm2ddl tool.
page revision: 7, last edited: 27 Dec 2011 11:21