|
Description:
|
Running Hibernate 4.1.8 in a GlassFish container.
My domain model looks something like:
@Entity(name = "parent")
@Table(name = "parent")
public class Parent ... {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "parent_id_seq")
@SequenceGenerator(name = "parent_id_seq", sequenceName = "parent_id_seq", allocationSize = 100)
@Column(name = "id", nullable = false)
private Long id;
@JoinColumn(name = "child", referencedColumnName = "id", nullable = true)
@ManyToOne(optional = true, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Child child;
....
}
@Entity(name = "child")
@Table(name = "child")
public class Child .... {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "child_id_seq")
@SequenceGenerator(name = "child_id_seq", sequenceName = "child_id_seq", allocationSize = 100)
@Column(name = "id", nullable = false)
private Long id;
...
}
I have a block of code that's looking for Child with:
entityManager.find(Child.class, id);
This is returning an javaassist lazy proxy instance, rather than the fully initialized Child instance I'd expect.
Basically, I'd expect getReference(Child.class, id) to give me a lazy proxy reference, but not find().
If I change the code to:
entityManager.clear();
entityManager.find(Child.class, id);
I get a fully initialized Child instance, as I'd expect.
It's worth noting that my Child and Parent classes are not @Cacheable
Digging into the execution that's taking place in this TX, I'm seeing something along these lines (Sorry, I don't have time to put together a test case this week - but I will do my best to make time next week).
Parent p = entityManager.find(Parent.class, 1);
Child c = entityManager.find(Child.class, 20);
If I invoke entityManager.clear(); prior to looking for the Child with id 20, I get the Child object.
It looks like the entitymanager is return cached references for find() instead of cached instances.
|