Search code examples
jpapersistenceeclipselinkcomposite-primary-key

Composite Primary key in JPA EclipseLink DynamicPersistence


Eclipselink Dynamic Persistence allow multiple primary key by : DynamicTypeBuilder.setPrimaryKeyFields(String primarykeysField...)

But how can i find entity object(instance) by EntityManager.find( Entity Class , Object primaryKey) method to find entity in case of composite primary key(Multiple Primarykey) (because of no IdClass or EmbeddedId) in Dynamic Persistence .

EclipseLink take List of pk in the find() operation but if composite pk key defined in example -


Entity = Person
Fields = username,emailId,firstName,lastName
Composite PK = username,emailId

Class<?> clazz = new DynamicClassLoader(Thread.currentThread().getContextClassLoader()).createDynamicClass("Person");
DynamicTypeBuilder builder = new DynamicTypeBuilder(clazz, null, "Person");
DynamicType dynamicType = builder.getType();
builder.setPrimaryKeyFields(["username","emailId"]);
builder.addDirectMapping("username",String.class,"username");
builder.addDirectMapping("emailId",String.class,"emailId");
builder.addDirectMapping("firstName",String.class,"firstName");
builder.addDirectMapping("lastName",String.class,"lastName");

List list = new ArrayList();
list.add(${username}); //Run time value
list.add(${emailId}); //Run time value

then EnitityManager.find(dynamicType.getJavaClass(),list) will take these arguments , am i right? If i am assuming correct then how will EnitityManager.find() operation will know that List 1st argument is username or emailId pk value (means sequence of composite pk fields value) instead of Map ({username:${username},emailId:${emailId}}) or other DataStucture .


Solution

  • You should be able to use a List with the find() operation.