Hibernate and MySQL
This is the top level page for Hibernate with GWT against MySQL.
Current Work
VISCAT-75 to add configuration information outside of re-deployment.
Some notes on the config options:
- If we're inside a container, let's find that container and use it DataSource instance. This should also be able to provide connection pooling. We just need to agree on a well-recognized name for the JNDI entry.
- If we're not inside a container that provides connection pooling, let's instantiate a session that does support connection pooling. I understand that's what the code currently does as of June, 2014.
- If we're not insde a container that provides connection pooling and we're running a standalone (JUnit) test, it's OK to create our own pool and so forth, but we'll want to override the database URL, the account and password.
All of the above should be accessible programmatically inside the HibernateUtil class. Reference: http://docs.jboss.org/hibernate/core/3.3/reference/en-US/html/session-configuration.html
References
- Much of this is dependent on getting the Eclipse Data Tools in place: http://www.eclipse.org/datatools/
- Hibernate Wikipedia article
- Hibernate Basics - Good introductory article for a number of Hibernate topics (connection pooling, transactions, cache).
- Download Page - recent versions listed here.
- Hibernate Annotations and Entity Manager - Annotations are an alternative to the XML config; Entity Manager supports the JPA.
- Chapter 2 (pdf) - Detail walkthrough of getting going with Hibernate, Annotations and Entity Manager.
- Hibernate4GWT - Recommended tool for making it easier to pass the Hibernate "POJO"s across the RPC.
- Hibernate Tools - for Eclipse; provides nice views into the configuration and the database.
- hbm2ddl or SchemaExport - generating Schemas from either Hibernate's XML mapping files or Java source-code Annotations.
- Using Hibernate Ant Tasks - I used this path since I couldn't get the standalone SchemaExport to dump anything.
- Naming Strategy - useful for automatically creating table names from the Java Class names.
- Connection Pooling (C3P0 ?)
- Maven Guide for Hibernate - does mention the Maven Eclipse Plugin as well.
- Mapping Cheat Sheet - unfortunately, doesn't use Annotations.
- Collections Tutorial with persistence strategies; again, with no Annotations.
- Hibernate Project's Documentation - not bad for simply reading.
- AndroMDA for Hibernate - Code Generation for Hibernate.
- Inheritance Options - with examples of 3 different strategies
- Hibernate Code Generation - DDL from Java Code, Java Code from a schema, and more
How To
This initial post is a capture of the first thing I've tried with Hibernate. Second attempt is under the name Hibernate Based Web Service. This has been further distilled and corrected on the page Hibernate Setup.
- I have a list of UserIDs for which I want to capture a "role" representing that user's authorization level. At this time, there is a single role per user, but I can see that a user may have multiple roles in the future.
- I'd like to map the entries in this table to a HashMap (or other Map implementation, I don't care).
- I'd like to use the Hibernate Tool for Eclipse where possible.
- I'd like to generate the DDL 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)
- Mentions that Java 5 (not 6) should be used. Possible source of conflict with Five Points server.
- Pasted in the recommended changes to the settings.xml that allow the Hibernate plugins and repositories to be found.
- Ran mvn clean install and expected the Hibernate files to be downloaded and installed, but I guess because I don't have any dependencies, nothing was downloaded yet.
- Added the Index to the JBoss (Hibernate) Repository
- Bring in the standard download of a recent version. Release 3.5 bundles the core with Annotations and the Entity Manager, but it is still in Beta as of 01 Nov 2009. Also, there is a dependency of the Tools on slightly older versions of the Core and Annotations. I'm currently grabbing the following pieces:
Package | Version | Release Date | Category | Download |
---|---|---|---|---|
Hibernate Core | 3.2.6 GA | 07 Feb 2008 | Production | zip |
Hibernate Annotations | 3.3.1 GA | 14 Mar 2008 | Production | zip |
Hibernate Tools | 3.2.4 GA | 07 May 2009 | Production | zip |
- Bring in Hibernate Tools for Eclipse
- 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.
/** * Handles Authorization of Users via roles recorded in a database table per * UserID. * * @author jett */ @Entity public class UserAuth { }
- Used Maven "Add Dependency" to pull up hibernate-annotations artifact to resolve the dependency created by the @Entity annotation.
- Whereas that worked for the Entity, it hasn't worked for anything else. The Maven "Add Dependency" is showing lots of choices for "Column", but nothing for "SessionFactory". I"m loading in the libraries suggested by the Hibernate Annotations page:
- Hibernate Core (all of it)
- hibernate-annotations.jar,
- lib/hibernate-comons-annotations.jar and
- lib/ejb3-persistence.jar
- Adding the libraries listed above, I'm able to compile this simple piece of code:
@Entity public class UserAuth implements Serializable { /** * */ private static final long serialVersionUID = 1L; @CollectionOfElements @MapKey() private Map<String, String> userRoles = new HashMap<String, String>(); public String getUserRole(String userId) { return userRoles.get(userId); } }
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.
Questions
- Does the Entity Manager assist with populating Hashes (one-time loads of config type data from a table)? (Possible Solution)
*
page revision: 54, last edited: 03 Jul 2014 02:15