-->
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.  [ 8 posts ] 
Author Message
 Post subject: Query on a composite element : is it possible ?
PostPosted: Tue Jul 13, 2004 9:01 am 
Newbie

Joined: Thu Jun 10, 2004 5:37 am
Posts: 19
I'd like to execute a query over an object declared in a composite element.
To illustrate what I mean I take a piece of text in the reference documentation :

<list name="carComponents" table="car_components">
<key column="car_id"/>
<index column="posn"/>
<composite-element class="org.hibernate.car.CarComponent">
<property name="price" type="float"/>
<property name="type" type="org.hibernate.car.ComponentType"/>
<property name="serialNumber" column="serial_no" type="string"/>
</composite-element>
</list>

Here, as you can see, the list maps a table called car_components. In this list I have a composite-element which is represented in Java by an object called CarComponent.

Then I'd like to know if it's possible to do a query like this :
SELECT o FROM CarComponent AS o

Because I tried to do this but it doesn't seems to work : "net.sf.hibernate.QueryException: unexpected token: as [SELECT o FROM HbRefStructVoieV AS o]"

I think it's because the object CarComponent doesn't map an existing table in the relationnal database.

So is it possible to do a query with the object CarComponent ?
And how to do this ?

Thanks,

Nicolas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 9:03 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
select o.carComponent from EntityClass o

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Still a problem for query on composite elements
PostPosted: Tue Jul 13, 2004 9:21 am 
Newbie

Joined: Thu Jun 10, 2004 5:37 am
Posts: 19
select o.carComponent from EntityClass o


What is EntityClass ??

I think you mean this :
<class name="EntityClass" ....


<list name="carComponents" table="car_components">
<key column="car_id"/>
<index column="posn"/>
<composite-element class="org.hibernate.car.CarComponent">
<property name="price" type="float"/>
<property name="type" type="org.hibernate.car.ComponentType"/>
<property name="serialNumber" column="serial_no" type="string"/>
</composite-element>
</list>

</class>


I tried to execute select o.CarComponent from EntityClass o but I get this error : "could not resolve property: CarComponent"...

And also I wonder how it could work because the composite-element doesn't map a table, only the list called "carComponents" a table called "car_components". And this table has several properties which are : car_id and all the properties of the composite-element.

Then I really wonder if it's possible to do this kind of query like this, but I hope :)

Nico


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 9:24 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
select o.carComponents from EntityClass o

or give full mapping file if you want us to read it for you...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: OK but can't access the property of the composite element!?
PostPosted: Thu Jul 15, 2004 10:19 am 
Newbie

Joined: Thu Jun 10, 2004 5:37 am
Posts: 19
select o.carComponents from EntityClass o

-->in fact we just have to add elements...
select elements(o.carComponents) from EntityClass o


OK with this, but I can't access the properties of the composite element CarComponent :

SELECT compositeElem FROM EntityClass o, CarComponent compositeElem
WHERE compositeElem IN elements(o.carComponents)

This query is incorrect for Hibernate :
unexpected token: as [SELECT compositeElem FROM EntityClass o, CarComponent compositeElem...
And it is CarComponent which causes the problem.

It is like if CarComponent is not the kind of object contained in elements(o.carComponents).



Also you ask me to show you my mapping file, so I do this but I'm not sure it will help you :

Below is an extract of my mapping file, after this extrat I wrote an example of query I'd like to execute.

------------
<!-- TABLE REF_VOIE -->
<class name="vdm.administration.referentiel.dao.hbbean.HbRefVoie" table="REF_VOIE">
<id name="id" column="ID_VOIE" type="java.lang.Long">
<generator
class="sopra.twork.dao.hibernate.id.TableParamHiLoGenerator">
<param name="table">"ID_HB_REF"</param>
<param name="column">"ID"</param>
<!-- Pas de g


Top
 Profile  
 
 Post subject: Composite element in Hibernate queries ?
PostPosted: Thu Jul 15, 2004 11:45 am 
Newbie

Joined: Thu Jun 10, 2004 5:37 am
Posts: 19
I post another explanation of this problem on the following page : http://www.hibernate.org/118.html
And I hope somebody will be able to help me.


And on this page use the link "I have a many-to-many association between two tables, but the association table has some extra columns (apart from the foreign keys). What kind of mapping should I use?" to see the example given by hibernate.org.

My post is at the bottom of the page.

Here is the content of my post (you can read it to have another explanation of the problem...) :



When we have many-to-many association between two tables, and the
association table has some extra columns we have to use composite
elements. But how can we handle the composite elements in a Hibernate
query ?

<set name="relationship">
<key column="fk_of_foo"/>
<composite-element class="Relationship">
<property name="multiplicity" type="short" not-null="true"/>
<property name="created" type="date" not-null="true"/>
<many-to-one name="bar" class="Bar" not-null="true"/>
</composite-element>
</set>


In this example the composite element correspond to a Java object
called Relationship (I don't speak about the set name, but about the
attribute class of the composite element).

I tried to make a query like this, but it seems that Hibernate doesn't
permit this :
SELECT o FROM Relationship AS o

But it doesn't work, Hibernate says "unexpected token: as [SELECT o
FROM Relationship AS o]


But if we suppose that we have
<class name="MyClass">
...
<set name="relationship">
<key column="fk_of_foo"/>
<composite-element class="Relationship">
<property name="multiplicity" type="short" not-null="true"/>
<property name="created" type="date" not-null="true"/>
<many-to-one name="bar" class="Bar" not-null="true"/>
</composite-element>
</set>
...
</class>

Then we can do this :
SELECT elements(o.relationship) FROM MyClass AS o

But "SELECT comp FROM MyClass AS o, Relationship AS comp WHERE comp IN
elements(o.relationship)" doesn't work still because of the class
Relationship which seems not to be recognized by Hibernate.

I really need to access Relationship object in a query in order to
make joins with some properties of the composite element.

How can I do ?
Is it possible ?


Thanks,

Nicolas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 16, 2004 9:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
from MyClass cl join cl.components comp where comp.myproperty = 1

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Query on a composite element : solution
PostPosted: Mon Jul 19, 2004 3:51 am 
Newbie

Joined: Thu Jun 10, 2004 5:37 am
Posts: 19
Thank you very much Emmanuel, it's working very well !

So we have to use the key word JOIN in queries if we want to make queries on java objects which reference a composite element.


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