Hibernate序列生成器的无效对象名称

时间:2015-01-30 10:57:36

标签: java database hibernate sql-server-2012 sequence-generators

我正在开发一个通过Hibernate框架访问运行SQL Server 2012的数据库的应用程序。但是,我无法弄清楚如何使SequenceGenerator注释的实例工作;每当我尝试将新对象实例保存到数据库表时,我都会收到异常。要保存的类如下:

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="MESSAGES")
public class Message implements Serializable {

    private static final long serialVersionUID = -3535373804021266134L;

    @Id
    @Column(name="MESSAGE_ID")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="MESSAGE_GEN")
    @SequenceGenerator(name="MESSAGE_GEN", sequenceName="MESSAGE_SEQ", initialValue=1, allocationSize=1) 
    private Long id;

    @Column(name="TEXT")
    private String text;

    public Message(String text) { 
        this.text = text;
    }

    public Long getId() {
        return id;
    }

    public String getText() {
        return text;
    }

}

一个引发问题的方案可能是以下交易:

import org.hibernate.Session;

public class Manager {

    public static void main(String[] args) {
        Session session = Database.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        Message message = new Message("Hello, World!");
        session.save(message);

        session.getTransaction().commit();

        Database.getSessionFactory().close();
    }

}

这导致以下与标识符序列相关的堆栈跟踪:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:91)
    at org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.java:122)
    at org.hibernate.id.SequenceHiLoGenerator.generate(SequenceHiLoGenerator.java:73)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
    at com.sun.proxy.$Proxy8.save(Unknown Source)
    at dk.radiometer.tests.hibernate.service.Manager.doTransaction(Manager.java:35)
    at dk.radiometer.tests.hibernate.service.Manager.main(Manager.java:16)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'MESSAGE_SEQ'.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:285)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
    ... 19 more

问题似乎与序列的名称有关。我的猜测是我需要手动创建某种类型的序列表供Hibernate使用,但是,我不知道如何做到这一点,一直无法找到与我的环境相关的资源。如果这确实是问题,请将我重定向到一些文档,以便遵循协议。另外,我使用以下程序化Hibernate配置:

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class Database {

    private static final SessionFactory SESSION_FACTORY = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            Configuration cfg = buildConfiguration();
            ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
            SessionFactory sf = cfg.buildSessionFactory(sr);
            return sf;
        } catch (Throwable t) {
            System.err.println("Initial SessionFactory creation failed: " + t);
            throw new ExceptionInInitializerError(t);
        }
    }

    private static Configuration buildConfiguration() {
        return new Configuration()
            .addAnnotatedClass(Message.class)
            .setProperty("hibernate.connection.driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
            .setProperty("hibernate.connection.url", "jdbc:sqlserver://SERVER_NAME;databaseName=DATABASE_NAME;integratedSecurity=true;")
            .setProperty("hibernate.connection.pool_size", "1")
            .setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServer2012Dialect")
            .setProperty("hibernate.current_session_context_class", "thread")
            .setProperty("hibernate.cache.provider_class", "org.hibernate.cache.internal.NoCacheProvider")
            .setProperty("hibernate.show_sql", "true")
            .setProperty("hibernate.hbm2ddl.auto", "update");
    }

    public static SessionFactory getSessionFactory() {
        return SESSION_FACTORY;
    }

}

提前致谢!

1 个答案:

答案 0 :(得分:4)

此问题的解决方案是将目录和架构属性添加到@Table注释中。

@Table(name="<<TableName>>", catalog="<<DatabaseName>>", schema="SchemaName")

e.g

@Table(name="EVENTS", catalog="EVENTMANAGER", schema="DBO")
  1. 目录是指给定表所属的数据库名称 至。
  2. Schema是指容器(使用oracle定义)所针对的 数据库所属,在Microsoft SQL Server中,数据库都是 通常在数据库操作员(DBO)架构下创建。
  3. e.g。 Hibernata Mapping File(hbm)对我有用。

    <class name="Event" table="EVENTS" catalog="EVENTMANAGER" schema="DBO">
        <id name="id" column="EVENT_ID">
            <generator class="native"/>
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE"/>
        <property name="title" column="TITLE" not-null="true"/>
    </class>