【问题标题】:Access sessionFactory using Jboss Jndi lookup e.g. java:/hibernate/SessionFactory使用 Jboss Jndi 查找访问 sessionFactory,例如java:/休眠/会话工厂
【发布时间】:2013-03-01 09:30:49
【问题描述】:

我正在尝试使用 Jboss7.1.1.Final Jndi 查找在 maven 下访问 sessionFactory。

在部署它时,我正在关注日志

17:28:33,670 INFO  [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-1) configuring from resource: /hibernate.cfg.xml
17:28:33,671 INFO  [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-1) Configuration resource: /hibernate.cfg.xml
17:28:34,026 INFO  [org.hibernate.cfg.Configuration] (http-localhost-127.0.0.1-8080-1) Configured SessionFactory: java:/hibernate/SessionFactory

17:28:35,785 INFO  [org.hibernate.impl.SessionFactoryImpl] (http-localhost-127.0.0.1-8080-1) building session factory
17:28:36,535 INFO  [org.hibernate.impl.SessionFactoryObjectFactory] (http-localhost-127.0.0.1-8080-1) Factory name: java:/hibernate/SessionFactory
17:28:36,537 INFO  [org.hibernate.util.NamingHelper] (http-localhost-127.0.0.1-8080-1) JNDI InitialContext properties:{}

17:28:36,546 INFO  [org.hibernate.impl.SessionFactoryObjectFactory] (http-localhost-127.0.0.1-8080-1) Bound factory to JNDI name: java:/hibernate/SessionFactory

现在在使用以下代码进行投射时

Context ctx1=new InitialContext();
Object obj1=ctx1.lookup("java:/hibernate/SessionFactory");
SessionFactoryImpl sess= (SessionFactoryImpl)obj1;

我遇到如下异常

17:28:36,691 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to org.hibernate.impl.SessionFactoryImpl

我使用的依赖是

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>

我的 xml 是

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="java:/hibernate/SessionFactory">
    <property name="connection.url">jdbc:postgresql://localhost:5432/DUMY</property>
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.username">postgres</property>
    <property name="connection.password">postgres</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>        

    <property name="hibernate.session_factory_name">java:/hibernate/SessionFactory</property>          
    .
    .
    .

    </session-factory>
</hibernate-configuration>  

请帮忙。

【问题讨论】:

    标签: hibernate maven jboss7.x sessionfactory


    【解决方案1】:

    我通过将整个数据库访问逻辑放入无状态会话 bean 使其工作,它对我来说工作正常 :)

    【讨论】:

      猜你喜欢
      • 2013-10-21
      • 1970-01-01
      • 2016-01-27
      • 2011-02-01
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多