0

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.

1
  • After a discussion with the coworkers found that in fact it's a error in the meta-models.
    – Dhananjaya
    Jan 21, 2015 at 10:18

2 Answers 2

2

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.

1
  • My bad, @ReadOnly is from EclipseLink, edited the question to reflect that
    – Dhananjaya
    Jan 21, 2015 at 7:51
1

The question may be almost 6 years old, but it's still being found today, so I'd like to address another option:

public class Foobar {

  @OneToOne
  @JoinColumn(name="SELF_COLUMN_FOO", referencedColumnName = "FOREIGN_COLUMN_TO_JOIN")
  public Foo foo;

  @OneToOne
  @JoinColumn(name="SELF_COLUMN_BAR", referencedColumnName = "FOREIGN_COLUMN_TO_JOIN")
  public Bar bar;
}

This can be used where SELF_COLUMN is obviously the relevant column in the Foobar table, and FOREIGN_COLUMN_TO_JOIN would be single key in the other table you wish to join.

This will be useful where you want to have two (or more) attributes in a single class, but only one column to join on the foreign DB table. For example: An Employee may have a home phone number, cell number, and a work phone number. All are mapped to different attributes in the class, but on the database there's a single table of phone numbers and id's, and an identifier column, say VARCHAR(1) with 'H' or 'W' or 'C'. The real example would then be...

Tables:

PHONENUMBERS

PHONENUMBER_ID, ACTUAL_NUMBER

EMPLOYEE
ID
HOMENUMBER VARCHAR(12), CELLNUMBER VARCHAR(12), WORKNUMBER VARCHAR(12)

public class Employee {

  @OneToOne
  @JoinColumn(name="HOMENUMBER", referencedColumnName = "PHONENUMBER_ID")
  public Phone homeNum;

  @OneToOne
  @JoinColumn(name="CELLNUMBER", referencedColumnName = "PHONENUMBER_ID")
  public Phone cellNum;

  @OneToOne
  @JoinColumn(name="WORKNUMBER", referencedColumnName = "PHONENUMBER_ID")
  public Phone workNum;
}

As you can see, this would require multiple columns on the Entity's table, but allows you to reference a foreign key multiple times without throwing the 'Multiple writable mappings exist...' that you showed above. Not a perfect solve, but helpful for those encountering the same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.