R – Flex Hibernate – Setting up EntityManager and SessionFactory with Flex/Mate/ and BlazeDS

blazedsflex3hibernatetomcat

I am creating a Flex project using Mate, Blaze DS, Tomcat server. All of that seems to be working. Even my Hibernate calls are working. However they do not seem to be created in the correct way. I am logging in the user with the following block of code in java:

public AbstractUser login(String username, String password){ 
    //Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
    //SessionFactory sessionFactory= cfg.buildSessionFactory();
    SessionFactory sessionFactory;
    sessionFactory = new AnnotationConfiguration()
    .addAnnotatedClass(AbstractUser.class)
    .addAnnotatedClass(CourseSession.class)
    .addAnnotatedClass(Course.class)
    .addAnnotatedClass(Message.class)
    .addAnnotatedClass(Material.class)
    .addAnnotatedClass(TopicSession.class)
    .addAnnotatedClass(Step.class)
    .addAnnotatedClass(Section.class)
    .addAnnotatedClass(Topic.class)
    .addAnnotatedClass(Subtopic.class)
    .configure("hibernate.cfg.xml") 
    .buildSessionFactory();
    sessionFactory.openSession();

    emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);                             
    em = emf.createEntityManager(); 
    logger.debug("** username:"+username);
    logger.debug("** password:"+password);

    Query q = em.createNamedQuery("users.byUnPass");                         
    q.setParameter("username", username);   
    q.setParameter("password", password);
    AbstractUser user;

    try{
        user = (AbstractUser) q.getSingleResult(); 
    }catch(javax.persistence.NoResultException e){
        user = new AbstractUser();
    }

    return user;
}

There are two things wrong with this approach
1. the entityManager and SessionFactory both need to initialize itself when the first button is pressed, creating quite a lag in time, waiting for it to all intialize.
2. i read the docs on hibernate's SessionFactory:

We also recommend a small wrapper class to startup Hibernate in a static initializer block, known as HibernateUtil.
You might have seen this class in various forms in other areas of the Hibernate documentation.

Now I have documentation on how to write the class, but where in my flex/java file format would I place it? Currently in my Java file structure:
com
pegasus
tms- places first level Services
materials – places the material pojo's I am using
Also, what is necessary for setting up the SessionFactory to make the EntityManager fast – because I notice whenever I have a function that opens an EntityManager and then closes it, it takes a lag in time to initialize it.

More general question is, how can I setup my SessionFactory and EntityManager in an optimized way?

Thanks in advance.

Todd

Best Answer

Are you using Spring Framework? If not, I suggest you seriously consider it.

It provides Blaze DS integration as well as Hibernate and JPA integration. You would define everything in a context and let Spring bootstrap it for you. EntityManager instance would be injected directly into your service / DAO.

Related Topic