-->
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: Simple Mapping Two Table Join
PostPosted: Mon May 08, 2006 1:49 pm 
Newbie

Joined: Mon May 08, 2006 10:00 am
Posts: 8
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

Name and version of the database you are using:Oracle 9i

hi! - from an Newbie to Hibernate.

I need resultset by joining two tables (1-many).

Two tables -

Table 1 - SNO (pk) & NAME

Table 2 - SNO (fk) & ID

& two mapping beans MappingBean1 (Table 1) & MappingBean2 (Table 2).

for the sql query - select name from Table 1,Table 2 where
Table 1.SNO = Table 2.SNO AND Table 2.ID='value'

What are the different ways to achive this with mapping?

This is the mapping xml for MappingBean2.hbm.xml am using -

<?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 auto-import="true" package="mapping">
<class name="MappingBean2" table="Table 2">
<id name="strSno" column="SNO"/>
<generator class="foreign">
<param name="property">strSno</param> </generator>
<one-to-one name="mappingBean1" class="MappingBean1" outer-join="auto" />
<property name="strId" column="ID" update="false"/>
</class>
</hibernate-mapping>

When I call this -
Query query = session.createQuery("select name from MappingBean1 as map1, MappingBean2 as map2" + "where map1.strSno=map2.strSno" + "map2.id= "+strId);

Iam getting error -Error reading resource: MappingBean2.hbm.xml

Thanks.
Amit


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 2:19 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I think you have closed the element <id> without including the element <generator>.

it should be like

Code:
<id name="strSno" column="SNO">
   <generator class="foreign">
      <param name="property">strSno</param>
   </generator>
</id>


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 2:52 pm 
Newbie

Joined: Mon May 08, 2006 10:00 am
Posts: 8
Thanks for reply.
I tried to close the </generator></id>.

Now Iam getting this error -

could not resolve property: strSno of: mapping.MappingBean2..

Is something wrong with mapping?

Or how to use this using Criteria?

_________________
-- Amit.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 3:10 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
This link http://www.hibernate.org/hib_docs/v3/re ... n-onetoone has an example, which will help you

According to the example your mapping will be as below
Code:
<class name="MappingBean2" table="Table_2">
   <id name="strSno" column="SNO">
      <generator class="foreign">
         <param name="property">mappingBean1</param>
      </generator>
   </id>
   ...
   <one-to-one name="mappingBean1"
        class="MappingBean1"
        outer-join="auto"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 2:36 pm 
Newbie

Joined: Mon May 08, 2006 10:00 am
Posts: 8
Hello,

Now, Ive the following mappings for the composite primary key of Table 2.

Table 1 - SNO (pk) & NAME

Table 2 - SNO(fk),ID(fk) & Date (composite pk)

Table 3 - ID (pk) & DESC


Mapping for Table 2 -

<class name="MappingBean2" table="Table_2">

<composite-id>
<key-property name="SNO" update="false"/>
<key-property name="ID" update="false"/>
<key-property name="DATE" type="timestamp"/>
</composite-id>
...
<one-to-one name="mappingBean1" class="MappingBean1"
outer-join="auto"/>
</class>

If I append following mapping under <composite-id> or outside, it gives me - Error reading resource!

<generator class="foreign">
<param name="property">SNO</param>
</generator>

How to add all the foreign keys references (SNO & ID) to the table 2 mappings to join the tables?

_________________
-- Amit.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 2:56 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I would like to go with mappings as below. Check to see if it works for your requirements.

Code:
<class name="TableOne" table="Table_1">
   <id name="strSno" column="SNO"/>
   <property name="name" column="NAME"/>
   <set name="setOne"
        lazy="true"
   inverse="true"
        cascade="all-delete-orphan">
      <key>
        <column name="SNO" />
      </key>
      <one-to-many class="TableOne"/>
   </set>   
</class>

<class name="TableThree" table="Table_3">
   <id name="idThree" column="ID"/>
   <property name="description" column="DESC"/>
   <set name="setThree"
        lazy="true"
   inverse="true"
        cascade="all-delete-orphan">
      <key>
        <column name="ID" />
      </key>
      <one-to-many class="TableThree"/>
   </set>   
</class>

<class name="TableTwo" table="Table_2">
   <composite-id>
     <key-property name="sno"  column="SNO" />
     <key-property name="id"   column="ID" />
     <key-property name="date" column="DATE" type="timestamp"/>
   </composite-id> 

   <many-to-one name="tableTwoObject" class="TableTwo" column="SNO" insert="false" update="false"/>
   <many-to-one name="tableTwoObject" class="TableTwo" column="ID" insert="false" update="false"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 4:37 pm 
Newbie

Joined: Mon May 08, 2006 10:00 am
Posts: 8
Iam getting following exception :-

RuntimeException: broken column mapping for: SNO.id of: TableTwo

for the following mappings -


<class name="TableOne" table="Table_1">
<id name="strSno" column="SNO"/>
<property name="name" column="NAME"/>
<one-to-one name="strSno" class="TableOne"/>
</set>
</class>

<class name="TableTwo" table="Table_2">
<composite-id>
<key-property name="sno" column="SNO" />
<key-property name="id" column="ID" />
<key-property name="date" column="DATE" type="timestamp"/>
</composite-id>
<one-to-one name="tableTwoObject" class="TableTwo" column="SNO" insert="false" update="false"/>
</class>

_________________
-- Amit.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 5:11 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
The example mappings will work for many-to-one relations. For one-to-one relation as expected it will fail. I never had any experience writing one-to-one mappings.

However even with my inexperience your mapping files seems wrong. See if below ones will work

Code:
<class name="TableOne" table="Table_1">
<id name="strSno" column="SNO"/>
<property name="name" column="NAME"/>
<one-to-one name="tableTwoObject" class="TableTwo" outer-join="auto"/>
</set>
</class>

<class name="TableTwo" table="Table_2">
<composite-id>
<key-property name="sno" column="SNO" />
<key-property name="id" column="ID" />
<key-property name="date" column="DATE" type="timestamp"/>
</composite-id>
<one-to-one name="tableOneObject" class="TableOne" outer-join="auto"/>
</class>


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.