-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: HQL problem when using TABLE_PER_CLASS
PostPosted: Tue Mar 24, 2009 9:13 am 
Newbie

Joined: Tue Feb 05, 2008 11:35 am
Posts: 10
Hello,

I have a problem with TABLE_PER_CLASS inheritance mapping.
When loading only some properties of the parent class, some properties are not loaded as expected. Please see below.

Am I trying to do some unsupported mapping, or is this maybe a bug?

Thanks for your help in advance!
Regards:
Norbi


Version info:
hibernate-core: 3.3.1.GA
hibernate-annotations: 3.4.0.GA
hibernate-entitymanager: 3.4.0.GA
database: oracle 10g

Annotated entities:

Parent.java
Code:
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Parent implements Serializable {
    private int id;

    private int parentValue;

    private int childValue;

    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getParentValue() {
        return parentValue;
    }

    public void setParentValue(int parentValue) {
        this.parentValue = parentValue;
    }

    @Transient
    public int getChildValue() {
        return childValue;
    }

    public void setChildValue(int childValue) {
        this.childValue = childValue;
    }

    @Override
    public String toString() {
        return "[id=" + getId() + ", parentValue=" + getParentValue() + ", childValue=" + getChildValue() + "]";
    }
}


Child1.java
Code:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Child1 extends Parent {
    @Override
    @Column(name = "childValueSomeName")
    public int getChildValue() {
        return super.getChildValue();
    }
}


Child2.java
Code:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Child2 extends Parent {
    @Override
    @Column(name = "childValueSomeOtherName")
    public int getChildValue() {
        return super.getChildValue();
    }
}


SQL:
Code:
create table Child1 (id number(10,0) not null, parentValue number(10,0) not null, childValueSomeName number(10,0), primary key (id));
create table Child2 (id number(10,0) not null, parentValue number(10,0) not null, childValueSomeOtherName number(10,0), primary key (id));

Insert into CHILD1 (ID,PARENTVALUE,CHILDVALUESOMENAME) values (1 1 1);
Insert into CHILD2 (ID,PARENTVALUE,CHILDVALUESOMEOTHERNAME) values (2 2 2);


Loading entire objects works as expected:

HQL: SELECT o FROM Parent o

Result:
[id=1, parentValue=1, childValue=1]
[id=2, parentValue=2, childValue=2]

Generated SQL:
Code:
select parent0_.id as id0_, parent0_.parentValue as parentVa2_0_, parent0_.childValueSomeName as childVal1_1_, parent0_.childValueSomeOtherName as childVal1_2_, parent0_.clazz_ as clazz_ from ( select id, null as childValueSomeOtherName, childValueSomeName, parentValue, 1 as clazz_ from Child1 union all select id, childValueSomeOtherName, null as childValueSomeName, parentValue, 2 as clazz_ from Child2 ) parent0_


But loading the overridden properties does not work:

HQL: SELECT o.id, o.parentValue, o.childValue FROM Parent o

Result:
1 1 null
2 2 2

But the correct result would be:
1 1 1
2 2 2

Generated SQL:
Code:
select parent0_.id as col_0_0_, parent0_.parentValue as col_1_0_, parent0_.childValueSomeOtherName as col_2_0_ from ( select id, null as childValueSomeOtherName, childValueSomeName, parentValue, 1 as clazz_ from Child1 union all select id, childValueSomeOtherName, null as childValueSomeName, parentValue, 2 as clazz_ from Child2 ) parent0_


Querying the subclasses directly works as expected, so SELECT o.id, o.parentValue, o.childValue FROM Child1 o gives the correct result.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 25, 2009 5:25 pm 
Newbie

Joined: Wed Nov 19, 2008 5:55 pm
Posts: 6
Just for the records:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-3642
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2920

Workarounds:
1) use one of the two other Inheritance Strategies or
2) use another database (seems to be a HSQLDB only problem)
3) Fix Hibernate as suggested in HHH-2920 :-(

Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 26, 2009 3:36 am 
Newbie

Joined: Tue Feb 05, 2008 11:35 am
Posts: 10
Thanks for reading my long post and for your suggestions, but... :)

1. We have to deal with a legacy database schema, so our only option is TABLE_PER_CLASS.
2. I saw these issues, but we use Oracle 10g, not HSQLDB.
3. Probably it will be a little complicated to getting know to Hibernate internals :)

--
Norbi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 26, 2009 6:02 pm 
Newbie

Joined: Wed Nov 19, 2008 5:55 pm
Posts: 6
Hi Norbi,

I didn't see that you were using Oracle, sorry.

In this case, I would advice you to file a new bug report (with link to mine), because the solution might be different, and because it is a big difference whether this problems affects a dull database which is never used for production, or whether it breaks Oracle usage.

Does the workaround for HSQLDB (adding "cast(null as <datatype>) as <column>" to the query) work for oracle, too?

No chance to convert your legacy database schema to SINGLE_TABLE or JOINED_SUPERCLASS?

Wolfgang

PS: my last post was slightly ironical, the advice to fix hibernate was no serious suggestion ;-).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 3:36 am 
Newbie

Joined: Tue Feb 05, 2008 11:35 am
Posts: 10
Thanks, Wolfgang.
I filed an issue pointing to this thread: EJB-425


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.