views:

1243

answers:

1

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

+1  A: 

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.

ChssPly76
I am not currently using spring. I looked into it, but I ran out of time to research it and it seemed pretty complex to set up.
tcoulson
Am I missing something? I tried to download Spring Blaze DS integration and it lead me to a page to download, I click the button and nothing happens.
tcoulson
Here's a direct link: http://s3.amazonaws.com/dist.springframework.org/release/FLEX/spring-flex-1.0.0.RELEASE-with-dependencies.zip - seems to work for me. I personally haven't worked with BlazeDS; but Spring itself - while it does have a bit of a learning curve - is not that hard to set up but provides a lot of features. Once you work with it you'll wonder how you've done without. To get started you only really need to understand IOC / app context (http://static.springsource.org/spring/docs/2.5.x/reference/beans.html) plus Hibernate integration (link in my answer).
ChssPly76
yeah, so suppose I do not work with Spring yet (because of that learning curve). Any advice on the questions pertaining to EntityManager and SessionFactory? Does every function in java need to open an EntityManager and EntityManagerFactory and close it when it is finished? That seems like overuse, and not a good implementation. But it is how all of the hibernate examples seem to write their code.
tcoulson
You most certainly should not be building SessionFactory (EntityManagerFactory) in every call - they should be created once and reused. Session / EntityManager should be created for each separate **unit of work**, which may be a single call or may span across several calls. The reason I've mentioned Spring is because it will take care of (majority of) this for you; whatever time you spend learning it now you'll save ten times over later when you won't have to debug your code.
ChssPly76
I agree it will save me time, but our project is small in scope and initially I do not have time to dedicate to Spring. It may be something I revisit in phase 3 of the project when I have more time, but for now I cannot do that. How does a HibernateUtil class get called? and I know the Hibernate docs say to have SessionFactory built in the HibernateUtil class, can you build a EntityManagerFactory inside this class too?
tcoulson
It's either or. If you're using JPA you don't need SessionFactory and vice versa. Hibernate EM documentation describes EMF / EM instantiation process: http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/configuration.html#d0e612
ChssPly76
thank you much, as soon as I get an open id, I will mark this as answered.
tcoulson