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<Address> 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

Reply via email to