openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simone Gianni <simo...@apache.org>
Subject Re: JoinForeignKeyDeleteAction=restrict not working as expected
Date Tue, 14 Oct 2008 10:49:20 GMT
Hi eggbert,
maybe I'm missing something as well, but being a many to many, doesn't
also the row on the cross table gets deleted? In that case, you are
effectively removing the person from the address and the address from
the person, except for the copy you have in RAM which remains dirty.

The violation would occur if you have a many to one (One person having
many addresses, one address belonging to one Person), with reverse
mapping and without a cascade, and you delete the Person, leaving the
Address with a non existing PERSON_ID that would cause the error.

Could you also post the resulting schema and inspect the content of the
cross table before and after the deletion?

Simone



eggbert wrote:
> 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!
>
>
>
>
>
>   


-- 
Simone Gianni            CEO Semeru s.r.l.           Apache Committer
MALE human being programming a computer   http://www.simonegianni.it/


Mime
View raw message