java - Unable to delete parent JPA entity with @OneToMany relationship using Hibernate -
i've hit problem when using hibernate delete jpa entity has @onetomany
relationship child entity, same code works fine when using eclipselink instead of hibernate jpa provider. annotation on parent entity @onetomany(fetch = fetchtype.eager, cascade = {cascadetype.all}, orphanremoval=true)
when using hibernate attempts set join column on child entity null fails column not allow nulls. when using eclipselink deletes child entities first , deletes parent entity desired behaviour.
my questions are:
- why behaviour different between hibernate , eclipselink? understand
orphanremoval
,cascasetype.remove
features in jpa2 should function same both providers. - is there can change in code allow both eclipselink , hibernate function same when deleting parent entity, without deleting child entities first? 1 constraint code must work both jpa providers.
i've found several similar questions on so, of them either relate jpa 1.0, using depreciated hibernate annotations or suggest setting orphanremoval=true
on @onetomany
annotation.
the version of hibernate i'm using 4.3.10.final , version of eclipselink 2.5.2.
to demonstrate problem i've created following relatively simple example.
the code parent entity follows:
@entity @table(name="house") public class houseentity { @id @column(name="house_id") private int houseid; @column(name="house_name") private string housename; @onetomany(fetch = fetchtype.eager, cascade = {cascadetype.all}, orphanremoval=true) @joincolumn(name = "house_id", referencedcolumnname = "house_id") private list<roomentity> rooms; //getters, setters, equals, , hashcode omitted }
the code child entity follows:
@entity @table(name="room") public class roomentity { @id @column(name="room_id") private int roomid; @manytoone(fetch = fetchtype.eager) @joincolumn(name = "house_id", referencedcolumnname = "house_id") private houseentity house; @column(name="room_name") private string roomname; //getters, setters, equals, , hashcode omitted }
the ejb code used delete parent entity follows:
@stateless public class houseservice { @persistencecontext private entitymanager em; public void deletehouse(int houseid) { houseentity houseentity = em.find(houseentity.class, houseid); em.remove(houseentity); } }
in hibernate following sql statement executed against database update room set house_id=null house_id=?
fails ora-01407: cannot update ("test"."room"."house_id") null
in eclipselink sql statement delete room (house_id = ?)
executed first , sql statement delete house (house_id = ?)
executed.
try following
@entity @table(name="house") public class houseentity { @id @column(name="house_id") private int houseid; @column(name="house_name") private string housename; @onetomany(fetch = fetchtype.lazy, mappedby = "house", cascade = cascadetype.all) private list<roomentity> rooms; //getters, setters, equals, , hashcode omitted } @entity @table(name="room") public class roomentity { @id @column(name="room_id") private int roomid; @manytoone(fetch = fetchtype.eager) @joincolumn(name = "house_id", referencedcolumnname = "house_id", nullable = false) private houseentity house; @column(name="room_name") private string roomname; //getters, setters, equals, , hashcode omitted }
Comments
Post a Comment