我正在使用 hiberante 4。当我尝试创建我的工厂连接时,我收到了这个错误。
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 文件夹下。不知道为什么它没有被检测到。
当路径更改为 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
使用的罐子: