-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Newbie confusion with lazy fetching/load problem
PostPosted: Thu Sep 17, 2009 11:24 pm 
Newbie

Joined: Thu Sep 17, 2009 10:02 pm
Posts: 4
I'm having a problem with lazy fetching and accessing data through hibernate from my JSF Web app. I am using a DAO pattern to access my data through Hibernate. At first, it woriked fine and would pass all my unit tests. When I tried to access it though my Web bean LogonBean, it does not return any data into the User object I initialize, but doesn't five me any data. I have double-checked the mysql database table and the record with the key I am using is definitely there. When I ran junit tests again, I started getting this exception in the find method:

Code:
org.hibernate.LazyInitializationException <init>
SEVERE: could not initialize proxy - no Session


I cannot understand why it cannot find the session as the session is being created just before the load. The other DAO methods run fine. There is a lot written on lazy fetching and mixed reviews on turning it off. I'm not sure I can even do that in this simple database. Suggestions on what the problem might be and how to resolve it would be greatly appreciated.

I'm using Hibernate 3, MySQL 5.0, and NetBeans 6.7.1

hibernate-cfg.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/quoteestimator</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">dilbert</property>
    <mapping resource="user.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


user.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="com.lingosys.hibernate.quoteest.User" table="users">
    <id name="id" type="string">
        <generator class="assigned"/>
    </id>
    <property name="name" type="string"/>
    <property name="password" type="string"/>
    <property name="admin" type="char"/>
  </class>
</hibernate-mapping>


UserDao.java
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.lingosys.hibernate.quoteest;

/**
*
* @author mphoenix
*/
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import java.util.List;

/**
* The Data Access Object for managing the persistent Users.
*
*/
public class UserDao {
    private Session session;
    private Transaction tx;

    public UserDao() {
        HibernateFactory.buildIfNeeded();
    }

    /**
     * Insert a new User into the database.
     * @param User
     */
    public void create(User User) throws DAOException {
        try {
            startOperation();
            session.save(User);
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
    }


    /**
     * Delete a detached User from the database.
     * @param user
     */
    public void delete(User user) throws DAOException {
        try {
            startOperation();
            session.delete(user);
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
    }
    /**
     * Find an User by its primary key.
     * @param id
     * @return
     */
    public User find(String id) throws DAOException {
        User user = null;
        try {
            startOperation();
            user = (User) session.load(User.class, id);
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
        return user;
    }

    /**
     * Updates the state of a detached User.
     *
     * @param User
     */
    public void update(User User) throws DAOException {
        try {
            startOperation();
            session.update(User);
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
    }

    /**
     * Finds all Users in the database.
     * @return
     */
    public List findAll() throws DAOException{
        List users = null;
        try {
            startOperation();
            Query query = session.createQuery("from User");
            users =  query.list();
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
        return users;
    }

    private void handleException(HibernateException e) throws DAOException {
        HibernateFactory.rollback(tx);
        throw new DAOException(e);
    }

    private void startOperation() throws HibernateException {
        session = HibernateFactory.openSession();
        tx = session.beginTransaction();
    }

}


HibernateFactory.java
Code:
package com.lingosys.hibernate.quoteest;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class HibernateFactory {
    private static SessionFactory sessionFactory;

    /**
     * Constructs a new Singleton SessionFactory
     * @return
     * @throws HibernateException
     */
    public static SessionFactory buildSessionFactory() throws HibernateException {
        if (sessionFactory != null) {
            closeFactory();
        }
        return configureSessionFactory();
    }

    /**
     * Builds a SessionFactory, if it hasn't been already.
     */
    public static SessionFactory buildIfNeeded() throws DAOException{
        if (sessionFactory != null) {
            return sessionFactory;
        }
        try {
            return configureSessionFactory();
        } catch (HibernateException e) {
            throw new DAOException(e);
        }
    }
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
   

    public static Session openSession() throws HibernateException {
        buildIfNeeded();
        return sessionFactory.openSession();
    }

    public static void closeFactory() {
        if (sessionFactory != null) {
            try {
                sessionFactory.close();
            } catch (HibernateException ignored) {
                System.out.println("Couldn't close SessionFactory" + ignored);
            }
        }
    }

    public static void close(Session session) {
        if (session != null) {
            try {
                session.close();
            } catch (HibernateException ignored) {
                System.out.println("Couldn't close Session" + ignored);
            }
        }
    }

    public static void rollback(Transaction tx) {
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (HibernateException ignored) {
            System.out.println("Couldn't rollback Transaction" + ignored);
        }
    }
    /**
     *
     * @return
     * @throws HibernateException
     */
    private static SessionFactory configureSessionFactory() throws HibernateException {
        Configuration configuration = new Configuration();
        configuration.configure();
        sessionFactory = configuration.buildSessionFactory();
        return sessionFactory;
    }
}


LogonBean.java (authenticate method only)
Code:
    public String authenticate() throws NoSuchAlgorithmException, NoSuchAlgorithmException, UnsupportedEncodingException {
        UserDao dao = new UserDao();
        String id = getUid();
        User user = dao.find(id);
        PasswordProcessor pp = PasswordProcessor.getInstance();
        if (pp.encrypt(getPassword()).equals(user.getPassword())) {
            setAuthenticated(true);
            if (user.getAdmin() == 'Y')
                setAdmin(true);
            return "loggedon";
        }
        else {
//            FacesContext.getCurrentInstance().
//                    addMessage(null, new FacesMessage("Incorrect user ID and/or password, please try again."));
            return "failed";
        }

    }


Junit test code
Code:
    @Test
    public void testCreate() {
        System.out.println("create");
        User user = new User("testing", "Phoenix, Michael", "pass", 'Y');
        UserDao instance = new UserDao();
        instance.create(user);
    }

    @Test
    public void testFind() {
        System.out.println("find");
        String id = "testing";
        UserDao instance = new UserDao();
        User result = instance.find(id);
        System.out.println("name = " + result.getName());
        System.out.println("password = " + result.getPassword());
    }

    @Test
    public void testUpdate() {
        System.out.println("update");
        User User = new User("testing", "Phoenix, M", "nopass", 'Y');
        UserDao instance = new UserDao();
        instance.update(User);
    }

    @Test
    public void testFindAll() {
        System.out.println("findAll");
        UserDao instance = new UserDao();
        List result = instance.findAll();
        Iterator iter = result.iterator();
        while (iter.hasNext()) {
            User user = (User) iter.next();
            System.out.println("name = " + user.getName());
            System.out.println("password = " + user.getPassword());
        }
    }

    @Test
    public void testDelete() {
        System.out.println("delete");
        User user = new User("testing", "Phoenix, M", "nopass", 'Y');
        UserDao instance = new UserDao();
        instance.delete(user);
    }


Top
 Profile  
 
 Post subject: Re: Newbie confusion with lazy fetching/load problem
PostPosted: Fri Sep 18, 2009 1:14 pm 
Newbie

Joined: Thu Sep 17, 2009 10:02 pm
Posts: 4
UP. Any ideas on this? I did a debug and checked the session and it appears to be there when the load is executed.


Top
 Profile  
 
 Post subject: Re: Newbie confusion with lazy fetching/load problem
PostPosted: Fri Sep 18, 2009 2:36 pm 
Newbie

Joined: Thu Sep 17, 2009 10:02 pm
Posts: 4
OK, I was able to solve the problem by using get rather than load. Go figure.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.