我的SQL Server数据库中有一个表,其中主键字段定义为NEWID()
作为默认值。期望是客户端不需要传递主键字段值,SQL服务器将处理它。
在JPA中定义我的模型类时,我必须使用生成类型定义此ID字段。我尝试了IDENTITY
,TABLE
和SEQUENCE
生成器。不幸的是我收到错误
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>
我的Model类如下
@Entity
public class OrderHeader implements Serializable {
@Id
@Basic(optional = false)
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="OrderId")
private String orderId;
...
}
答案 0 :(得分:6)
Hi @ Joe2013不确定是否仍然存在问题但是在使用表生成器时并没有为Eclipse Link指定根据对象模型生成模式,您必须手动创建表并插入相应的行发电机及其初始值。 否则它将无法工作,您将收到您提到的错误。
答案 1 :(得分: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)
使用Table
,Sequence
或IDENTITY
ID生成时,请启用自动架构生成或创建用于手动存储ID的表。
选择#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