Hibernate version:3.1.1
Class A has two extended Class A1 and Class A2, and they are mapped to a single table. Class B has OneToMany relationship both Class A1 and Class A2. When i get the collection of these in Class B object. the one have all the Class A object. and another is null.
A.java
Code:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type_", discriminatorType = DiscriminatorType.STRING)
public class A implements Serializable {
protected long id;
public A() {
super();
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
A1.javaCode:
@Entity
@DiscriminatorValue("a1")
public class A1 extends A {
private B b;
public A1() {
super();
}
@ManyToOne
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
A2.javaCode:
@Entity
@DiscriminatorValue("a2")
public class A2 extends A {
private B b;
public A2() {
super();
}
@ManyToOne
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
B.javaCode:
@Entity
public class B implements Serializable {
private long id;
private Set<A1> a1s;
private Set<A2> a2s;
public B() {
super();
}
public void addA1(A1 a1){
if(a1s==null)
a1s=new HashSet<A1>();
a1s.add(a1);
a1.setB(this);
}
public void addA2(A2 a2){
if(a2s==null)
a2s=new HashSet<A2>();
a2s.add(a2);
a2.setB(this);
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@OneToMany(mappedBy="b",cascade = CascadeType.ALL)
public Set<A1> getA1s() {
return a1s;
}
public void setA1s(Set<A1> a1s) {
this.a1s = a1s;
}
@OneToMany(mappedBy="b",cascade = CascadeType.ALL)
public Set<A2> getA2s() {
return a2s;
}
public void setA2s(Set<A2> a2s) {
this.a2s = a2s;
}
}
Test codeCode:
public class TestInheritance extends TestCase{
private static Log log = LogFactory.getLog(TestInheritance.class);
private static EntityManagerFactory entityManagerFactory;
static {
entityManagerFactory = Persistence.createEntityManagerFactory(
"appContext", Collections.EMPTY_MAP);
}
public void testSimple(){
EntityManager em=entityManagerFactory.createEntityManager();
em.getTransaction().begin();
B b=new B();
b.addA1(new A1());
b.addA2(new A2());
em.persist(b);
em.getTransaction().commit();
em.close();
em=entityManagerFactory.createEntityManager();
em.getTransaction().begin();
b=em.find(B.class,b.getId());
Set a1s=b.getA1s();
log.debug(a1s);
log.debug(a1s.size());//error, a1s.size() be 2, but 1.
Set a2s=b.getA2s();
log.debug(a2s); //error ,a2s be null there[/color]
log.debug(a2s.size());
em.getTransaction().commit();
em.close();
}
}
and, the sql is:
Code:
select a1s0_.b_id as b3_1_, a1s0_.id as id1_, a1s0_.id as id0_0_, a1s0_.b_id as b3_0_0_ from A a1s0_ where a1s0_.b_id=?
select a2s0_.b_id as b3_1_, a2s0_.id as id1_, a2s0_.id as id0_0_, a2s0_.b_id as b3_0_0_ from A a2s0_ where a2s0_.b_id=?
note: there is not discriminator field in the sql "where..."., this make logic wrong. To deal with this problem, only make different property name for B property of A1,A2.
but,is this the hibernate bug?