Hi,
I'm playing with play, yes it is a JOY to play with it !
I've a little JPA question, when I try to delete a parent entity an
exception has always been thrown.
Here my 2 models (parent: ProductSector / child:
ProductSectorTranslation)
ProductSector.java
package models;
import play.*;
import play.db.jpa.*;
import javax.persistence.*;
import java.util.*;
@Entity(name="product_sectors")
public class ProductSector extends Model {
@OneToMany(mappedBy="productSector", cascade=CascadeType.PERSIST)
public List<ProductSectorTranslation> productSectorTranslations =
new ArrayList<ProductSectorTranslation>();
public transient String name;
public String getName() {
return getName("it");
}
public void setName(String value) {
setName("it", value);
}
public String getName(String locale) {
String result = "";
ProductSectorTranslation pst = null;
try {pst = ProductSectorTranslation.find("product_sector_id
= ? and locale = ?",
this.id, locale).first();}
catch (Exception e) {}
if (pst != null) {
result =
pst.name;
}
return result;
}
public void setName(String locale, String value) {
boolean localeUpdated = false;
for (ProductSectorTranslation pst : productSectorTranslations)
{
if (pst.locale.equals(locale)) {
localeUpdated = true;
pst.name = value;
break;
}
}
if (!localeUpdated) {
ProductSectorTranslation pst = new
ProductSectorTranslation();
pst.locale = locale;
pst.name = value;
pst.productSector = this;
productSectorTranslations.add(pst);
}
}
}
ProductSectorTranslation.java
package models;
import play.*;
import play.db.jpa.*;
import javax.persistence.*;
import java.util.*;
@Entity(name="product_sector_translations")
public class ProductSectorTranslation extends Model {
@ManyToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="product_sector_id")
public ProductSector productSector;
public String locale;
public String name;
}
And here the test:
BasicTest.java
@Test
public void simpleTest() {
ProductSector.deleteAll();
ProductSector ps = new ProductSector();
ps.name = "Test";
ps.save();
assertEquals(
ps.name, "Test");
}
When launched the 1st time the test is ok, when launched the 2nd time
the test throws an exception (it find some data into child relation
ProductSectorTranslation and stop the deletion on parent entity
ProductSector):
1st time:
Test ok, here the log:
12:05:30,391 DEBUG ~ delete from product_sectors
12:05:30,393 DEBUG ~ insert into product_sectors (id) values (null)
12:05:30,393 DEBUG ~ call identity()
12:05:30,394 DEBUG ~ insert into product_sector_translations (id,
locale, name, product_sector_id) values (null, ?, ?, ?)
12:05:30,394 DEBUG ~ call identity()
12:05:30,419 DEBUG ~ select top ? productsec0_.id as id5_,
productsec0_.locale as locale5_, productsec0_.name as name5_,
productsec0_.product_sector_id as product4_5_ from
product_sector_translations productsec0_ where product_sector_id=? and
productsec0_.locale=?
2nd time:
Test wrong: I see:
A javax.persistence.PersistenceException has been caught,
org.hibernate.exception.ConstraintViolationException: could not
execute update query
In /test/BasicTest.java, line 10 :
ProductSector.deleteAll();
and here the log:
12:06:20,354 DEBUG ~ delete from product_sectors
12:06:20,355 WARN ~ SQL Error: -8, SQLState: 23000
12:06:20,355 ERROR ~ Violazione del vincolo di integrità
FK5DE7B8CBD31775D5 tabella: PRODUCT_SECTOR_TRANSLATIONS in statement
[delete from product_sectors]
Translation: "violazione del vincolo di integrità" => "integrity
constraint violation"
How can I delete the parent entity with automatic deletion on child
relation ?
Many thanks in advance...