views:

69

answers:

0

Hi there, I have a jpa mapping class with a polymorphic association like the following:

@Entity
class MyEnt {
    private Long id;
    private MyInterface mi1;
    private MyInterface mi2;
    //...
    @Any(metaColumn = @Column(name = "mi1_type"))
    @AnyMetaDef(idType = "long", metaType = "string", metaValues = { 
    @MetaValue(value = "typeA", targetEntity = TypeA.class),
    @MetaValue(value = "typeB", targetEntity = TypeB.class) 
    })
    @JoinColumn(name = "mi1_id")
    public MyInterface getMi(){
        return mi;
    }

    @Any(metaColumn = @Column(name = "mi2_type"))
    @AnyMetaDef(idType = "long", metaType = "string", metaValues = { 
    @MetaValue(value = "typeA", targetEntity = TypeA.class),
    @MetaValue(value = "typeB", targetEntity = TypeB.class) 
    })
    @JoinColumn(name = "mi2_id")
    public MyInterface getMi(){
        return mi;
    }

    @Id //...
    public Long getId(){
        return id;
    }
}

interface MyEnt {
    Long getId();
}

@Entity
class TypeA implements MyEnt{
    private Long id;
    //...
}

@Entity
class TypeB implements MyEnt{
    private Long id;
    //...
}

I tried to execute an hql query that would retrieve all the polymorphic association value but it seems like that's not possible. Here is the query:

from MyEnt ment inner join fetch ment.mi1

And here is the exception I get:

java.lang.NullPointerException
at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:310)
(...)

I've tried theta style join as well, like the following:

select distinct me from MyEnt me,
       com.plete.path.to.MyInterface interf 
where me.mi1.id = interf.id and me.mi1.class = 'typeA'

that does give me the results but they're not distinct, meaning I have duplicate 'me's on the result.

I've searched in the hibernate forums for this kind of situation and it looks like that hibernate does not attend this kind of situation. Is this doable? Am I missing something?

thanks a lot! cheers!