Skip to content

Mapping one DB column to two seperate fields using JPA

I’m developing a code generator that have to generate JPA entities from database meta-model files. These model are from home-brewed modeling system which are being used to generate models other than JPA entities.

In these models some fields are mapping back to same database column. But it seems like JPA does not like that very much. When I try to run generated code I get

Exception [EclipseLink-48] (Eclipse Persistence Services - 2.6.0.v20140809-296a69f): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Multiple writable mappings exist for the field [FACT_INVENT_TRANS_HIST_DM.TRANSACTION_ID].  Only one may be defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[TransactionIdKey-->FACT_INVENT_TRANS_HIST_DM.TRANSACTION_ID]
Descriptor: RelationalDescriptor(InventTransHistFactDM --> [DatabaseTable(FACT_INVENT_TRANS_HIST_DM)])

As I can’t change the models only option left is to make one of those fields read-only. And the JPA entities being generated are only used to read data from database it will not used for writing data. Is there a way to mark some fields as read only or tell EclipseLink that these entities are read only so it does not have to worry about the multiple writable mapping.

I tried using EclipseLink’s @ReadOnly annotation in all entities but it did not help this issue.



There is no @ReadOnly in JPA.

There are however attributes “insertable”/”updatable” that you can set against a field via @Column to effectively do the same.

User contributions licensed under: CC BY-SA
2 People found this is helpful