使用 eclipse Hibernate 插件的问题 - 无法在 JNDI 中找到 sessionfactory [英] Problems using eclipse Hibernate plugin - could not locate sessionfactory in JNDI

查看:29
本文介绍了使用 eclipse Hibernate 插件的问题 - 无法在 JNDI 中找到 sessionfactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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.cfg.xml 文件中命名您的 SessionFactory 时会发生这种情况,所以我删除了名称标签,但我仍然遇到同样的错误.

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

谢谢

乔纳森

推荐答案

你可以直接在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆