views:

1653

answers:

2

Hi Guys,

I'm using the reverse engineering capabilities built into the eclipse hibernate plugin to generate dao's and hbm.xml files for each table.

It does it quite well but when I try to use the generated Objects I get a Could not locate SessionFactory in JNDI error.

I saw a post that suggested this happens when you name your SessionFactory in the hibernate.cfg.xml file so I removed the name tag and i'm still getting the same error.

This is my hibernate.cfg.xml

    <?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"&gt;
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">qwerty</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/agilegroup3</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.default_catalog">agilegroup3</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <mapping resource="generated/Usersroles.hbm.xml" />
        <mapping resource="generated/Role.hbm.xml" />
        <mapping resource="generated/Logdata.hbm.xml" />
        <mapping resource="generated/Logtrigger.hbm.xml" />
        <mapping resource="generated/User.hbm.xml" />
    </session-factory>
</hibernate-configuration>

This is the generated code that triggers the exception

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");
 }
}

I don't know much about JNDI but I guess its some sort of lookup equiv to a config file. I dont want to use JNDI but I dont know how to achieve this using the eclipse pluggin.

Changing the generated code wont really help me because I'll need to keep regenerating it at some points so if anyone could explain why/how this is happening and what I can do about it I'd be grateful

Thanks

Jonathan

A: 

You can either specify all the connection, password, username etc. directly in a hibernate configuration file, and then load using code like:

Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();

Or, you can obtain it from JNDI. This allows your sysadmin to change the connection, password, username etc. after deployment, by registering a different SessionFactory with JNDI.

You would need to consult your application server's documentation on how to specify JNDI resources with the application server.

toolkit
A: 

Inside of buildsessionfactory method initialize initialcontext.pass the sessionfactory jndi name in the get session factory method( i.e in lookup)

karan