具有自定义默认值的EclipseLink JPA主键 [英] EclipseLink JPA Primary Key with Custom Default Value
问题描述
我在SQL Server数据库中有一个表,其中的主键字段定义为 NEWID()
作为默认值。期望客户端不需要传递主键字段值,SQL Server将对其进行处理。
I have a table in my SQL Server database where the primary key field is defined with NEWID()
as the default value. The expectation is client need not pass the primary key field value and the SQL server will handle it.
在JPA定义模型类时,我必须使用生成类型定义此ID字段。我尝试了 IDENTITY
, TABLE
和 SEQUENCE
生成器。不幸的是,我得到一个错误,因为
While defining my model class at JPA I have to define this ID field with a generation type. I tried IDENTITY
, TABLE
and SEQUENCE
Generator. Unfortunately I am getting an error as
Exception Description: Error preallocating sequence numbers.
The sequence table information is not complete..
我的持久性。 XML如下
My Persistence. XML is as below
<?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;
...
}
推荐答案
嗨@ Joe2013不确定是否仍然存在问题,但是在使用表生成器时,并且您没有为Eclipse Link指定基于对象模型生成架构的方式,则必须手动创建表,并为相应的生成器插入行,并它们的初始值。
否则它将不起作用,并且您会收到提到的错误。
Hi @Joe2013 Not sure if is still an issue but when using Table Generators AND you did not specified for Eclipse Link to generate the schema based on your object model, you must manually create the table AND also insert the rows for the corresponding generators and their initial values. Otherwise it will not work and you will get the error you mentioned.
这篇关于具有自定义默认值的EclipseLink JPA主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!