Problems using eclipse Hibernate plugin - could not locate sessionfactory in JNDI

百般思念 提交于 2019-11-27 18:51:52

问题


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">
<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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/1620612/problems-using-eclipse-hibernate-plugin-could-not-locate-sessionfactory-in-jnd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!