java - Hibernate:javax.naming.NoInitialContextException(使用Hibernate生成的DAO)

标签 java eclipse hibernate dao

我是 Hibernate 新手。使用 Eclipse indigo 中的 Hibernate 3.0。

这里讨论了这个话题,答案没有帮助,Hibernate: javax.naming.NoInitialContextException (Component Mapping via Annotations) 即我尝试从 session 工厂中删除名称,但仍然收到错误。

我错过了什么吗?有人能帮忙吗?

错误如下:

Feb 6, 2013 3:59:05 PM PatternsHome getSessionFactory
SEVERE: Could not locate SessionFactory in JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:342)
at javax.naming.InitialContext.lookup(InitialContext.java:409)
at PatternsHome.getSessionFactory(PatternsHome.java:26)
at PatternsHome.<init>(PatternsHome.java:21)
at OutputProcessing.saveData(OutputProcessing.java:47)
at OutputProcessing.FPFileOutputWriter(OutputProcessing.java:110)
at OrderPatternFileCreate.main(OrderPatternFileCreate.java:84)
Exception in thread "main" java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
at PatternsHome.getSessionFactory(PatternsHome.java:29)
at PatternsHome.<init>(PatternsHome.java:21)
at OutputProcessing.saveData(OutputProcessing.java:47)
at OutputProcessing.FPFileOutputWriter(OutputProcessing.java:110)
at OrderPatternFileCreate.main(OrderPatternFileCreate.java:84)

hibernate 配置文件:

<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  <property name="show sql">true</property>
  <mapping resource="hibernate_db_mapping.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

DAO 文件由 Hibernate 生成,大纲如下:

public class PatternsHome {

private static final Log log = LogFactory.getLog(PatternsHome.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");
    }
}
    .....
   }

最佳答案

我无法单独完成这项工作。但我使用 HibernateUtil 创建了一个通用 DAO,并使用创建了 sessionFactory

 sessionFactory = new Configuration().configure(new File("hibernate.cfg.xml")).buildSessionFactory();

并使用我的 DAO 访问数据库

Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try{
        transaction = session.beginTransaction();
        session.save(myData);
        transaction.commit();
        System.out.println("Data is Saved");
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        session.close();
    }

这有效。

关于java - Hibernate:javax.naming.NoInitialContextException(使用Hibernate生成的DAO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14741425/

相关文章:

java - 如何知道进程何时退出?

java - 多个 && 语句 - 效率

java - 在 Eclipse 中禁用 TreeViewer 的 Alt+Enter

android.content.ActivityNotFoundException : No Activity found to handle Intent { act=android. intent.action.VIEW

eclipse - Servlet post 请求记录到 Eclipse 控制台

java - 制作新扫描仪时使用相同的输入名称是否有缺点?

android - 让我们解决当前主题中的 "Failed to find style ' mapViewstyle'错误

java - 为什么在 Spring Boot 中检索 mysql View 时出现 "unknown column"错误?

java - 是否可以检测来自另一个项目的实体?

java - Hibernate 可嵌入继承