-->
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.  [ 7 posts ] 
Author Message
 Post subject: 1 class, 2 tables, some conditions... mapping.
PostPosted: Wed Jun 13, 2007 5:28 am 
Newbie

Joined: Wed Jun 13, 2007 4:50 am
Posts: 4
hi there.
im new to hibernate. i really cant figure out how can i map one class to two tables with no direct relation (FK<->PK).


i have this 2 tables:

Stops:
-code
-thing
...

Problems:
-code
-description



and this sql statment:

SELECT S.code, P.description, thing, ...
FROM Stops S, Problems P
WHERE S.code=P.code AND thing<>'sausages'
ORDER BY code DESC


i want each line of the resulting table of this SQL to be represented by one object of my class. is there a way?
i know i can use the join element to bring together columns from diferent tables,
but how can i express the conditions in the hibernate mapping file???


many thanks in advance.
hope anyone can help me.
cheers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 6:53 am 
Newbie

Joined: Wed Jun 13, 2007 4:50 am
Posts: 4
i forgot something in the Stops database table:

Stops:
-stopid <-----
-code
-thing
...

can anyone tell me if this mapping will do the trick???


Code:
<hibernate-mapping>

   <class name="package.Stop" table="Stops"
      where="Stops.code=Problems.code AND NOT(thing='sausages')">

      <id name="stopid">
         <generator class="assigned" />
      </id>

      <property name="code" />
      <property name="thing" />

      
      <join table="Problems">
         <key column="code"/>
         <property name="description" />
      </join>


   </class>

</hibernate-mapping>



and if i dont want to map the 'stopid' column of the table? i do need to define another <id> element for the class, right?

many thanks


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed Jun 13, 2007 7:36 am 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Hi sign,

Hibernate does not recomend this approach but it does support this kind of requirement for legacy systems.

You need to create a unique key on the first class with the two columns which are being referred in the second table.

Then in the second class you would need to use property-ref to the unique key on the first class.

Let me know if you need more detailed solution. If you do give me your hibernate mapping files and I will update them.

Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 9:07 am 
Newbie

Joined: Wed Jun 13, 2007 4:50 am
Posts: 4
That is my case, a legacy system... cant change database.

(Correction: there is a PK-FK relationship between the tables, sorry)

Stops:
- stopID NOTNULL primarykey
- code NOTNULL foreignkey
- thingA NOTNULL
- thingB NOTNULL
- othersThings

Problems:
- code NOTNULL primarykey
- description
- thing1 NOTNULL
- thing2 NOTNULL


i want to map a class like:

Code:
package testPackage;

public class Stop {
   private String code;
   private String thingA;
   private String thingB;
   private String otherThings;
   private String description;

   ...

   //gets and sets

}

the only thing coming from the 'Problems' table is the 'description' that matches the 'code'.
in my case i only want to read this information from the database, never updates, inserts...etc.
do i need to map the stopID too?

Quote:
"You need to create a unique key on the first class with the two columns which are being referred in the second table."

can you show me a example?
Quote:
"Then in the second class you would need to use property-ref to the unique key on the first class. "

can this be done with only one class like the one above?

thanks.
Andre


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 2:12 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Unless the primary key in both the tables is same I don't think its possible to do it using a single class.

Any issues with having 2 classes? One of them could be an internal class...

sample for unique-key ..
Code:
<property name="x">
         <column name="xval" not-null="true" length="4" unique-key="xy"/>
      </property>
      <property name="y">
         <column name="yval" not-null="true" length="4" unique-key="xy"/>
      </property>


But since you have a FK relationship the unique-key and property-ref is not required in your case.

Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 5:38 am 
Newbie

Joined: Wed Jun 13, 2007 4:50 am
Posts: 4
hi... i've came up with something that might result well... i think.

THE TALES are has shown above.

THE CLASSES:

Code:
public class Stop {
   private Integer stopid;
   private String thinga;
   private String thingb;
   private Problem prob;
//gets-sets
...
}

public class Problem {
   private String code;
   private String description;
   private String thinga;
   private String thingb;
//gets-sets
...
}


THE MAPPING FILES:

Code:
<!--  STOP -->
<hibernate-mapping>

   <class name="hib.test.Stop" table="Stops" where="NOT (thinga='sausages')">
   
      <id name="stopid">
         <generator class="assigned"/>
      </id>      
      <many-to-one name="prob" class="hib.test.Problem" column="code"/>      
      <property name="thinga"/>      
      <property name="thingb"/>

   </class>

</hibernate-mapping>

<!--  PROBLEM -->
<hibernate-mapping>

   <class name="hib.test.Problem" table="Problems">
   
      <id name="code">
         <generator class="assigned"/>
      </id>      
      <property name="description"/>   
      <property name="thing1"/>      
      <property name="thing2"/>   

   </class>

</hibernate-mapping>



the only problem is this way im getting all the information, all the columns and not only the ones i need. since i'm not going to change the database data, only reading data, could i ignore the primary key column of the stops table (stopid) and some other columns and shrink the class to:

Code:
public class Stop {
   private String thinga;
   private Problem prob;
//gets-sets
...
}


and how could the hibernate mapping be to map this?
or maybe mapping only the NOTNULL fields :?


thanks a lot
cheers

André


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Jun 14, 2007 6:54 am 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
All columns except primary key can be skipped. If there is any other column that is unique, then you can use that as your id column.

Regards,
Jitendra


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