Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] SecondaryTable mapping question

@SecondaryTable requires that both rows be treated as a single object, in
that they are inserted, updated, deleted together.  You should really define
a @OneToOne given these seem to be independent rows with their own
life-cycle.  You could still have you Person object appear as a single
object by using get/set method to hide/access its IdRecord attributes.

You might be able to hack something together using EclipseLink
preInsert/update events, but you would probably be better off modeling the
data correctly.  You could also define your own database view that makes the
two tables look as one, and handle the views insert/update through triggers.



ljnelson wrote:
> 
> On Wed, Aug 26, 2009 at 2:05 PM, Laird Nelson <ljnelson@xxxxxxxxx> wrote:
> 
>> What if--this is hypothetical for this example, but realistic for other
>> similar ones--what if a row already exists in the id_rec table with, say,
>> a
>> pk of 5, and I insert a new Person whose primary key ("id") is set to 5? 
>> Is
>> EclipseLink smart enough to realize that it should do an "upsert" on the
>> id_rec table?  Or will it try, blindly, to slam two INSERTS home, thus
>> causing the transaction to roll back?
> 
> 
> Turns out it does a blind INSERT.  So does OpenJPA.  Hibernate is the only
> one that handles this scenario flawlessly.
> 
> The question I guess is: should it?
> 
> This is not the case of having a primary record whose secondary record may
> not exist.  That's well documented, and you can work around it (at least
> in
> Hibernate; possibly in EcliseLink as well) with custom annotations or
> descriptor customizers and so on.
> 
> This is, instead, the case of having "too many" primary records.  That is,
> you're guaranteed you're going to have a secondary record, but the related
> primary record might already be there, so a merge() or a persist() of the
> mapped object will need to result in different SQL for each table.
> 
> As far as I can tell, the specification is silent here.  Any thoughts are
> welcome.
> 
> Best,
> Laird
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/SecondaryTable-mapping-question-tp25158380p25274458.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top