Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Inserting into a Parent and Child table using IDENTITY

Sorry, just to put this issue to bed, I did figure it out and it was
quite simple.  Basically, here it is:

public class Parent {
    @OneToOne(mappedBy = "parent", cascade = CascadeType.ALL)
    private Child child;
}

public class Child {
    @OneToOne
    @JoinColumn(name="PARENT_ID", nullable = false)
    private Parent parent;
}

I'm using DB2 so this results in an insert into the parent table
followed by an insert into the child table and in between the two I
see this in the logs:

SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1

No idea why it wasn't working earlier,  some forums I read wanted me
to put a @OneToOne(cascade=CascadeType.ALL) in the child table but its
apparently not necessary.

Thanks,
Zarar








On Thu, Feb 12, 2009 at 8:14 AM, James Sutherland <jamesssss@xxxxxxxxx> wrote:
>
> See,
>
> http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Child.27s_id_is_not_assign_from_parent_on_persist.
>
> But in general, I would not recommend using IDENTITY as it does not support
> preallocation.  Use TABLE or SEQUENCE.
>
>
> Zarar Siddiqi wrote:
>>
>> Hi,
>>
>> I have a parent table with an autogenerated primary key and a one to one
>> table which is a child thus contains the parent's primary key.
>>
>> The partent table entity class has the @GeneratedValue(strategy =
>> GenerationType.IDENTITY)
>>
>> When I persist the parent using the em.persist(parent) method, it tries to
>> insert the child as well which is correct behavior.  However, it doesn't
>> have the ID of the parent yet so tries to insert a NULL into the child
>> table
>> which is wrong.  I'm sure this is a fairly common problem, is there a
>> workaround to this?  I know I could get this to work using a different ID
>> generation strategy but is it possible to do this using IDENTITY?
>>
>> Any help is appreciated.
>>
>> Thanks,
>> Zarar
>>
>>
>
>
> -----
> ---
> 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/Inserting-into-a-Parent-and-Child-table-using-IDENTITY-tp21965147p21975799.html
> Sent from the EclipseLink - Users mailing list archive at Nabble.com.
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>


Back to the top