|
I am using Hibernate 3.5 JPA and have doubt about specific behavior by this implementation.
We have Parent which has two child with below property
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.LAZY) private List<ChildOne> childOneList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.LAZY) private List<ChildTwo> childTwoList;
And ChildOne with below property for referencing Parent (Assume even ChildTwo with same code as below)
@JoinColumn(name = "PUID", referencedColumnName = "PUID") @ManyToOne private Parent parent; I am persisting parent object using JPA EntityManager.merge(PARENT), at this time I don't have child of this specific parent, however this child table has many records which are NOT related to this parent record. (Linked child record will be created after some time)
parent.setChildOneList(null); parent.setChildTwoList(null); em.merge(parent); What happens is, on EntityManager.merge, it first fire select query with "left outer join" on childs (which fetches all unrelated records) and then apply insert/update on parent record. SQL queries in log like.
Hibernate: select parent0_.PUID as PUID96_1_,childoneli1_.PUID as PUID96_3_,childoneli1_.CHILD_UID as CHILD1_3_,childoneli1_.CHILD_UID as CHILD1_97_0_,childoneli1_.PUID as PUID97_0_ from PARENTT parent0_ left outer join CHILDONE childoneli1_ on parent0_.PUID=childoneli1_.PUID where parent0_.PUID=?|#] Hibernate: insert into PARENTT (PUID) values |#] This is temporarily solved by removing the Cascade.ALL. Now, I may need this in future so, wanted to know any pointers on this behavior by Hibernate JPA implementation. (Annotation Import has all javax.... no Hibernate specific).
If this is what expected from Hibernate JPA then and please point me on how to correct it with Cascade.ALL and without unnecessary "left outer join" select on merge call.
Note: Above env created to replicate our actual table/field scenario. Attached: EAR (along with source) and DB script files.
Steps: 1. Create tables as given in ddl and one time script. 2. Create JNDI -> jdbc/customerhub (pointing to the tables created through scripts 3. Deploy EAR on Glassfish 3.1.x (with Hibernate 3.5) and monitor the logs every 2 minutes.
|