Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Dynamic JPA: How to handle auto generated primary key in Dynamic Type?
Dynamic JPA: How to handle auto generated primary key in Dynamic Type? [message #815055] Wed, 07 March 2012 02:07 Go to next message
Nabeel Ahmed is currently offline Nabeel Ahmed
Messages: 20
Registered: January 2012
Junior Member
The problem is that when i save Dynamic Type it saved with primary key value 0, doesn't pick next seed value. The following is the mapping for dynamic type.

JPADynamicTypeBuilder personBuilder = new JPADynamicTypeBuilder(classLoader.createDynamicClass(packageName + "Person"), null, "Person");
personBuilder.setPrimaryKeyFields("pid");
personBuilder.addDirectMapping("pid",int.class, "pid");
personBuilder.addDirectMapping("personName", String.class, "personName");

-------------------------------------------
Am i missing anything?


Re: Dynamic JPA: How to handle auto generated primary key in Dynamic Type? [message #815272 is a reply to message #815055] Wed, 07 March 2012 07:54 Go to previous messageGo to next message
Nabeel Ahmed is currently offline Nabeel Ahmed
Messages: 20
Registered: January 2012
Junior Member
I have also tried this,

JPADynamicTypeBuilder personBuilder = new JPADynamicTypeBuilder(classLoader.createDynamicClass(packageName + "Person"), null, "Person");
personBuilder.setPrimaryKeyFields("pid");
DirectToFieldMapping dfm = personBuilder.addDirectMapping("pid",int.class, "pid");
dfm.getDescriptor().setSequenceNumberField(dfm.getField());
dfm.getDescriptor().setSequenceNumberName("SEQUENCE");
dfm.getDescriptor().setSequenceNumberFieldName(dfm.getFieldName());
personBuilder.addDirectMapping("personName", String.class, "personName");

but it also, not work.
Re: Dynamic JPA: How to handle auto generated primary key in Dynamic Type? [message #815502 is a reply to message #815272] Wed, 07 March 2012 13:51 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1016
Registered: July 2009
Senior Member
You need to use the JPADynamicTypeBuilder configureSequencing method.
ie:
personBuilder.configureSequencing("PERSON_SEQ", "pid");

which will use the default SEQUENCE table setup.

You can pass in a sequence object if you do want the default sequencing. An example using native sequencing might be:
        NativeSequence sequence = new NativeSequence();
        sequence.setPreallocationSize(1);
        typeBuilder.configureSequencing(sequence, ENTITY_TYPE + "_SEQ", "SID");


Best Regards,
Chris
Re: Dynamic JPA: How to handle auto generated primary key in Dynamic Type? [message #815957 is a reply to message #815502] Thu, 08 March 2012 04:03 Go to previous message
Nabeel Ahmed is currently offline Nabeel Ahmed
Messages: 20
Registered: January 2012
Junior Member
HI, Thanks for your reply,
I have applied both the approaches you suggested, but it's not working.

For static model i used these two annotations and they work
---------------------------------------------------------------
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
---------------------------------------------------------------

For dynamic model it doesn't work.
---------------------------------------------------------------
JPADynamicTypeBuilder personBuilder = new JPADynamicTypeBuilder(classLoader.createDynamicClass(packageName + "Person"), null, "Person");
personBuilder.setPrimaryKeyFields("pid");
personBuilder.addDirectMapping("pid",int.class, "pid");
personBuilder.addDirectMapping("personName", String.class, "personName");

NativeSequence sequence = new NativeSequence();//new NativeSequence(true);
sequence.setPreallocationSize(1);
personBuilder.configureSequencing(sequence, "SEQ_GEN_IDENTITY", "pid");

The database is same for both the cases, but i got the following error in case of dynamic model:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'SEQUENCE'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196) [sqljdbc4.jar:]
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1454) [sqljdbc4.jar:]
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:388) [sqljdbc4.jar:]
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:338) [sqljdbc4.jar:]
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026) [sqljdbc4.jar:]
---------------------------------------------------------------------------
I have debug the eclipse link code, it uses the native sequence in case of static model, but I end always with the default sequencing strategy, i.e. a table named SEQUENCE, in case of dynamic although i have configured Native sequence.
any help?

[Updated on: Thu, 08 March 2012 08:48]

Report message to a moderator

Previous Topic:Find - returns detached or cached?
Next Topic:MOXy - specify element form in external bindings
Goto Forum:
  


Current Time: Fri Jul 25 05:00:25 EDT 2014

Powered by FUDForum. Page generated in 0.02891 seconds