-->
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: Named query with join
PostPosted: Mon Dec 04, 2006 2:11 pm 
Newbie

Joined: Sat Nov 04, 2006 3:43 pm
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html

Can someone please show me an example of named query with join. I have two tables and two pojos classes. The first table has a FK of the second table. My first class has a reference to the second class.

Any help is greatly appreciated!
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 3:17 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 11:52 am
Posts: 43
I have a super class, AbstractPerson, that defines a bunch of properties and joins two tables, Person_appearance, and Person_App_Relationship. The subclass, Appearance, has a couple named queries that get instances of the subclass. There's nothing special that has to be done in the named query because of the join. You just use the class name and hibernate handles the join under the covers.

Code:
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping default-access="org.egcrc.cfr.hibernate.MyBasicPropertyAccessor" default-cascade="none">
    <class name="org.egcrc.cfr.common.AbstractPerson" table="PERSON_APPEARANCE" lazy="true" discriminator-value="-1">
        <id name="myId" column="PERSON_PK" type="long" unsaved-value="null">
            <generator class="native"/>
        </id>
        <discriminator column="IS_POPULATION" type="integer"/>
... bunch of properties ...
        <join table="PERSON_APP_RELATIONSHIP" inverse="false">
            <key column="PERSON_FK" not-null="true"/>
            <many-to-one name="myNormalizedRecord" column="NORM_REC_FK" class="org.egcrc.cfr.normalized.NormalizedRecord"/>
            <many-to-one name="myFather" class="org.egcrc.cfr.common.AbstractPerson" column="FATHER_FK" cascade="save-update"/>
            <many-to-one name="myMother" class="org.egcrc.cfr.common.AbstractPerson" column="MOTHER_FK" cascade="save-update"/>
            <many-to-one name="myHusband" class="org.egcrc.cfr.common.AbstractPerson" column="HUSBAND_FK" cascade="save-update"/>            <many-to-one name="myWife" class="org.egcrc.cfr.common.AbstractPerson" column="WIFE_FK" cascade="save-update"/>
            <property name="myIsPopulation_" column="IS_POPULATION"/>
        </join>
    </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping default-access="org.egcrc.cfr.hibernate.MyBasicPropertyAccessor" default-cascade="none">
    <subclass name="org.egcrc.cfr.normalized.Appearance" extends="org.egcrc.cfr.common.AbstractPerson" discriminator-value="0" lazy="true" proxy="org.egcrc.cfr.normalized.Appearance">
        <many-to-one name="myPopulationRecord" class="org.egcrc.cfr.population.CompositePerson" column="MERGED_APPEARANCE_FK" cascade="none"/>
    </subclass>
    <!-- Get all appearances that did not link to another appearance -->
    <query name="getUnlinkedAppearances">
        <![CDATA[from Appearance as appearance where appearance.myId not in (select pl.myAppearance1 from ProposedLink as pl) and appearance.myId not in (select pl.myAppearance2 from ProposedLink as pl)]]>
    </query>
    <!-- Get all appearances that are not associated with a population record. -->
    <query name="getUnmergedAppearances">
        <![CDATA[from Appearance as appearance where appearance.myPopulationRecord is null]]>
    </query>
</hibernate-mapping>


Feel free to ask questions about this example.

Please rate me if this helps. I'm running low on credits ;-).


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.