1

我是休眠菜鸟。我最近开始学习 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>

使用细节.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)

我查看了hibernate jar,但找不到org.hibernate.ejb.test.item

更新

现在我得到这些错误

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

4

1 回答 1

1

我怀疑缺少的课程是您正在学习的教程的一部分。为简单起见,请在 XML 配置中注释掉 class-cache 和 collection-cache 元素,并将 use_query_cache 属性设置为 false。还要注释掉与缓存相关的任何其他属性。

给定更新的错误日志,从 XML 配置中删除 hibernate.cache.region.factory_ 类属性。就像我之前说的,让你的代码在没有缓存配置的情况下工作。

于 2012-08-25T17:47:48.587 回答