使用eclipse Hibernate插件的问题 - 无法在JNDI中找到sessionfactory [英] Problems using eclipse Hibernate plugin - could not locate sessionfactory in JNDI
问题描述
我使用eclipse hibernate插件内置的反向工程功能为每个表生成dao和hbm.xml文件。
I'm using the reverse engineering capabilities built into the eclipse hibernate plugin to generate dao's and hbm.xml files for each table.
它做得很好,但是当我尝试使用生成的对象时,我得到一个无法在JNDI错误中找到SessionFactory。
It does it quite well but when I try to use the generated Objects I get a Could not locate SessionFactory in JNDI error.
我看到一个帖子,建议这种情况发生在您在Hibernate中命名您的SessionFactory .cfg.xml文件,所以我删除了名称标签,我仍然得到相同的错误。
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.
这是我的hibernate.cfg.xml
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");
}
}
我不太了解JNDI,但我猜它的某种查找等同于配置文件。我不想使用JNDI,但我不知道如何使用eclipse插件实现这一点。
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
谢谢
Jonathan
推荐答案
您可以直接在一个指定所有连接,密码,用户名等hibernate配置文件,然后加载使用如下代码:
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();
或者,您可以从JNDI获取。这将允许您的系统管理员通过在JNDI上注册不同的SessionFactory来更改部署后的连接,密码,用户名等。
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.
您需要查询应用服务器的文档如何使用应用程序服务器指定JNDI资源。
You would need to consult your application server's documentation on how to specify JNDI resources with the application server.
这篇关于使用eclipse Hibernate插件的问题 - 无法在JNDI中找到sessionfactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!