本文介绍了java.lang.IllegalStateException:无法在JNDI中找到SessionFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,我在使用Hibernate和JSF时遇到了上述异常,过去我多次看到它,根本原因就是这样< session-factory name =sessionFactory> ; 所以我删除了名称并更改了生成的用于创建SessionFactory的代码:

  protected SessionFactory getSessionFactory(){
try {
return(SessionFactory)new InitialContext()
.lookup(SessionFactory);
} catch(Exception e){
log.error(在JNDI中找不到SessionFactory,e);
抛出新的IllegalStateException(
无法在JNDI中找到SessionFactory);
}
}

至:

  protected SessionFactory getSessionFactory(){
try {
return new Configuration()。configure(hibernate.cfg.xml)。buildSessionFactory ();
} catch(Exception e){
log.error(在JNDI中找不到SessionFactory,e);
抛出新的IllegalStateException(
无法在JNDI中找到SessionFactory);


$ / code>

它和我一起工作得很好,但这一次我没有解决方案,你知道问题出在哪里吗?


$ b

hibernate-cfg.xml

 < 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>根< / property>
< property name =hibernate.connection.url> jdbc:mysql:// localhost:3306 / GUNO< / property>
< property name =hibernate.connection.username>根< / property>
< property name =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / property>
< property name =hibernate.search.autoregister_listeners> false< / property>
< / session-factory>


解决方案

对不起,这个,但我认为,因为这没有标记为答复,也许它会好的。其他人可能已经成为与我一样的陷阱的牺牲品。我最近在合并了一些HBM之后遇到了类似的问题。后来,由于HBM文件中的重复类映射导致JNDI / SessionFactory服务无法启动,因此我遇到了有关JNDI和sessionFactory查找失败的问题。



所以,作为一个开始 - 确保你没有在映射文件中声明重复类:)

这可能是也可能不是看这个问题的人需要的东西,但这是我的问题:)

Good evening, i get the above exception when using Hibernate with JSF, i saw it many times in the past and the root cause was that like this <session-factory name="sessionFactory"> so i removed the name and change the generated code for creating the SessionFactory from that:

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");
    }
}

to that:

protected SessionFactory getSessionFactory() {
    try {
        return new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
    } catch (Exception e) {
        log.error("Could not locate SessionFactory in JNDI", e);
        throw new IllegalStateException(
                "Could not locate SessionFactory in JNDI");
    }
}

it was working fine with me, but this time i have no solution to it, do you know where the problem resides?

the hibernate-cfg.xml

<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">root</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/GUNO</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.search.autoregister_listeners">false</property>
</session-factory>
解决方案

Sorry to necro this, but I thought that since this wasn't marked as answered perhaps it would be OK. Others may have fallen victim to the same pitfall as I.

I recently ran into similar problems after merging some HBM's. Subsequently, I had issues regarding JNDI and sessionFactory look-up failures due to duplicate class mappings in the HBM files causing the JNDI/SessionFactory service to be unable to start up..

So, as a start - make sure you are not declaring duplicate classes in your mapping files :)

This may or may not be what someone looking at this question needs, but it was my issue :)

这篇关于java.lang.IllegalStateException:无法在JNDI中找到SessionFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:56