2016-06-08 85 views
0

喜逢一個我試圖與休眠 我使用MySQL的 小的項目,這是我的hibernate.cfg.xml文件錯誤的定義休眠xml文件

<?xml version='1.0' encoding='utf-8'?> 
<!-- 
    ~ Hibernate, Relational Persistence for Idiomatic Java 
    ~ 
    ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. 
    ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 
    --> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 

    <session-factory> 

     <!-- Database connection settings --> 
     <!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> 
        <property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> --> 

     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password">root</property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

     <!-- SQL dialect --> 
     <property name="dialect"> 
      org.hibernate.dialect.MySQLDialect 
     </property> 

     <!-- Enable Hibernate's automatic session context management --> 
     <property name="current_session_context_class">thread</property> 

     <property name="cache.use_query_cache">true</property> 
     <property name="cache.use_second_level_cache">true</property> 
     <property name="cache.use_structured_entries">true</property> 
     <property name="cache.region.factory_class">org.hibernate.cache.jcache.JCacheRegionFactory</property> 
     <property name="net.sf.ehcache.configurationResourceName">/hibernate-config/ehcache.xml</property> 
     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 

     <mapping resource="hibernate-config/domain/Event.hbm.xml"/> 
     <mapping resource="hibernate-config/domain/Person.hbm.xml"/> 
     <mapping resource="hibernate-config/domain/PhoneNumber.hbm.xml"/> 
     <mapping resource="hibernate-config/domain/Account.hbm.xml"/> 
     <mapping resource="hibernate-config/domain/HolidayCalendar.hbm.xml"/> 

     <mapping resource="hibernate-config/domain/Item.hbm.xml"/> 
<mapping class="com.hamada.hibernate.dto.UserDetails"></mapping> 
    </session-factory> 

</hibernate-configuration> 

這UserDetails.java文件

package com.hamada.hibernate.dto; 

import javax.persistence.Entity; 
import javax.persistence.Id; 

@Entity 
public class UserDetails { 

    @Id 
    private int userId; 
    private String userName; 

    public int getUserId() { 
     return userId; 
    } 

    public void setUserId(int userId) { 
     this.userId = userId; 
    } 

    public String getUserName() { 
     return userName; 
    } 

    public void setUserName(String userName) { 
     this.userName = userName; 
    } 
} 

也這是我的主類

package com.hamada.hibernate; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 

import com.hamada.hibernate.dto.UserDetails; 

public class HibernateTest { 

    public static void main(String[] args) { 
     UserDetails user=new UserDetails(); 
     user.setUserId(1); 
     user.setUserName("Ahmed"); 
     SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory(); 
     Session session=sessionFactory.openSession(); 
     session.beginTransaction(); 
     session.save(user); 
     session.getTransaction().commit(); 
    } 

} 

當我運行該程序我得到這個錯誤

Jun 08, 2016 2:17:06 AM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {5.2.0.Final} 
Jun 08, 2016 2:17:06 AM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Jun 08, 2016 2:17:06 AM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 

Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory] 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) 
    at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.<init>(MetadataBuilderImpl.java:663) 
    at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:127) 
    at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:135) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:654) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:723) 
    at com.hamada.hibernate.HibernateTest.main(HibernateTest.java:15) 
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.cache.jcache.JCacheRegionFactory] as strategy [org.hibernate.cache.spi.RegionFactory] 
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:126) 
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:194) 
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:161) 
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:67) 
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:28) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234) 
    ... 8 more 
+0

也許你錯過了你的類路徑中的一些相關jar – Jens

+0

而這與Spring怎麼樣? –

+0

我認爲我所有的罐子都存在 –

回答

0

JCacheRegionFactory是休眠,JCACHE模塊,它並非是Hibernate核心部分的一部分,你需要把它列入到你的classpath /行家/ gradle這個配置

0

在Hibernate .cfg.xml,我看你已經包含了一些與緩存相關的屬性,即。

<property name="cache.use_query_cache">true</property> 
<property name="cache.use_second_level_cache">true</property> 
<property name="cache.use_structured_entries">true</property> 
<property name="cache.region.factory_class">org.hibernate.cache.jcache.JCacheRegionFactory</property> 
<property name="net.sf.ehcache.configurationResourceName">/hibernate-config/ehcache.xml</property> 

從你的問題,我知道你想測試一個簡單的Hibernate演示項目和,如果你不需要任何的緩存功能,您註釋掉的代碼上述塊。這樣你就不會再收到錯誤了,就像你在這個代碼塊中包含了JCacheRegionFactory並且不存在於你已經使用的jar中。 [在這裏,我假設你已經包括只存在於你可能已經下載的休眠版本zip文件夾中的jar。]

爲了以防萬一,你需要包含緩存代碼,那麼作爲@Arthur已經提到,你需要包含相關的文件。作爲一名初學者,我可以建議在你的hibernate發行版本文件夾中,找到path/hibernate-jcache路徑。有一個名爲hibernate-jcache.gradle的文件。也許,你可以嘗試在你的項目中包含它,看看錯誤是否會停止。

0

謝謝你們,問題解決了。我建立的問題是,應用程序無法識別休眠XML文件,然後我只是把它放在src文件夾中,然後它正在工作