【问题标题】:Could not locate SessionFactory in JNDI在 JNDI 中找不到 SessionFactory
【发布时间】:2014-06-10 06:44:37
【问题描述】:

我知道你会生我的气。但我还是会问。 我尝试了stackoverflow中的每个解决方案来解决问题。但没有解决。 我生成休眠 dao pojos 和 hbm.xml.and 当我尝试用 dao 添加一些东西时,我收到错误“无法在 JNDI 中找到 SessionFactory”。***

KullanicilarHome.java

public class KullanicilarHome {

    private static final Log log = LogFactory.getLog(KullanicilarHome.class);
    private final SessionFactory sessionFactory = getSessionFactory();

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

    public void persist(Kullanicilar transientInstance) {
        log.debug("persisting Kullanicilar instance");
        try {
            sessionFactory.getCurrentSession().persist(transientInstance);
            log.debug("persist successful");
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

和 MainClass.java(测试)

public class MainClass {
    public static void main(String[] args) {

        // TODO Auto-generated method stub

        Kullanicilar user = new Kullanicilar();
        user.setAd("Ergin");
        user.setSoyad("DURAN");
        user.setUniversite("Kxxx");
        user.setBolum("bxx");
        user.setCepTel("5xxxxxx");
        user.setEmail("exxxx");
        user.setVeliTel("55xxxxx");
        KullanicilarHome x = new KullanicilarHome();
        x.persist(user);

    }

和 hbm.cfg.xml

<hibernate-configuration>
    <session-factory name="SessionFactory">
        <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/db</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.session_factory_name">SessionFactory</property>
    </session-factory>
</hibernate-configuration>

和 tomcat 版本:v7.0.50 我再说一遍 在你开始生气之前。我尝试了 stackoverflow 中的所有解决方案。但没有奏效。 ->enter link description here ->enter link description here ->enter link description here ->enter link description here

有同样的问题,那里的解决方案对我没有帮助或不理解那里的解决方案。 请帮帮我.. 我为我糟糕的英语道歉 谢谢***

【问题讨论】:

    标签: hibernate jndi sessionfactory


    【解决方案1】:

    我认为您对 ejb 和 hibernate 感到困惑,我看不出您在哪里尝试将 sessionfactory 设置为 JNDI。您必须执行以下操作

    public class KullanicilarHome {
    
    private static final Log log = LogFactory.getLog(KullanicilarHome.class);
    private final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    .
    .
    .
    .
    

    注意:确保类路径中有 .property 文件。一般是放在WEB-INF文件夹中。

    参考:link

    【讨论】:

    • 感谢您的回答。我按照他们说的做。property 和 hbm.cfg.xml 文件位于同一目录中。 src>main>java/mainclass & hibernate.properties & hibernate.cfg.xml 我得到一个错误,比如:codepaste.net/q3xyzt
    • 如果我将文件(属性和 hbm.cfg.xml)放入 web-inf,我会收到“hibernate.cfg.xml”未找到错误
    • 非常感谢。我最终可以做到。但除此之外,我还必须为每个 blabla.hbm.xml 添加 hibernate.cfg.xml,这很累。再次感谢
    猜你喜欢
    • 2013-04-29
    • 1970-01-01
    • 2012-08-25
    • 2010-12-09
    • 2010-10-14
    • 2011-01-29
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多