1

我正在使用 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
 

使用的罐子: 在此处输入图像描述

4

2 回答 2

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您的类路径中缺少该类。从您已经拥有的 jars 来看,缺少的可以在此链接中找到: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

于 2013-10-12T00:13:50.463 回答
0

您可以使用带有 hibernateConfig File 参数的 configure(File configFile) 方法从不同的目录(不一定是类路径)加载 hibernate.cfg.xml。(注意,我使用的是休眠 4.3.7)

优点是,如果您无权访问 war 文件,但可以访问不同目录中的休眠文件,例如进行维护。

像这样:


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);

于 2014-12-27T10:18:55.367 回答