Hi,
I'm upgrading an application from Hibernate ORM 4.3.8 to 5.0.2.Final and I'm having some trouble that I think might be related to caching but I'm not entirely certain, so any help would be much appreciated!
I have 2 entities:
Job
Log
Job is linked to Log with the following on getLogs():
Code:
@OneToMany(cascade = {CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "job", fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
Log is linked to Job with the following on getJob():
Code:
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "job_id", nullable = false)
The underlying database is created separately and does not have on cascade delete (could be supported for this use case in this database, but the live application runs under sql server and there are some limitations that prevent cascade being used on all the foreign keys)
In the original test I wrote I have one transaction that:
a) Creates a Job entity and persists it
b) Creates a Log entity and persists it (directly, not by retrieving a Job and adding the Log to the logs collection)
c) Creates another Log entity and persists it
d) Deletes the Job entity (and with the cascade annotations, deletes the associated Log entities)
With Hibernate 4.3.8 this worked, however with Hibernate 5.0.2.Final issuing a delete doesn't delete the associated Log, resulting in an underlying foreign key violation (neither Log entity is deleted). I'm issuing a delete with:
Code:
session.delete(session.get(Job.class, 1)))
If I look at the result of session.get(Job.class,1), the logs collection has not been initialised, making me think it's picking up the object I originally persisted (where the logs collection would have been null)
As part of my testing to figure out what was going on I changed the test to split it into 2 transactions:
1. steps a) and b)
2. steps c) and d)
With this approach I can see Hibernate issuing the delete for the Log entity created in TX 1, but it doesn't delete the Log created as part of TX 2.
I have tried using the following to ignore the cache on all my sessions, but this doesn't seem to change the results at all:
Code:
setCacheMode(CacheMode.IGNORE)
What I'd like to know is:
1. Is this likely due to caching?
2. Does anyone know if caching has changed between 4.3.8 and 5.0.2? I didn't see anything in the documentation about this
3. Is there a way to get my original code to work in 5.0.2 without having to split up into more transactions or to explicitly fetch the Job, add the Log to the Job and then alert Hibernate to the change in Job?
4. If there is a way to solve Q3, is it a dumb idea? (if it's a dumb idea from a performance standpoint, would the necessary change make the performance worse than the default 4.3.8 performance?)