我正在使用hiberante 4.当我尝试创建Factory连接时,我收到此错误。
Testlog4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Failed to create sessionFactory object.org.hibernate.HibernateException: src/hibernate.cfg.xml not found
Test
我的 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.driver_class">org.Postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="connection.username">postgres</property>
<property name="connection.password">test</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.search.default.directory_provider">filesystem</property>
<property name="hibernate.search.default.indexBase">target/indexes</property>
<mapping class="org.mark.dto.UserDetails"/>
</session-factory>
</hibernate-configuration>
我的测试类读起来像这样。
package org.mark.dto;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.mark.usr.UserDetails;
public class HibernateTest {
private static ServiceRegistry serviceRegistry;
private static SessionFactory sessionFactory;
public static void main(String[] args){
UserDetails user= new UserDetails();
user.setUserId(1);
user.setUserName("Test");
System.out.print("Test");
try{
Configuration configuration = new Configuration();
configuration.configure("src/hibernate.cfg.xml");
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session= sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}catch (HibernateException ex) {
System.err.println("Failed to create sessionFactory object." + ex);
} finally {
System.out.print("Test");
}
}
}
我正在使用Hibernate 4.我的配置文件直接在我的src文件夹下。不知道为什么没有被发现。
当path更改为configuration.configure(“hibernate.cfg.xml”);
此错误显示
Testlog4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" Testjava.util.ServiceConfigurationError: org.hibernate.integrator.spi.Integrator: Provider org.hibernate.search.hcore.impl.HibernateSearchIntegrator could not be instantiated: java.lang.NoClassDefFoundError: org/hibernate/search/util/logging/impl/LoggerFactory
at java.util.ServiceLoader.fail(ServiceLoader.java:224)
at java.util.ServiceLoader.access$100(ServiceLoader.java:181)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:377)
at java.util.ServiceLoader$1.next(ServiceLoader.java:445)
at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:236)
at org.hibernate.integrator.internal.IntegratorServiceImpl.<init>(IntegratorServiceImpl.java:53)
at org.hibernate.service.internal.BootstrapServiceRegistryImpl.<init>(BootstrapServiceRegistryImpl.java:80)
at org.hibernate.service.internal.BootstrapServiceRegistryImpl.<init>(BootstrapServiceRegistryImpl.java:57)
at org.hibernate.service.ServiceRegistryBuilder.<init>(ServiceRegistryBuilder.java:76)
at org.mark.dto.HibernateTest.main(HibernateTest.java:28)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/search/util/logging/impl/LoggerFactory
at org.hibernate.search.hcore.impl.HibernateSearchIntegrator.<clinit>(HibernateSearchIntegrator.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:374)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:373)
... 7 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.search.util.logging.impl.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 14 more
使用的罐子:
答案 0 :(得分:2)
最终,文件在IDE中的位置并不重要。所有重要的是文件在执行期间的位置,在最终的包中(在您的情况下可能是生成的jar)。
因此,假设您已经提供的信息,您的src/
文件夹应该成为已执行包(jar)的根。这样,当hibernate.cfg.xml
在src/
中时,它应该在执行期间(在jar的根目录中)。换句话说:
而不是:
configuration.configure("src/hibernate.cfg.xml");
使用:
configuration.configure("hibernate.cfg.xml");
对于错误:
Caused by: java.lang.ClassNotFoundException: org.hibernate.search.util.logging.impl.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
...
我们可以看到类路径中缺少类org.hibernate.search.util.logging.impl.LoggerFactory
。从您已经拥有的罐子来看,可以在此链接中找到丢失的罐子:https://repository.jboss.org/nexus/content/groups/public/org/hibernate/hibernate-search-engine/4.4.0.CR1/hibernate-search-engine-4.4.0.CR1.jar
答案 1 :(得分:0)
您可以使用带有hibernateConfig File参数的configure(File configFile)方法从其他目录(不一定是类路径)加载hibernate.cfg.xml。 (注意,我正在使用hibernate 4.3.7)
优点是,如果您无法访问war文件,但可以访问其他目录中的hibernate文件,例如维护。
像这样:
String hibernatePropsFilePath = "/etc/configs/hibernate.cfg.xml";
File hibernatePropsFile = new File(hibernatePropsFilePath);
Configuration configuration = new Configuration();
configuration.configure(hibernatePropsFile);
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);