-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem. Generated DAO code refers to JNDI Context.
PostPosted: Wed Nov 07, 2007 6:46 am 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
I am using hibernate 3.2.5, and I am learning to generate automatic code from mapping files. To generate code I use Hibernate Code Generation Dialog for Eclipse. The problem I have, is with generated DAOs. They all try to get SessionFactory from JNDI context, but I don't use JNDI context. I use the HibernateUtil class that is fount on tutorial. The important files are:

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

<hibernate-mapping package="cars">
   <class name="Option" table="OPTIONS">
      <id name="id" column="OPTION_ID" type="integer">
         <meta attribute="use-in-tostring">true</meta>
         <meta attribute="use-in-equals">true</meta>         
         <generator class="native" />
      </id>   
      <property name="optionName" type="string" column="OPTION_NAME" unique="true" not-null="true" />   
   </class>   
</hibernate-mapping>


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 name="SessionFactory">
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.pool_size">1</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cars</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <mapping resource="cars/Option.hbm.xml" />
    </session-factory>
</hibernate-configuration>


Generated DAO file:
Code:
public class OptionHome {

    private static final Log log = LogFactory.getLog(OptionHome.class);

    private final SessionFactory sessionFactory = getSessionFactory();
   
    protected SessionFactory getSessionFactory() {
        try {
            return (SessionFactory) new InitialContext().lookup("SessionFactory");
        }
        catch (Exception e) {
            log.error("Could not locate SessionFactory in JNDI", e);
            throw new IllegalStateException("Could not locate SessionFactory in JNDI");
        }
    }   
    ........ //Some more irrelevant code
}


And the HibernateUtil.java file:


Code:
public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}


The question is: Is there any way to generate a code that refers to HibernateUtil.getSessionFactory() instead of (SessionFactory) new InitialContext().lookup("SessionFactory") ?

If not, there is an easy way to setup InitialContext? (Preferably, not using a web server with EJB container)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 07, 2007 9:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you can adjust the templates.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.