如何修复错误:“INFO:HHH000206:找不到hibernate.properties”?
我试图测试hibernate配置是否正常工作。 我试过但是我收到了一个错误:
INFO: HHH000206: hibernate.properties not found
要做到这一点:我创建:
[1] hibernate配置文件[使用xml]
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306 root explorecalifornia org.hibernate.dialect.MySQLDialect true abc123
[2]一个hibernate实用程序类
public class HibernateUtilities { private static SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; static{ try{ Configuration configuration = new Configuration().configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch(HibernateException exception){ System.out.println("Problem creating session factory"); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void setSessionFactory(SessionFactory sessionFactory) { HibernateUtilities.sessionFactory = sessionFactory; } }
[3]主要方案:
import org.hibernate.Session; public class Program { public static void main(String[] args) { System.out.println("Hibernate"); Session session = HibernateUtilities.getSessionFactory().openSession(); session.close(); } }
但是当我运行程序时,我得到了以下内容:
Hibernate Sep 29, 2013 10:47:15 PM org.hibernate.annotations.common.Version INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} Sep 29, 2013 10:47:15 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.2.5.Final} Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml Problem creating session factory Exception in thread "main" java.lang.NullPointerException at com.simpleprogrammer.Program.main(Program.java:10)
为了解决这个问题,我尝试了Google并应用了我找到的方法。 但我仍然无法解决问题。 有人可以帮帮我吗?
这只是一条INFO消息,告诉您没有hibernate.properties
文件。 此属性文件不是必需的,因此不会阻止您的应用程序工作。
如果您想知道导致SessionFactory
创建失败的原因,您需要将catch块更改为:
catch(HibernateException exception){ System.out.println("Problem creating session factory"); exception.printStackTrace(); }
您应该使用日志框架。
你的问题是创建会话工厂为你的应用程序发生在HibernateUtilities
类中,原因可能是由于,你不能通过sessionRegistry创建一个sessionfactory由hibernate配置类创建它,因为你在hibernate.cfg.xml
注册了你的配置
只需在HibernateUtilities类中替换以下代码即可
`sessionFactory = configuration.buildSessionFactory(serviceRegistry);`
至
`sessionFactory = configuration.buildSessionFactory();`
就我而言,我在tomcat上运行带有JPA注释的Hibernate 5 ,当我改为glassfish 4.1时停止工作
错误:
找不到hibernate.properties
确保: src / main / resources / hibernate.cfg.xml存在
如果你只有hibernate-core的依赖,那我就是在使用hibernate-annotations和hibernate-common-annotations,而且它正在创建冲突。 hibernate 5不需要这两个,我在某处读过。试着删除;)
之后会出现一个新错误:
java.lang.NoSuchMethodError:org.jboss.logging.Logger.debugf(Ljava / lang / String; I)V
原因是最古老的jboss-logging.jar:“YOUR_GLASSFISH_FOLDER / glassfish / modules”
为什么? hibernate 5依赖于最新版本的jboss-logging,即使你在POM文件中声明最新版本, glassfish 4也会使用最旧版本。 其实我用的是:
org.jboss.logging jboss-logging 3.3.0.Final
然后我下载了它并替换模块路径中的旧.jar并重新开始工作,我花了2天时间试图解决这个问题,我希望它能帮助解决未来的问题= D
我用这个链接来帮助我: https : //medium.com/@mertcal/using-hibernate-5-on-payara-cc242212a5d6#.npq2hdprz
在我的情况下,另一个解决方案可以回到最后一个Hibernate 4版本(4.3.11.Final),但在我看来它已经太老了
配置文件必须放在类路径中。 确保hibernate.cfg.xml在class-path中。 由于您的程序是控制台程序,您需要将hibernate.cfg.xml放在src文件夹中。 如果你把它放在其他文件夹中,而不是在Confingure.configure(fileName);
指定它Confingure.configure(fileName);
正如Mr.Kark回答的那样,你必须删除以下内容
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
我想你错过了一些hibernate库(我猜可能是hibernate-tools.jar
)。 所以请检查一下。
无论如何,我看到你使用了hibernate注释,所以请尝试这段代码,看看未找到错误属性会发生什么。
try { AnnotationConfiguration configuration = new AnnotationConfiguration(); sessionFactory = configuration.configure().buildSessionFactory(); }