It seems that the associations like one-to-one or one-to-many ignore the inheritance and always fetch the base class. Let's suppose that there are 3 model classes, A, B and C. B extends A, and C has a one-to-one relations with A:
Code:
@Entity
@DiscriminatorColumn(name="dtype")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@Table(name="test_entities_a")
public class EntityA {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String someValue;
// Getters and setters are omitted
}
Code:
@Entity
public class EntityB extends EntityA{
}
Code:
@Entity
@Table(name="test_entities_c")
public class EntityC {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
EntityA entity;
// Getters and setters are omitted
And here's the test case:
Code:
// Code to start the session is omitted
EntityB entityB=new EntityB();
entityB.setSomeValue("test");
EntityC entityC=new EntityC();
entityC.setEntity(entityB);
session.save(entityC);
session.flush();
long id=entityC.getId();
session.close();
// Code to reopen the session is omitted
entityC=(EntityC) session.get(EntityC.class, id);
System.out.println(entityC.getEntity() instanceof EntityB);
Now, the data is saved correctly, the discriminator column in the DB has the "EntityB" value. However the last line prints "false" because the entityC.getEntity() call actually returns an instance of the EntityA class wrapped in a proxy. I have found a couple of similar bug reports,
here and
here, is it the same issue? Is there a known workaround? I tried to evict the associated entity and re-read it by ID in the real code, but I got a non unique object exception.
The issue was discovered on Hibernate-3.6.0-Final, PostgreSQL 9.0, JDK 1.6.