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 {
    @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:


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.


On Thu, Feb 12, 2009 at 8:14 AM, James Sutherland <jamesssss@xxxxxxxxx> wrote:
> See,
> 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
> -----
> ---
> James Sutherland
>  EclipseLink ,
> TopLink
> Wiki: EclipseLink ,
> TopLink
> Forums: TopLink ,
> EclipseLink
> Book: Java Persistence
> --
> View this message in context:
> Sent from the EclipseLink - Users mailing list archive at
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx

Back to the top