Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Dynamic JPA

Hi Dennis
Each DynamicType must have a list of fields defined as a primary key. That said, it is not mandatory that the primary keys you define "must" correspond to the primary keys of the database table/view (ideally it should be but it is not mandatory). The information that you provide here is the source of truth for constructing the SQL.
In your example, it seems that the view does not have the column UUID. But since you created the view with columns (name,description), this pair should typically be unique otherwise you would be having duplicate rows in your view. Assuming it is unique, you can as well mark both these columns as comprising the composite primary key of the view.
viewType.setPrimaryKeyFields("name", "description");
You are getting that exception because you must have a mapping defined for any field name that you specify in the DynamicType using the setPrimaryKeyFieldsMethod(). Can you also add the UUID column to the view? Can you not use the table directly instead of the view?
Thanks
Rohit
----- Original Message -----
From: dennis.fuglsang@xxxxxxxxxx
To: eclipselink-users@xxxxxxxxxxx
Sent: Friday, March 11, 2011 9:27:27 PM GMT +05:30 Chennai, Kolkata, Mumbai, New Delhi
Subject: [eclipselink-users]  Dynamic JPA

I have a use case where the “types” in my application are created dynamically so the JPA entity’s Java .class is not present at startup.  To accommodate this functionality, my object model and DB schema store the data in a very abstract form.  My thought was to use EclipseLink’s Dynamic Persistence functionality to create dynamic entity types and then configure their mappings against DB Views which are also dynamically created rather than map ! directly to the underlying general tables.  The dynamic entity types and DB views would only be used to search the data (read only).  Persistence and update of the data would be done using a different set of JPA mappings.

 

The problem I encountered is that since my DB view does not have a primary key,! the primary key constraint being on the backing table, I get exception shown below.  If I attempt to leave off the primary key mapping I get a different exception.  Is what I am trying to do even possible?

 

Exception [EclipseLink-46] (Eclipse Persistence Services - 2.1.0.v20100614-r7608): org.eclipse.persistence.exceptions.DescriptorException

Exception Description: There should be one non-read-only mapping defined for the primary key field [Component.UUID].

Descriptor: RelationalDescriptor(my.co.domain.Component --> [DatabaseTable(Component)])

 

 

        Class<?> dynamicClass = dcl.createDynamicClass(packagePrefix + "Component");

        JPADynamicTypeBuilder compType = new JPADynamicTypeBuilder(dynamicClass, null, dbViewName);

       

        viewType.setPrimaryKeyFields("UUID");

        viewType.addDirectMapping("name", String.class, "NAME");

        viewType.addDirectMapping("description", String.class, "DESC ");

 

!

 

Any suggestions you can provide on the approach or alternative approaches would be greatly appreciated. 

 

Thanks,

 

Dennis Fuglsang

 


Back to the top