EclipseLink JPA Master Key with Custom Default

I have a table in my SQL Server database where the primary key field is defined by NEWID() as the default value. It is expected that the client does not need to pass the value of the primary key field, and the SQL server will process it.

When defining my model class in JPA, I must define this ID field with the generation type. I tried the IDENTITY , TABLE and SEQUENCE Generator. Sorry, I get an error message like

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

My contention. XML 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 below

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

Hi @ Joe2013 Not sure if there is still a problem, but when using table generators And you did not specify for Eclipse Link to create a schema based on your object model, you must manually create a table AND also insert rows for the corresponding generators and their initial values. Otherwise, this will not work and you will receive an error message.

+5
source

I have provided custom and standard examples of sequence generators as shown below.

Use custom sequence identifier generator (EclipseLink only)

Define a user sequence class

 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; } } 

Register custom sequence in persistence.xml file

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

Add a sequence entry with a custom sequence name

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

Use default sequence identifier generator (JPA, EclipseLink, Hibernate)

Please enable automatic schema generation or create a table to store the identifier manually when using the Table , Sequence or IDENTITY Identifier Generation.

manual # 1. Enable automatic circuit creation

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

non-automatic # 2. Create a table to store the identifier manually

Identify Consistency in Your Organization

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

SQL script to create a sequence

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

+3
source

Source: https://habr.com/ru/post/1499761/


All Articles