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

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

  1. Prepare Maven for Hibernate dependencies. It looks like I can follow the Maven Guide for Hibernate.
    1. Includes Maven Install (which I have)
    2. Mentions that Java 5 (not 6) should be used. Possible source of conflict with Five Points server.
    3. Pasted in the recommended changes to the settings.xml that allow the Hibernate plugins and repositories to be found.
    4. 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.
  2. Added the Index to the JBoss (Hibernate) Repository
  3. 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
  1. Bring in Hibernate Tools for Eclipse
    1. Using Eclipse to maintain versions of the Hibernate Tools (use Help -> Software Updates …) from within Eclipse and add a site for JBoss/Hibernate.
    2. Update Site is http://download.jboss.org/jbosstools/updates/stable
  2. Built up "Persistence" class (a bean)
    1. 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
{
 
}
  1. Used Maven "Add Dependency" to pull up hibernate-annotations artifact to resolve the dependency created by the @Entity annotation.
  2. 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
  3. 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

  1. Open Hibernate Configurations window (Window -> Show View -> Other -> Hibernate)
  2. Right Click and then select "Add Configuration"
    1. Change name to mySql
    2. Type is Annotations
    3. Browse for project (listTags for initial pass)
    4. 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)

*

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License