@JoinColumn @Convert [message #1083663] |
Sat, 10 August 2013 09:04 |
Peer Törngren Messages: 14 Registered: July 2009 Location: Sweden |
Junior Member |
|
|
Is there some way to modify the way a @JoinColumn handles foreign keys for null fields? I think I'm looking for something like a @Convert that I can apply to the @JoinColumn, similar to how I can apply @Convert to @Column.
More specifically, what I need to do is to convert a DB field with all spaces to null when reading, and convert a entity field that is null to all spaces when writing, but let the join behave normally in all other cases (i.e. look up and return the entity instance with the referenced id).
New to JPA and EclipseLink, hope there is a simple and obvious answer?
Background:
Trying to join a simple entity (A) to another entity (B) using a plain join and a single field key in a legacy DB.
Problem is that the DB schema states all fields as NOT NULL, using spaces to indicate an absent key. This works just fine when writing native SQL, but I have problems doing this with a JPA mapping. Since this concerns a legacy DB with lots of manual SQL based on these conventions, changing the DB schema is not an option.
Using a plain @JoinColumn seems to work ok when I read entity A, since it fails to find an entity B with all spaces as key (albeit presumably with some cost for trying to look it up), and entity A is loaded with null in the field for entity B.
But when I try to write entity A (insert or update) I get errors complaining about trying to insert null into not nullable field for B in table A.
Simple illustration - READ:
TableA
id, fkB (NOT NULL)
'A1', 'B1'
'A2', ' '
TableB
id
'B1'
Desired result when loading entities from TableA:
a1.b: b1
a2.b: null
Simple illustration - WRITE:
a1.b: b1
a2.b: null
Desired result when writing entities to TableA:
TableA
id, fkB (NOT NULL)
'A1', 'B1'
'A2', ' '
TableB
id
'B1'
[Updated on: Sat, 10 August 2013 09:09] Report message to a moderator
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05239 seconds