【问题标题】:Problems using eclipse Hibernate plugin - could not locate sessionfactory in JNDI使用 eclipse Hibernate 插件的问题 - 无法在 JNDI 中找到 sessionfactory
【发布时间】:2010-12-09 21:18:49
【问题描述】:

我正在使用 eclipse hibernate 插件中内置的逆向工程功能为每个表生成 dao 和 hbm.xml 文件。

它做得很好,但是当我尝试使用生成的对象时,我得到一个无法在 JNDI 中找到 SessionFactory 错误。

我看到一篇帖子建议当您在 hibernate.cfg.xml 文件中命名您的 SessionFactory 时会发生这种情况,因此我删除了名称标签,但仍然遇到相同的错误。

这是我的 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>

这是触发异常的生成代码

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 插件来实现这一点。

更改生成的代码不会真正帮助我,因为我需要在某些时候继续重新生成它,所以如果有人能解释为什么/如何发生这种情况以及我能做些什么,我将不胜感激

谢谢

乔纳森

【问题讨论】:

  • 我也面临同样的问题。你摆脱了这个问题吗?如果是,请告诉我们解决方案....

标签: hibernate eclipse-plugin jndi sessionfactory


【解决方案1】:

在 buildsessionfactory 方法内部初始化 initialcontext.pass the sessionfactory jndi name in the get session factory method(i.e in lookup)

【讨论】:

    【解决方案2】:

    您可以直接在休眠配置文件中指定所有连接、密码、用户名等,然后使用如下代码加载:

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

    或者,您可以从 JNDI 获得它。这允许您的系统管理员在部署后通过向 JNDI 注册不同的 SessionFactory 来更改连接、密码、用户名等。

    您需要查阅应用服务器的文档以了解如何使用应用服务器指定 JNDI 资源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-29
      • 2012-08-25
      • 2014-06-10
      • 2011-01-29
      • 2011-06-25
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      相关资源
      最近更新 更多