Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EclipseLink JPA Primary Key with Custom Default Value

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.

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..

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>

My Model class is as below

@Entity
public class OrderHeader implements Serializable {
@Id
@Basic(optional = false)    
@GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="OrderId")
    private String orderId;        
    ...
}
like image 463
Joe2013 Avatar asked Dec 25 '22 21:12

Joe2013


1 Answers

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.

like image 192
iullianr Avatar answered Feb 19 '23 10:02

iullianr