-->
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.  [ 2 posts ] 
Author Message
 Post subject: need help with hibernate query
PostPosted: Fri Jul 19, 2013 3:32 am 
Beginner
Beginner

Joined: Thu Nov 12, 2009 1:57 am
Posts: 22
I have following entities.

Code:
public class Guitar { 
 
    private long serialNumber; 
    private int price; 
    GuitarSpec specification; 

 
public class GuitarSpec { 
 
    private long specId; 
    private String builder; 
    private String model; 
    private String type; 
    private int numStrings; 
    private String backWood; 
    private String topWood; 

 
<hibernate-mapping package="com.transcend.music.entity"> 
    <class name="Guitar" table="t_guitar"> 
        <id name="serialNumber" type="long"> 
            <generator class="native"/> 
        </id> 
        <property name="price" type="integer" /> 
        <many-to-one name="specification" column="specification_id" 
                     class="GuitarSpec" cascade="all" not-null="true" /> 
    </class> 
</hibernate-mapping> 
 
<hibernate-mapping package="com.transcend.music.entity"> 
    <class name="GuitarSpec" table="t_guitar_specs"> 
        <id name="specId" type="long"> 
            <generator class="native"/> 
        </id> 
        <property name="builder" type="string" length="100" not-null="false"/> 
        <property name="model" type="string" not-null="false"/> 
        <property name="type" type="string" not-null="false"/> 
        <property name="numStrings" type="integer" not-null="false"/> 
        <property name="backWood" type="string" not-null="false"/> 
        <property name="topWood" type="string" not-null="false"/> 
    </class> 
</hibernate-mapping>


i.e. there is many(Guitar) to one(GuitarSpec) relation. I am fetching a row from GuitarSpec(these rows are unique). Now i want to fetch rows from Guitar(Column specification_id references the specId from GuitarSpec).

I am trying the following but it does not work.

Code:
    Query query = session 
            .createQuery("from GuitarSpec where builder=:builder and model=:model and type=:type and numStrings=:numStrings and backWood=:backWood and topWood=:topWood"); 
            query.setParameter("builder", spec.getBuilder()); 
            query.setParameter("model", spec.getModel()); 
            query.setParameter("type", spec.getType()); 
            query.setParameter("numStrings", spec.getNumStrings()); 
            query.setParameter("backWood", spec.getBackWood()); 
            query.setParameter("topWood", spec.getTopWood()); 
            List results = query.list(); 
             
            GuitarSpec guitarSpec = null; 
            Guitar guitar; 
            Long specId=0L; 
            if (results.size() > 0)   
            { 
                specId = new Long((((GuitarSpec) results.get(0)).getSpecId())); 
                guitarSpec = (GuitarSpec) session.get(GuitarSpec.class, specId); 
            } 
         
            Query queryGuitar = session 
            .createQuery("from Guitar where specification=:specification"); 
            query.setParameter("specification", guitarSpec); 
             
            List guitars = queryGuitar.list();   


I am able to fetch GuitarSpec , but not able to fetch Guitar. How can i fetch Guitar.

Thanks.


Top
 Profile  
 
 Post subject: Re: need help with hibernate query
PostPosted: Tue Jul 23, 2013 4:38 am 
Beginner
Beginner

Joined: Thu Nov 12, 2009 1:57 am
Posts: 22
I was able to solve the problem using following query:

Code:
Query queryGuitar = session.createQuery("from Guitar g join g.specification s where s.specId=:specId"); 
queryGuitar.setParameter("specId", specId); 


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