-->
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: How to create a simple join in POJOs
PostPosted: Mon Jul 16, 2007 5:27 am 
Newbie

Joined: Sun Oct 01, 2006 6:33 am
Posts: 8
Hello.
Today I was surprised I do not know how to do a very simple thing with Hibernate. Here is a sample SQL that does what I want:

SELECT * FROM main_table, sub_table WHERE main_table.sub_id = sub_table.sub_id

i.e., fetch the record for the corresponding sub_id from sub_table.
I don't want HQL for that - just how should I create my mappings in order to get a SubTable object from a MainTable?
It should be really simple, and one of the things that are done most often, but I don't have an idea how to do it. Anyone help?

Thanks in adnvance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 16, 2007 7:15 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Code:
public class MainTable{

Set subTable;
//other properties

}


Code:
public class SubTable{


//other properties

}



MainTable.hbm.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>     
    <class
        name="lk.wts.test.MainTable"
        table="AccountsDetails"
    >
     <id
            name="id"
            column="id"
            type="java.lang.String"
        >
            <generator class="assigned">             
            </generator>
        </id>

        <set
            name="subTable"
            lazy="false"
            inverse="true"
            cascade="all"
            sort="unsorted"
           
        >
            <key
                column="id"
            >
            </key>

            <one-to-many
                  class="lk.wts.test.SubTable"
            />

        </set>       
    </class>

</hibernate-mapping>



Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 16, 2007 8:06 am 
Newbie

Joined: Sun Oct 01, 2006 6:33 am
Posts: 8
Thanks for your reply.
The one-to-many association is a good idea, but in my case it is one-to-one. If I don't find how to use the one-to-one, I will use the one-to-many, with only one element in the set.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 16, 2007 8:12 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

use
<one-to-one
name="subTable"
class="lk.wts.test.SubTable"
cascade="none"
update="false"
insert="false"
lazy="false"
column="id"
/>
and change

Set subTable;

to SubTable subTable;


Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 16, 2007 3:02 pm 
Newbie

Joined: Sun Oct 01, 2006 6:33 am
Posts: 8
Thanks a lot


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.