【问题标题】:Hibernate can read the content of hibernate.cfg.xml but still give errors?Hibernate 可以读取 hibernate.cfg.xml 的内容但仍然报错?
【发布时间】:2014-01-29 12:22:21
【问题描述】:

我知道这是一个老问题,但它确实让我陷入了困境。

我接受了使用classLoader获取inputsream的建议,代码如下:

 public static Session getSession() {
    if (sessionFactory == null) {
        InputStream xmlInputStream = 
                ResourceUtil.getInputStream(HibernateDBUtil.class, CFG_XML);
        Scanner scanner = new Scanner(xmlInputStream);
        //just a test I can read the xml file ,print content here
        while(scanner.hasNext()) System.out.println(scanner.nextLine());
        scanner.close();
        //reget the stream
        xmlInputStream = 
                ResourceUtil.getInputStream(HibernateDBUtil.class, CFG_XML);
        Configuration cfg = new Configuration();
        cfg.addInputStream(xmlInputStream).configure();
        // version 4.3 's way to config
        StandardServiceRegistryBuilder builder = 
                new StandardServiceRegistryBuilder().applySettings
                (cfg.getProperties());  
        StandardServiceRegistryImpl registry = 
                (StandardServiceRegistryImpl) builder.build();  

        sessionFactory = cfg.buildSessionFactory(registry);  
    }
    return sessionFactory.openSession();
}
private static SessionFactory sessionFactory;
private static final String CFG_XML = "properties/hibernate.cfg.xml"; 

我确认 ResourceUtil.getInputStream 工作正常,因为我可以打印 hibernate.cfg.xml 的内容,但为什么仍然给我错误:

更新: 完整的堆栈跟踪:

信息:HHH000412:休眠核心 {4.3.0.Final} 2014 年 1 月 10 日晚上 11:33:43 org.hibernate.cfg.Environment

信息:HHH000206:找不到 hibernate.properties 2014 年 1 月 10 日 11:33:43 PM org.hibernate.cfg.Environment buildBytecodeProvider

信息:HHH000021:字节码提供程序名称:javassist 2014 年 1 月 10 日 11:33:44 PM org.hibernate.cfg.Configuration 配置信息:HHH000043: 从资源配置:/hibernate.cfg.xml 2014 年 1 月 10 日晚上 11:33:44 org.hibernate.cfg.Configuration getConfigurationInputStream

信息:HHH000040:配置资源:/hibernate.cfg.xml 异常 在线程“主”org.hibernate.HibernateException:/hibernate.cfg.xml 找不到 org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)atorg.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2093) 在 org.hibernate.cfg.Configuration.configure(Configuration.java:2074) 在 org.hibernate.cfg.Configuration.configure(Configuration.java:2054) 在 cn.edu.gdut.utils.HibernateDBUtil.getSession(HibernateDBUtil.java:49) 在 cn.edu.gdut.utils.HibernateDBUtil.listBasicElement(HibernateDBUtil.java:25) 在 cn.edu.gdut.utils.HibernateDBUtil.main(HibernateDBUtil.java:20)

【问题讨论】:

  • 请提供完整的堆栈跟踪。
  • 谢谢,查看完整的堆栈。我尝试使堆栈文本看起来更好但失败了,希望不会让你不高兴。
  • hibernate好像找不到你的hibernate.cfg.xml。尝试使用 CFG_XML 作为路径参数创建一个文件,并检查方法 getAbsolutePath() 将导致的位置。也许你的根目录有误?
  • 谢谢。抱歉回复晚了。其实,在上面的代码中,我可以得到xmlInputstream的内容(看scanner,我可以读到内容),说明没有错。我用一个在 ResourceUtil 类中加载资源的类加载器,它可以与其他部分正常工作,例如图标和属性文件。但是 Hibernate 就是找不到 xmlInputStream,为什么?

标签: java hibernate


【解决方案1】:

我认为,文件在 IDE 中的位置并不重要。重要的是文件在执行期间在最终包中的位置。

因此,假设您的properties/ 文件夹应该成为执行包的根目录。这样,由于hibernate.cfg.xml 在properties/ 中,那么它应该在执行期间位于根目录中。您可以尝试使用

private static final String CFG_XML = "hibernate.cfg.xml";

更新

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

在此之下,您的代码将继续。在hibernate.cfg.xml 我提到了<mapping resource="pkg/name/of/mapping/file/file.hbm.xml" />

【讨论】:

  • 谢谢。我之前试过了,但是不行。也许我需要更仔细地阅读官方文档以获得帮助。
  • @xiaowang 据我所知,Configuration 类的addInputStream() 用于从 InputStream 读取映射,而不是读取休眠配置文件。要么您必须使用setProperty() 设置休眠的不同属性,例如hibernate.dialect 等,要么使用SettingsFactory 传递cfg 文件
  • 我同意更新。如果你想使用cfg.xml文件,那么你必须使用上面提到的代码。否则你可以使用setProperty()从代码中设置休眠配置并使用@传递hbm inputStream 987654334@到Configuration
  • 谢谢,我阅读了[questions/8830357]并阅读了源代码,明白了。我的结论是不要混合配置文件hibernate.cfg.xml和xxx.hbm.xml映射文件。并且我们在使用Hibernate时最好让javadoc工作,否则会引起麻烦。
  • 是的,最好根据部署使用hibernate.cfg.xml,因为如果您通过代码完成了hibernate的所有配置,并且在部署后您想更改hibernate的一些设置,那么您必须重新部署/Hotdeploy `.class' 文件...我认为它比只编辑 xml 文件更好..
猜你喜欢
  • 2013-03-14
  • 2012-07-11
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多