-->
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.  [ 9 posts ] 
Author Message
 Post subject: HQL and inheritance
PostPosted: Tue Dec 13, 2005 9:24 am 
Newbie

Joined: Tue Dec 13, 2005 9:05 am
Posts: 6
Hi everyone.

Hibernate version:

2.1.8

Mapping documents:

<class
name="mypackage.PropertyValue"
table="cm_property_value"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="id"
type="java.lang.Long"
>
<generator class="native"/>
</id>

<joined-subclass
name="mypackage.PropertyValueBoolean"
table="cm_property_value_boolean"
dynamic-update="false"
dynamic-insert="false"
>

<key
column="id"
/>

<property
name="value"
type="java.lang.Boolean"
update="true"
insert="true"
>
<column
name="value"
sql-type="boolean"
/>
</property>

</joined-subclass>

</class>

Java classes:

package mypackage;

public abstract class PropertyValue extends BaseObject {

public Long getId() {
return id;
}

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

public abstract Object getValue();

public abstract void setValue(Object value);

}

package mypackage;

public class PropertyValueBoolean extends PropertyValue {

private Boolean value;

public Object getValue() {
return value;
}

public void setValue(Object value) {
this.value = (Boolean)value;
}

}

Name and version of the database you are using:

PostgreSQL 8.0

"value" is defined only at subclass level. If I try to access the "value" field of PropertyValue class in HQL (i. e., "select p.value from mypackage.PropertyValue p where p.id = ?"), Hibernate complains that "value" is not a mapped property. Obviously, I want to access the "value" field of the subclass. How should I do it?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 10:53 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
don't confuse yourself with dot notation, just do a regular sql call. I say sql because hql tends to work better when returning objects.

Code:
select value from <table name> where id=?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 11:43 am 
Newbie

Joined: Tue Dec 13, 2005 9:05 am
Posts: 6
kochcp wrote:
don't confuse yourself with dot notation, just do a regular sql call. I say sql because hql tends to work better when returning objects.

Code:
select value from <table name> where id=?


Hi, thanks for your answer. For sake of simplicity, I put just one (the simplest) of PropertyValue's subclasses. In the real scenario, some subclasses store in "value" rather complex objects (collections), so HQL would be very useful.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 11:58 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Sorry, I didn't look at anything but your query at first. What you are doing here is making it very confusing. In order to query for anything using HQL, it must be in a mapping file. Therefore, you must make a separate mapping file for each class (extending mapping files does not work as well as extending java classes.)

So, each mapping file should represent exactly a table in your database. If you want to extend classes on top of one of these that is ok, so long as you are adding some business logic to the class; you cannot add more fields as these would not be accessible by HQL.

You'll need to work on your mapping files a little bit first before proceeding. Post a description of your DB (which classes should have collections of what, etc) and I might be able to help you out.

oh, and in your HQL query, you do not have to precede the class name with the package name.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 12:55 pm 
Newbie

Joined: Tue Dec 13, 2005 9:05 am
Posts: 6
kochcp wrote:
So, each mapping file should represent exactly a table in your database.


And they do (I'm using the table-per-subclass strategy). The problem is that only subclasses of PropertyValue have a "value" property. In the Java class, I have getValue and setValue methods marked as abstract. What I would need is the equivalent in the mapping files, a way to tell Hibernate "this property is abstract in the superclass (so I could query it), and you have to retrieve the value defined in the concrete class". I tried to make a dummy "value" property in PropertyValue, but didn't work.

kochcp wrote:
You'll need to work on your mapping files a little bit first before proceeding. Post a description of your DB (which classes should have collections of what, etc) and I might be able to help you out.


The mapping files are OK (this app has been working for months), and if I retrieve the full object (select p from PropertyValue p where p.id = ? and ...), there is no problem (the right concrete subclass is retrieved, including its "value" property).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 1:02 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
in that case, you should be able to just query the subclass directly, not the abstract class

in HQL
Code:
from PropertyValueBoolean where value=:value


sorry if I was unable to understand what you had posted, the lack of formatting made it difficult to read

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 1:19 pm 
Newbie

Joined: Tue Dec 13, 2005 9:05 am
Posts: 6
kochcp wrote:
in that case, you should be able to just query the subclass directly, not the abstract class

in HQL
Code:
from PropertyValueBoolean where value=:value



Well, the actual query is pretty complex (I tried to oversimplify the example by ommitting details but instead I'm afraid that I've overcomplicated id :) ). It looks like this (it's also simplified):

Code:
SELECT b.id, b.name, propertyValue.value
FROM BasicContent b left outer join b.propertyValues propertyValue
WHERE (condition)


BasicContent has a one-to-many relationship (a list mapping) with PropertyValue. Each of the elements of that list is of one type, so I can't explicitly put its class name in the query.

kochcp wrote:
the lack of formatting made it difficult to read


Sorry, I should have used code tags.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 5:00 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
have you tried forming your HQL query like this

taken from
http://www.hibernate.org/hib_docs/v3/re ... oins-forms

Code:
select new list(mother, offspr, mate.name)
from DomesticCat as mother
    inner join mother.mate as mate
    left outer join mother.kittens as offspr

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 5:48 am 
Newbie

Joined: Tue Dec 13, 2005 9:05 am
Posts: 6
kochcp wrote:
have you tried forming your HQL query like this

taken from
http://www.hibernate.org/hib_docs/v3/re ... oins-forms

Code:
select new list(mother, offspr, mate.name)
from DomesticCat as mother
    inner join mother.mate as mate
    left outer join mother.kittens as offspr


I don't think that is useful in this case, because I don't want to retrieve the full PropertyValue objects (offspr objects in your example), and I want to be able to filter the query using the "value" property.

I've been searching the forums (not that I hadn't done it before), and I found this:

http://forums.hibernate.org/viewtopic.p ... 3e18cb4aa8

So it seems this feature is not supported in Hibernate 2, but does anyone know if it's supported in Hibernate 3? I could consider an upgrade.

Thanks kohcp for all of your replies.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.