Whilst attempting to persist something similar to the following structure:
using HSQLDb, loading the objects back out doesn't seem to work as expected. For example:
Create an instance of TestConditionB, tcb, and an instance of TestConditionA, tca setting the appropriate properties on each to identify them later. Set tcb as a member of tca using the setCondtion method. Persist tca then read it back out again. Now when inspecting tcb as a member of tca only the base class properties have been read back out from the database. Checking the contents of the database shows that they have been persisted correctly and trying the same scenario works under Oracle & MySQL. So I am led to believe this is a bug, though I've no idea if it's with Hibernate or HSQLDb.
Here's the code:
Code:
package hsqlbug;
import org.apache.log4j.Logger;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class TestCondition {
private static final Logger log = Logger.getLogger(TestCondition.class);
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public Long getId() {
return id;
}
@SuppressWarnings({"UnusedDeclaration"})
protected void setId(Long id) {
this.id = id;
}
public String getCommonName() {
return commonName;
}
public void setCommonName(String commonName) {
this.commonName = commonName;
}
protected String commonName;
}
Code:
package hsqlbug;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Proxy;
import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.FetchType;
import javax.persistence.Table;
@Entity
@Table(name = "conditions_a")
@Proxy(proxyClass = TestConditionA.class)
public class TestConditionA extends TestCondition {
private TestCondition condition;
private String nameA;
@OneToOne(fetch = FetchType.EAGER, targetEntity = TestCondition.class)
@Cascade(value = {CascadeType.MERGE,CascadeType.PERSIST,CascadeType.SAVE_UPDATE,CascadeType.REFRESH})
public TestCondition getCondition() {
return condition;
}
public void setCondition(TestCondition condition) {
this.condition = condition;
}
public String getNameA() {
return nameA;
}
public void setNameA(String nameA) {
this.nameA = nameA;
}
}
Code:
package hsqlbug;
import org.hibernate.annotations.Proxy;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "conditions_b")
@Proxy(proxyClass = TestConditionB.class)
public class TestConditionB extends TestCondition {
private String nameB;
public String getNameB() {
return nameB;
}
public void setNameB(String nameB) {
this.nameB = nameB;
}
}
and the test code is like this (extract):
Code:
...
TestConditionA conditionA = new TestConditionA();
conditionA.setNameA("my A");
conditionA.setCommonName("common A");
TestConditionB conditionB = new TestConditionB();
conditionB.setCommonName("common B");
conditionB.setNameB("my B");
conditionA.setCondition(conditionB);
session.save(conditionA);
...
TestConditionA conditionA = (TestConditionA) session.load(TestConditionA.class, conditionA.getId());
assertNotNull("expected to find persisted conditionA", conditionA);
TestConditionB conditionB = (TestConditionB) conditionA.getCondition();
assertNotNull("expected to find conditionB", conditionB);
// this test fails against HSQLDB, but not against MySQL
assertEquals("expected persisted conditionB property to be returned", "my B", conditionB.getNameB());
...
This was using Hibernate-3.2.2, Hibernate-Annotations-3.2.1 and hsqldb-1.8.0.7.