4

我的 SQL Server 数据库中有一个表,其中将主键字段定义NEWID()为默认值。期望客户端不需要传递主键字段值,SQL Server 会处理它。

在 JPA 定义我的模型类时,我必须使用生成类型定义这个 ID 字段。我试过了,IDENTITY发电机。不幸的是,我收到了一个错误TABLESEQUENCE

Exception Description: Error preallocating sequence numbers.  
The sequence table information is not complete..

我的坚持。XML如下

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="LOB_Webservice" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.xyz.lob.model.jpa.OrderDetail</class>
    <class>com.xyz.lob.model.jpa.OrderHeader</class>
    <shared-cache-mode>NONE</shared-cache-mode> 
    <properties>
        <property name="jboss.as.jpa.providerModule" value="org.eclipse.persistence"/>
        <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:1433;databaseName=LOB_INT" /> 
        <property name="javax.persistence.jdbc.user" value="sa" /> 
        <property name="javax.persistence.jdbc.password" value="*******" /> 
        <property name="eclipselink.logging.level" value="FINE"/>       
        <property name="eclipselink.sharedCache.mode" value="None"/>
        <property name="eclipselink.jdbc.cache-statements" value="false" />  
        <property name="eclipselink.query-results-cache" value="false"/>    
        <property name="eclipselink.logging.exceptions" value="true"/>  
        <property name="eclipselink.weaving" value="static"/>       
    </properties>
</persistence-unit>

我的模型类如下

@Entity
public class OrderHeader implements Serializable {
@Id
@Basic(optional = false)    
@GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="OrderId")
    private String orderId;        
    ...
}
4

2 回答 2

7

嗨@Joe2013 不确定是否仍然是一个问题,但是当使用表生成器并且您没有指定 Eclipse Link 根据您的对象模型生成模式时,您必须手动创建表并插入相应生成器的行及其初始值。否则它将不起作用,您将收到您提到的错误。

于 2014-06-26T19:52:11.710 回答
3

我提供了自定义和默认的序列 ID 生成器示例,如下所示。

使用自定义序列 ID 生成器(仅限 EclipseLink)

定义自定义序列类

package org.phstudy.sequence;

public class MyNewIDSequence extends Sequence implements SessionCustomizer {
    private static final long serialVersionUID = -6308907478094680131L;
    public MyNewIDSequence() {
        super();
    }

    public MyNewIDSequence(String name) {
        super(name);
    }

    public void customize(Session session) throws Exception {
        MyNewIDSequence sequence = new MyNewIDSequence("mynewid");
        session.getLogin().addSequence(sequence);
    }

    @Override
    public Object getGeneratedValue(Accessor accessor, AbstractSession writeSession, String seqName) {
        DataReadQuery query = new DataReadQuery("select NEWID()");
        query.setResultType(DataReadQuery.VALUE);
        return writeSession.executeQuery(query);
    }

    @Override
    public Vector getGeneratedVector(Accessor accessor, AbstractSession writeSession, String seqName, int size) {
        return null;
    }

    @Override
    public void onConnect() { }

    @Override
    public void onDisconnect() { }

    @Override
    public boolean shouldAcquireValueAfterInsert() {
        return false;
    }

    @Override
    public boolean shouldUsePreallocation() {
        return false;
    }

    @Override
    public boolean shouldUseTransaction() {
        return false;
    }
}

在 persistence.xml 中注册自定义序列

<persistence ...>
    <persistence-unit ...>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
        <properties>
            ...
            <property name="eclipselink.session.customizer" value="org.phstudy.sequence.MyNewIDSequence"/>
            ...
    </persistence-unit>
</persistence>

使用自定义序列名称添加序列注释

@Entity
public class CustomSequence {
    @Id
    @GeneratedValue(generator = "mynewid")
    private String id;
    ...
}

使用默认序列 ID 生成器(JPA、EclipseLink、Hibernate)

使用时请启用自动模式生成或创建用于存储 ID 的表TableSequenceIDENTITYID 生成。

选择#1。启用自动模式生成

<persistence ...>
    <persistence-unit ...>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
        <properties>
            ...
            <property name="eclipselink.ddl-generation" value="create-tables"/>
            ...
    </persistence-unit>
</persistence>

选择#2。手动创建用于存储 ID 的表

在您的实体中定义序列

@SequenceGenerator(name="Emp_Gen", sequenceName="Emp_Seq")
@Id @GeneratedValue(generator="Emp_Gen") 
private int getId;

创建序列的 SQL 脚本

CREATE SEQUENCE Emp_Seq
     MINVALUE 1
     START WITH 1
     INCREMENT BY 50 //allocation size

于 2013-08-30T09:25:22.753 回答