java - Hibernate 无法与 Sql Server 2008 一起使用

标签 java hibernate

我是 Hibernate 菜鸟。最近开始学习Hibernate。 我正在遵循教程...但我现在迷路了。我一直在努力解决这个问题...

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)

我查看了 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

最佳答案

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

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

关于java - Hibernate 无法与 Sql Server 2008 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12124222/

相关文章:

java - 刷新引起的延迟初始化错误

hibernate - 将具有 InheritanceType.JOINED 的实体添加到 native 查询

java - 包 org.hibernate 可以从多个模块访问 : hibernate. commons.annotations, hibernate.core

java - hibernate 条件 : Joining table without a mapped association

通过互联网的 Java RMI 连接

java - 在 MongoDB 中插入完整列表

java - 检测到设备正在关闭?

java - 优化插入多个具有嵌入式主键的实体时的 JPA 性能

java - 从浏览器运行小程序时出现 InvocableTargetException

java - 我可以从 spring bean 访问 graphql-context 吗?