EJB (Enterprise Java Beans) - EJB3 OneToMany Unidireccional Remove Cascade

   
Vista:

EJB3 OneToMany Unidireccional Remove Cascade

Publicado por serlec (1 intervención) el 05/09/2007 17:28:08
Mi pregunta es como hacer una relacion unidireccional con borrado en cascada, lo he intentado todo y me da siempre el mismo error:

Internal Exception: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`lopdata/address_person`, CONSTRAINT `FK_ADDRESS_PERSON_residents_PERSON_ID` FOREIGN KEY (`residents_PERSON_ID`) REFERENCES `person` (`PERSON_ID`))Error Code: 1451
Call:DELETE FROM PERSON WHERE (PERSON_ID = ?)

os dejo las clases para qeu lo mireis a ver si alguien me da una solución:

@Entity
public class Address implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private String street;
private String postcode;
private Set<Person> residents=new HashSet<Person>();

public Address() {}

@Id
@GeneratedValue
@Column(name="ADDRESS_ID")
public Long getId() { return id; }
public void setId(Long id) { this.id=id; }

public String getStreet() { return street; }
public void setStreet(String street) { this.street = street; }

public String getPostcode() { return postcode; }

public void setPostcode(String postcode) { this.postcode = postcode; }

public String toString() {
return "Address("+id+","+street+","+postcode+")";
}

@OneToMany
( cascade = {CascadeType.PERSIST , CascadeType.REMOVE} )
public Set<Person> getResidents() {
return residents;
}

public void setResidents(Set<Person> residents) {
this.residents = residents;
}

}

@Entity
public class Person
{
private Long id;
private String name;
private Address address;

public Person() {}

@Id
@GeneratedValue
@Column(name="PERSON_ID")
public Long getId() { return id; }
public void setId(Long id) { this.id=id; }

public String getName() { return name; }
public void setName(String name) { this.name=name; }

public String toString()
{
return "Person("+name+","+address+")";
}


}

----------------------------------------------------------------------

public class Example {

EntityManagerFactory emf;

public Example()
{
emf=Persistence
.createEntityManagerFactory( "test" );

}

-----------------------------------------------------------------------

public void exercise()
{
Address address=addAddress("ZZ9 ZZZ","Beeblebrox Street");
Address address2=findByPostcode("ZZ9 ZZZ");

Address anotheraddress=addAddress("AA9 AAA","Sesame Street");
Address ax=findByPostcode("AA9 AAA");

System.out.println("Saved address "+address2);
System.out.println("Saved address "+ax);

Person p=new Person();
p.setName("John Doe");
savePerson(p);
address.getResidents().add(p);
updateAddress(address);

Person pa=new Person();
pa.setName("Fred Bloggs");
savePerson(pa);
anotheraddress.getResidents().add(pa);
updateAddress(anotheraddress);

Person pb=new Person();
pb.setName("ALF");
savePerson(pb);
address.getResidents().add(pb);
updateAddress(address);

Person px=findByName("ALF");
System.out.println(px);

Person py=findByName("Fred Bloggs");
System.out.println(py);

Person pz=findByName("John Doe");
System.out.println(pz);

ax=findByPostcode("ZZ9 ZZZ");
System.out.println(ax);
for(Person pi:ax.getResidents()) System.out.println(pi);

ax=findByPostcode("AA9 AAA");
System.out.println(ax);
for(Person pi:ax.getResidents()) System.out.println(pi);

removeAdres(ax);

emf.close();


}

private Person findByName(String name) {

EntityManager em=emf.createEntityManager();
Query q=em.createQuery("select person from Person as person where person.name=:param");
q.setParameter("param",name);
Person p=(Person)q.getSingleResult();
em.close();
return p;
}

private Address findByPostcode(String postcode) {

EntityManager em=emf.createEntityManager();
Query q=em.createQuery("select address from Address as address where address.postcode = '"+postcode+"'");
Address address=(Address)q.getSingleResult();
em.close();
return address;
}

private Person findById(Long id) {

EntityManager em=emf.createEntityManager();
Person p=em.find(Person.class,id);
em.close();
return p;
}

private void savePerson(final Person p) {
EntityManager em=emf.createEntityManager();

EntityTransaction tx=em.getTransaction();
tx.begin();
em.persist(p);
tx.commit();

em.close();
}

private void removeAdres(final Address p) {
EntityManager em=emf.createEntityManager();

EntityTransaction tx=em.getTransaction();
tx.begin();
Set<Person> persons = p.getResidents();
for( Person per : persons ){

em.merge( per );
}
em.remove(em.merge(p));
tx.commit();

em.close();
}

private void updatePerson(final Person p) {
EntityManager em=emf.createEntityManager();

EntityTransaction tx=em.getTransaction();
tx.begin();
em.merge(p);
tx.commit();

em.close();
}

private Address addAddress(String postcode,String street)
{
EntityManager em=emf.createEntityManager();
EntityTransaction tx=em.getTransaction();
tx.begin();
Address address=new Address();
address.setPostcode(postcode);
address.setStreet(street);
em.persist(address);
tx.commit();
em.close();
return address;
}

private Address updateAddress(Address a)
{
EntityManager em=emf.createEntityManager();
EntityTransaction tx=em.getTransaction();
tx.begin();
Address ma=em.merge(a);
tx.commit();
em.close();
return ma;
}



public static void main(String[] args)
{
new Example().exercise();

}


}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder