@JoinColumn @Convert [message #1083663] |
Sat, 10 August 2013 05:04  |
Eclipse User |
|
|
|
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 05:09] by Moderator
|
|
|
|
|
|
|
|
Re: @JoinColumn @Convert [message #1095894 is a reply to message #1095792] |
Tue, 27 August 2013 13:30  |
Eclipse User |
|
|
|
Chris Delahunt wrote on Tue, 27 August 2013 16:26What product and version are you using? EclipseLink supports lazy basics through fetch groups which require weaving to enhance your entities : http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving
Best Regards,
Chris
I'm using 2.5.0, and I do static weaving that works just fine for regular @JoinColumn mappings mappings (e.g. @ManyToOne).
What do you mean by "through fetch groups" - is there something I need to do to get @Basic to do lazy fetch that I don't have to do for more "sophisticated" mappings?
N.B. I'm doing static weaving, running resource-local POJOs (not server managed), if it matters.
[Updated on: Tue, 27 August 2013 16:01] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.06238 seconds