Issue Type: Bug Bug
Affects Versions: 4.1.8
Assignee: Unassigned
Components: entity-manager
Created: 28/Jan/13 3:57 PM
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);

// The returned p has a Child with the id of 20

// later in the TX, in a totally different class where code scope only has access to the ID of the child, and knows nothing about the parent instance...

Child c = entityManager.find(Child.class, 20);
// c = a lazy reference, just like I'd get from entityManager.getReference(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.

Environment: Hibernate 4.1.8 entitymanager, PostgreSQL 9.1, Oracle JDK 7 64bit linux.
Project: Hibernate ORM
Priority: Major Major
Reporter: Bryan Varner
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira