ObjectNotFoundException for an audited optional association. Envers is creating a proxy even when the association is null.

Description

Hi,

I have a Contact entity which has an optional unidirectional 1:1 association with an Address entity. Now, when I'm retrieving a version of Contact which doesn't have an Address from audit tables I can see that Address is not null, instead Envers is creating a proxy for it. When I'm trying to access that Address proxy I'm getting: org.hibernate.ObjectNotFoundException: No row with the given identifier exists

This is my entity class:

@Entity
@Audited
public class Contact {
// some fields

private Address address;

@OneToOne(cascade = { CascadeType.ALL }, orphanRemoval = true)
@JoinColumns( {
@JoinColumn(name = "ID_ADDRESS", referencedColumnName = "ID_ADDRESS"),
@JoinColumn(name = "VER_ADDRESS", referencedColumnName = "VER_ADDRESS") })
public Address getAddress() {
return this.address;
}

Now when I'm retrieving a Contact revision:

AuditReader reader = AuditReaderFactory.get(session);
Contact contact = reader.find(Contact.class, id, revision);
// here address is not null
Address address = contact.getAddress();
// here I'm getting org.hibernate.ObjectNotFoundException
address.getId();

The exception which I'm getting:
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.acme.model.address.Address#com.acme.model.address.AddressPK@143fd2a[
id=<null>
ver=<null>
]]

I have to use composite PK because Address is a legacy table.

I posted this issue on Envers forum: http://community.jboss.org/thread/159906?tstart=0

The issue is reproducible while working with EntityManager and as well with Session.

I attached a TestNG test case for this.

Environment

Hibernate 3.6.0.Final, reproducible on Oracle and H2 with both EntityManager and Session

Status

Assignee

AdamA

Reporter

Andrei Zagorneanu

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

Affirmative

Pull Request

None

backportDecision

None

Components

Affects versions

3.6.0

Priority

Major
Configure