2012-08-25 63 views
1

我是Hibernate noob。我最近開始學習Hibernate。 我正在學習一個教程......但現在我迷了路。我一直在試圖解決這個問題...休眠不適用於Sql Server 2008

的hibernate.cfg.xml

<?xml version="1.0"?> 

<!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> 

     <property name="hibernate.dialect" > 
org.hibernate.dialect.SQLServer2008Dialect 
     </property> 

     <property name="hibernate.connection.driver_class" > 
com.microsoft.sqlserver.jdbc.SQLServerDriver 
     </property> 

     <property name="hibernate.connection.username" > 
not required 
     </property> 

     <property name="hibernate.connection.password" /> 
     <property name="hibernate.connection.url" > 
jdbc:sqlserver://localhost;databaseName=hibernate;integratedSecurity=false; 
     </property> 

     <property name="hibernate.cache.use_query_cache" > 
true 
     </property> 

     <property name="hibernate.cache.region_prefix" > 
hibernate.test 
     </property> 

     <property name="hibernate.jdbc.use_streams_for_binary" > 
true 
     </property> 

     <property name="hibernate.jdbc.batch_size" > 
0 
     </property> 

     <property name="hibernate.max_fetch_depth" > 
3 
     </property> 

     <property name="hibernate.hbm2ddl.auto" > 
create-drop 
     </property> 

     <property name="hibernate.generate_statistics" > 
true 
     </property> 

     <property name="hibernate.cache.region.factory_class" > 
org.hibernate.testing.cache.CachingRegionFactory 
     </property> 

     <mapping class="com.hibernate.dto.UserDetails" /> 



     <class-cache 
      class="org.hibernate.ejb.test.item" 
      usage="read-write" /> 

     <collection-cache 
      collection="org.hibernate.ejb.test.Item.distributors" 
      region="RegionName" 
      usage="read-write" /> 

     <event type="pre-insert" /> 
    </session-factory> 

</hibernate-configuration> 

UseDetails.java

package com.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; 
    } 




} 

hibernateTest.java

package com.hibernate.dto; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 
import org.hibernate.service.ServiceRegistryBuilder; 

public class HibernateTest { 

    public static void main(String[] args) { 

     UserDetails user = new UserDetails(); 

     user.setUserId(1); 
     user.setUserName("ahmed"); 



     SessionFactory sessionfact = new Configuration().configure() 
       .buildSessionFactory(); 

     Session session = sessionfact.openSession(); 
     session.beginTransaction(); 
     session.save(user); 
     session.getTransaction().commit(); 
     session.close(); 

    } 

} 

現在當我嘗試運行它。我每次都會收到以下錯誤...但我仍然不確定它是什麼意思? 請幫我...

錯誤日誌

Aug 25, 2012 10:27:40 PM org.hibernate.annotations.common.Version <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 
Aug 25, 2012 10:27:40 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.1.6.Final} 
Aug 25, 2012 10:27:40 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Aug 25, 2012 10:27:40 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Aug 25, 2012 10:27:40 PM org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
Aug 25, 2012 10:27:40 PM org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
Aug 25, 2012 10:27:40 PM org.hibernate.cfg.Configuration doConfigure 
INFO: HHH000041: Configured SessionFactory: null 
Exception in thread "main" org.hibernate.MappingException: Cannot cache an unknown entity: org.hibernate.ejb 
    at org.hibernate.cfg.Configuration.applyCacheConcurrencyStrategy(Configuration.java:2212) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1368) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1733) 
    at com.hibernate.dto.HibernateTest.main(HibernateTest.java:25) 

我看着進入休眠罐子,但找不到org.hibernate.ejb.test.item

UPDATE

現在我得到這些錯誤

Aug 25, 2012 11:51:06 PM org.hibernate.annotations.common.Version <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 
Aug 25, 2012 11:51:06 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.1.6.Final} 
Aug 25, 2012 11:51:06 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Aug 25, 2012 11:51:06 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Aug 25, 2012 11:51:06 PM org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
Aug 25, 2012 11:51:06 PM org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
Aug 25, 2012 11:51:06 PM org.hibernate.cfg.Configuration doConfigure 
INFO: HHH000041: Configured SessionFactory: null 
Aug 25, 2012 11:51:06 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 
Aug 25, 2012 11:51:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000115: Hibernate connection pool size: 20 
Aug 25, 2012 11:51:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000006: Autocommit mode: false 
Aug 25, 2012 11:51:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost;databaseName=hibernate;integratedSecurity=false;] 
Aug 25, 2012 11:51:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000046: Connection properties: {user=not required, password=****} 
Aug 25, 2012 11:51:07 PM com.microsoft.sqlserver.jdbc.SQLServerConnection <init> 
SEVERE: Java Runtime Environment (JRE) version 1.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0. 
Aug 25, 2012 11:51:07 PM org.hibernate.dialect.Dialect <init> 
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect 
Aug 25, 2012 11:51:07 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation 
INFO: HHH000422: Disabling contextual LOB creation as connection was null 
Aug 25, 2012 11:51:07 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService 
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 
Aug 25, 2012 11:51:07 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> 
INFO: HHH000397: Using ASTQueryTranslatorFactory 
Exception in thread "main" org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.testing.cache.CachingRegionFactory] 
    at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:410) 
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:264) 
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2279) 
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2275) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1784) 
    at com.hibernate.dto.HibernateTest.main(HibernateTest.java:21) 
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.testing.cache.CachingRegionFactory] 
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:141) 
    at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:393) 
    ... 6 more 
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.testing.cache.CachingRegionFactory 
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$1.findClass(ClassLoaderServiceImpl.java:99) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:264) 
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138) 
    ... 7 more 

我也加了@Table (name = "hibernate")以及@Entity

+0

入住這 - > http://www.roseindia.net/hibernate/hibernateannotations/hibernate-annotations-tutorial.shtml 我得到你沒有把註釋權的感覺。 – AnujKu

+0

你只需要嘗試不同的可能性和調試,並繼續嘗試,直到它得到解決。沒有大的錯誤 – AnujKu

+0

正如我所看到的,您尚未在註釋中定義類的表名。 – AnujKu

回答

1

我懷疑缺失的類是您正在關注的教程的一部分。爲了簡單起見,請在您的XML配置中註釋掉class-cache和collection-cache元素,並將use_query_cache屬性設置爲false。還註釋掉與緩存相關的其他任何屬性。

給定更新錯誤日誌,從XML配置中刪除hibernate.cache.region.factory_類屬性。就像我之前說過的那樣,讓代碼工作而不先緩存配置。

+0

評論讓事情變得更加複雜。現在有三個錯誤:(線程中的異常「main」org.hibernate.HibernateException:無法實例化RegionFactory [org.hibernate.testing.cache.CachingRegionFactory] ​​ ' –

+0

@Ahmed我編輯了我的答案。 – JamesB

+0

我已添加新的錯誤日誌 –

相關問題