openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Miłosz Tylenda <mtyle...@o2.pl>
Subject Re: Inconsistent behaviour with Bi-directional One to Many Mapping.
Date Thu, 02 Apr 2009 07:29:08 GMT
Srini,

I think you need to specify the @ForeignKey annotations on your entities. You will find more
details in the manual:

http://openjpa.apache.org/builds/1.2.1/apache-openjpa-1.2.1/docs/manual/manual.html#ref_guide_mapping_jpa_fk

Regards,
Milosz


> Hi Paul,
>  Thanks for your suggestion.. But even after implementing your
> suggestion I get the same result..
> It persists once and next time it fails.. its inconsistent..
> 
> I made the following changes to the mapping..
> @ManyToOne (optional=false, CascadeType.ALL)
> 
> Is this a defect?
> 
> Best regards,
> Srini
> 
> On Wed, Apr 1, 2009 at 11:20 PM, Paul Copeland (via Nabble)
>  wrote:
> > Does it make any difference if you put the CascadeType.ALL on both sides?
> >
> > Does it make any difference if you put optional=false on the ManyToOne ?
> >
> > @ManyToOne (optional=false, CascadeType.ALL)
> >
> > On 4/1/2009 10:29 AM, srini.krish wrote:
> >> Hi ,
> >>  I am trying to create a bidirectional one to many mapping and seeing
> >> inconsistent behaviour. If I enable trace persistence succeeds and if I
> >> disable it, it fails. The failure is due to foreign key violation. I think
> >> its due to the order of the execution of insert statements. The dependent
> >> table is inserted with rows first.
> >>
> >> Here is the outline of mapping I have done.
> >> (A person can have one or more address. Each address has an unique id and
> >> a person associated with it)
> >> public class Person {
> >>     @Id
> >>     private int Id;
> >>
> >>     @Basic
> >>     private String Name;
> >>
> >>     @OneToMany (cascade=CascadeType.ALL, mappedBy="person")
> >>     @OrderBy
> >>     private Collection addresses;
> >> }
> >>
> >> public class Address {
> >>     @Id
> >>     private int id;
> >>
> >> @ManyToOne
> >> @JoinColumn(name="PersonId", nullable=false)
> >> private Person person;
> >> }
> >>
> >> I try the following code
> >>
> >>  //Create new person
> >> // Create new addresses(2)
> >> //To each address add the same person.
> >> //To the person add addressCollection.(size 2).
> >>
> >>         em.getTransaction().begin();
> >>         em.persist(person);
> >>         em.getTransaction().commit();
> >>
> >> When I do this, sometimes person is first persisted and address is next.
> >> This case passes. (This happen when trace is on).
> >>
> >> The other times, address is persisted first. This fails for want of person
> >> id. (This happens when trace is at Info level).
> >>
> >> Can anybody please help me, if I am missing something?
> >>
> >> Best Regards,
> >> Srini
> >>
> >
> >
> > ________________________________
> > This email is a reply to your post @
> > http://n2.nabble.com/Inconsistent-behaviour-with-Bi-directional-One-to-Many-Mapping.-tp2570464p2570589.html
> > You can reply by email or by visting the link above.
> >
> >
> 
> -- 
> View this message in context: http://n2.nabble.com/Inconsistent-behaviour-with-Bi-directional-One-to-Many-Mapping.-tp2570464p2573585.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> 

Mime
View raw message