Hello,
I'm developing an application using
hibernate-core-3.3.0.SP1 (
hibernate-entitymanager-3.4.0.GA) and
ehcache-core-2.2.0 as a second-level cache provider. I have 2 entities with a bidirectional one-to-one association:
Code:
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Person {
@Id
@GeneratedValue
private Long id;
@OneToOne
private Car car;
// Getters and setters...
}
Code:
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Car {
@Id
@GeneratedValue
private Long id;
@OneToOne(mappedBy = "car")
private Person person;
// Getters and setters...
}
As you can see, Person is the owning side of the association and both entities are configured with Cache annotations.
I've tried to run a code similar to the following:
Code:
// Populating the database
Person p = new Person();
this.em.persist(p);
Car c = new Car();
this.em.persist(c);
p.setCar(c);
this.em.flush();
// Populating the second-level cache
this.em.clear();
this.em.find(Person.class, p.getId());
this.em.find(Car.class, c.getId());
// Clearing the first-level cache
this.em.clear();
// At this point, the second-level cache contains both entities and the first-level cache is empty
// I thought there would be no more SQLs after this point
// But the following line generates SQL (misses the second-level cache)
this.em.find(Person.class, p.getId());
Is the behavior I described expected?
If I delete the "person" attribute from the Car entity (creating a unidirectional one-to-one), the same piece of code runs as expected (no SQLs at the last find).
If I keep the bidirectional association, but modify the Car entity as follows, the same piece of code runs as expected (no SQLs at the last find).
Code:
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Car implements FieldHandled {
@Id
@GeneratedValue
private Long id;
@OneToOne(mappedBy = "car")
@LazyToOne(LazyToOneOption.NO_PROXY)
private Person person;
private FieldHandler fieldHandler;
// Getters and setters...
}
Is the modification above acceptable? Am I missing something?
Thanks a lot,
Tiago