Problems using the Hibernate plugin for eclipse - could not find sessionfactory in JNDI

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

It does it pretty well, but when I try to use the generated objects, I get I can not find the SessionFactory in the JNDI error.

I saw a message that suggested that this happens when you call your SessionFactory in the hibernate.cfg.xml file, so I removed the name tag and I still get 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"> <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 throws 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 am not very versed in JNDI, but I think that some kind of search is equivalent to a configuration file. I do not want to use JNDI, but I do not know how to achieve this using the eclipse pluggin.

Changing the generated code will not help me, because I will need to continue to restore it at some points, so if anyone can explain why and how this happens, and what I can do about it, I would be grateful

thanks

Jonathan

+4
source share
2 answers

You can specify all connection, password, username, etc. directly in the hibernation configuration file, and then load using the following code:

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

Or you can get it from JNDI. This allows your system administrator to change the connection, password, username, etc. After deployment, registering another SessionFactory using JNDI.

You will need to refer to your application server documentation on how to specify JNDI resources on the application server.

+3
source

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

0
source

All Articles