同事们,我有HibernateUtil.class,它返回sessionFactory:
public class HibernateUtil {
private static final Logger LOG = Logger.getLogger(HibernateUtil.class.getName());
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from app.properties
java.util.Properties properties = new Properties();
properties.load(new FileInputStream("src//main//resources//app.properties"));
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml").addProperties(properties);;
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
LOG.info("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
app.properties中有属性:
hibernate.connection.url=jdbc:oracle:thin:@srv.te.loc:1521:DB
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.username=user
hibernate.connection.password=pass
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.current_session_context_class=thread
当我打电话给测试IDE节目时:
02.02.2016 10:37:08.592 INFO ApplicationDAOImpl:53 - Get application status for Application with № 00000000009240
02.02.2016 10:37:08.961 INFO Version:37 - HHH000412: Hibernate Core {5.0.7.Final}
02.02.2016 10:37:08.965 INFO Environment:213 - HHH000206: hibernate.properties not found
02.02.2016 10:37:08.968 INFO Environment:317 - HHH000021: Bytecode provider name : javassist
02.02.2016 10:37:09.105 WARN deprecation:91 - HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
02.02.2016 10:37:09.931 WARN deprecation:91 - HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
02.02.2016 10:37:10.018 INFO Version:66 - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
02.02.2016 10:37:10.171 WARN connections:71 - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
02.02.2016 10:37:10.452 INFO connections:127 - HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@srv-pwcdb01.is.loc:1521:PWCDB]
02.02.2016 10:37:10.454 INFO connections:136 - HHH10001001: Connection properties: {user=user, password=****}
02.02.2016 10:37:10.455 INFO connections:141 - HHH10001003: Autocommit mode: false
02.02.2016 10:37:10.464 INFO DriverManagerConnectionProviderImpl:39 - HHH000115: Hibernate connection pool size: 20 (min=1)
02.02.2016 10:37:10.795 INFO Dialect:156 - HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
02.02.2016 10:37:11.311 INFO ApplicationDAOImpl:65 - Session Is Opened :: true
02.02.2016 10:37:11.312 INFO ApplicationDAOImpl:66 - Session Is Connected :: true
02.02.2016 10:37:11.325 WARN QuerySplitter:124 - HHH000183: no persistent classes found for query class: from com.comp.db.entity.Applications where applicationNumber = :applicationNumber
02.02.2016 10:37:11.332 INFO QueryTranslatorFactoryInitiator:47 - HHH000397: Using ASTQueryTranslatorFactory
org.hibernate.QueryParameterException: could not locate named parameter [applicationNumber]
at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:131)
at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:148)
at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:517)
at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:487)
at ru.com.isbank.db.DAO.ApplicationDAOImpl.getPcoApplicationIBStatus(ApplicationDAOImpl.java:71)
at ru.com.isbank.db.DAO.ApplicationDAOImplTest.testGetPcoApplicationIBStatus(ApplicationDAOImplTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
java.lang.NullPointerException
at ru.com.isbank.db.DAO.ApplicationDAOImpl.getApplicationStatus(ApplicationDAOImpl.java:87)
at ru.com.isbank.db.DAO.ApplicationDAOImplTest.testGetApplicationStatus(ApplicationDAOImplTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
如果我在标准hibernate.cfg.xml中使用相同的属性(url,pass,dialect)并从serviceRegistry
中的new Configuration().configure().buildSessionFactory(serviceRegistry)
中删除HibernateUtil.class
,则相同的测试工作正常。
我的属性文件或HibernateUtil类有什么问题?
hibernate.cfg.xml也是如此:
<?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="connection.pool_size">10</property>
<property name="hibernate.default_schema">defschema</property>
<property name="show_sql">false</property>
<property name="hibernate.connection.autocommit">false</property>
<mapping class="com.comp.db.entity.Applications"/>
<mapping resource="hbconfig/Applications.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
答案 0 :(得分:2)
我认为问题在于:
<key>LSMinimumSystemVersion</key>
<string>10.7.0</string>
为什么要实例化一个新的配置,而不是重用上面几行初始化的旧配置?
更改为
sessionFactory = new Configuration()...
修改强>
只需将属性添加到配置中,而不是使用ServiceRegistry,如下所示:
sessionFactory = configuration.configure().buildSessionFactory(serviceRegistry);
这对我有用。只需进行调试即可确保在加载后正确填充属性。
答案 1 :(得分:0)
首先,你的配置方式Hibernate 5是不正确的。有关详细信息,请参阅https://stackoverflow.com/a/32711654/3405171。
使用属性文件的绝对路径进行非标准属性的配置,可以执行此操作
File propertiesPath = new File("some_path");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
configure().loadProperties(propertiesPath).build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);
请参阅属性作为资源。
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
configure().loadProperties("app.properties").build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);
您需要在类路径中使用app.properties
。