users@glassfish.java.net

JPA + Hibernate + Enumerations

From: <glassfish_at_javadesktop.org>
Date: Tue, 08 Sep 2009 13:43:15 PDT

Hi,
I'm not sure this is the right section to post my question, it is about JPA, i am not using GF but i didn't see any JPA section.

I am using JPA with Hibernate on Tomcat6.

I want my domain classes to expose some static instances, in this example we could have business.Color.RED

In resume, what i want is that my domain classes acts as persistent enumeration only for predefined values. But not an Integer nor a String but just any domain class. I know about the Hibernate UserType class but i guess that one isn't designed for the same purpose, cause i want not only to have enumerated values but also normal instances populated through getReference() method.

At first i thought on doing something like this:

public class Color {
    public static final Color RED;

    static {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();

        RED = em.getReference(Color.class, "FF0000");

        em.close();
        PersistenceManager.getInstance().closeEntityManagerFactory();
    }
.
.

}

The problem is that a new EntityManagerFactory will be created on each domain class initialization.

Maybe i could solve this by instancing the EntityManagerFactory at request initialization time and saving it, but i am not sure how can i save something into request-scope. I have no EJB container so no RequestBean present.
Should i save it to HttpSession?

I suspect that there is a better way for solving this situation.
Maybe i am focusing the problem in the wrong way. Any suggestion?

Thanks in advance.
[Message sent by forum member 'zeven' (zeva77_at_gmail.com)]

http://forums.java.net/jive/thread.jspa?messageID=363285