You are here

MyEclipse+Hibernate-My Exceptions List

Environment: hibernate 2 and Oracle 9i:
This post will record the strange exceptions I have met.

Exception 1:


java.sql.SQLException: ORA-02289: sequence does not exist

When I tried to insert one new record I got this error, although I can access the database and query. The explaination from http://ora-02289.ora-code.com/ :
Cause: The specified sequence does not exist, or the user does not have the required privilege to perform this operation.
Action: Make sure the sequence name is correct, and that you have the right to perform the desired operation on this sequence.

Uhmmmm, not clear for me. Search on google and found some thing.
this is my hibernate mapping hbm xml:

<hibernate-mapping>
<class name="genis_su.genis_gleis.Gg180Mastkomponente" table="GG180_MASTKOMPONENTE" schema="GENIS_GLEIS">
<id name="gg180Id" type="java.lang.Long">
<column name="GG180_ID" precision="22" scale="0" />
<generator class="native" />
</id>
.........

Someone said the id parameter should be changed like this:

<id name="gg180Id" type="java.lang.Long">
<column name="GG180_ID" precision="22" scale="0" />
<generator class="sequence" >
<param name="sequence">yourname_ID_SEQUENCE</param>
</generator>
</id>

And excute this command in oracle to create the sequence:


create sequence yourname_ID_SEQUENCE;

I tried, but still failed. And someone said:

If you are using
<generator class="native"> and oracle as backend

then you should create a sequence called hibernate.sequence in oracle because natve generator takes sequence to generate values.

And I use this command to create the sequence, and the insert function works fine.

create sequence hibernate.sequence;

Ref:
http://www.myeclipseide.com/PNphpBB2-printview-t-4630-start-0.html
http://forum.hibernate.org/viewtopic.php?p=2388110

 Exception 2:

javax.servlet.ServletException: Proxool Provider unable to load JAXP configurator file: proxoolConf.xml
org.logicalcobwebs.proxool.ProxoolException: Attempt to register duplicate pool called 'MyPoolName'

After 10 times requests My app crashed, Hibernate eat many memory of Tomcat, it's time to use hibernate connection pool. After configuration of hibernate.cfm.xml and create new Proxool config file, I got this Exception.

It seems that the app try to create new connection even in the thread already there is one old connection, now the solution is how to use the old sessionFactory in the thread. I use this class (auto created by MyEclipse):

 import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html }.
 */
public class HibernateSessionFactory {

    /**
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses  
     * #resourceAsStream style lookup for its configuration file.
     * The default classpath location of the hibernate config file is
     * in the default package. Use #setConfigFile() to update
     * the location of the configuration file for the current session.   
     */
    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

    private HibernateSessionFactory() {
    }
    
    /**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

        if (session == null || !session.isOpen()) {
            if (sessionFactory == null) {
                rebuildSessionFactory();
            }
            session = (sessionFactory != null) ? sessionFactory.openSession()
                    : null;
            threadLocal.set(session);
        }

        return session;
    }

    /**
     *  Rebuild hibernate session factory
     *
     */
    public static void rebuildSessionFactory() {
        try {
            configuration.configure(configFile);
            sessionFactory = configuration.buildSessionFactory();
        } catch (Exception e) {
            System.err
                    .println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
        }
    }

    /**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

    /**
     *  return session factory
     *
     */
    public static org.hibernate.SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     *  return session factory
     *
     *    session factory will be rebuilded in the next call
     */
    public static void setConfigFile(String configFile) {
        HibernateSessionFactory.configFile = configFile;
        sessionFactory = null;
    }

    /**
     *  return hibernate configuration
     *
     */
    public static Configuration getConfiguration() {
        return configuration;
    }

}

Now in my code, when session is needed, just call :
Session session = HibernateSessionFactory.getSession();

The Class HibernateSessionFactory will check whether the session is already in the thread,  if not, create new one.
Notice, do not close the sessionFactory after quering, just leave it, otherwise you will have connection exception.

Ref:
Improving Hibernate's Performance
 http://blog.chinaunix.net/u/21790/showart_170245.html
http://blog.csdn.net/shangyu79/archive/2007/09/27/1802513.aspx
http://cszyj780217.blogbus.com/logs/2985731.html
http://www.webkaifa.com/sea/200805041651211.htm

Exception 3:

 

Blog: