openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From eggbert <starbase1...@gmail.com>
Subject JoinForeignKeyDeleteAction=restrict not working as expected
Date Tue, 14 Oct 2008 10:19:17 GMT

Hello,

I am letting OpenJPA build the schema from the entities, and I have enabled
"ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict" for
the MappingDefaults in my persistence.xml file. However, the
JoinForeignKeyDeleteAction appears to have no effect.

For example, if I have the following two entities...

@Entity
public class Person {    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @ManyToMany
    @JoinTable(name = "person_address",
    joinColumns = {@JoinColumn(name = "person_id",
referencedColumnName="ID")},
    inverseJoinColumns = {@JoinColumn(name = "address_id",
referencedColumnName="ID")})
    private Set<Address> addresses = new HashSet<Address>();
    
    public Person() {        
    }
    
    Collection<Address> getAddresses() {
        return addresses;
    }    
}

and...

@Entity
public class Address {    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @ManyToMany(mappedBy="addresses")
    private Set<Person> people = new HashSet<Person>();
    
    public Address() {        
    }
    
    Collection<Person> getPeople() {
        return people;
    }    
}

Then the following code will NOT throw an exception complaining about a
foreign key violation.

Person p = new Person();
Address a = new Address();
        
em.getTransaction().begin();
em.persist(p);
em.persist(a);
p.getAddresses().add(a);
a.getPeople().add(p);
em.getTransaction().commit();
        
em.getTransaction().begin();
em.remove(a);   //remove an Address that has a Person
em.getTransaction().commit();  //why does this commit?


Shouldn't the JoinForignKeyDeleteAction=restrict prevent this, (i.e,
preventing an entity from being deleted that has an ID in a join table), or
am I totally missing something here.  

Thanks!





-- 
View this message in context: http://n2.nabble.com/JoinForeignKeyDeleteAction%3Drestrict-not-working-as-expected-tp1332128p1332128.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Mime
View raw message