-->
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: Does anyone have an example of a subselect?
PostPosted: Mon Jun 01, 2009 5:18 pm 
Newbie

Joined: Wed May 27, 2009 11:43 am
Posts: 4
Does anyone have a working example of .hbm.xml file that does a subselect? And the java code that is needed to execute the subselect?


Top
 Profile  
 
 Post subject: Re: Does anyone have an example of a subselect?
PostPosted: Tue Jun 02, 2009 6:02 am 
Newbie

Joined: Tue Jun 02, 2009 4:51 am
Posts: 2
Location: Iran
salaam,
imagine we have a table called "main" in a database and it has a foreign key "fkey" from another table called "foreign". we want to make classes and mapping files for them:

classes:

class MainClass {

private int id; //primary key
private int fkey; // foreign key
private ForeignClass fClass; //foreign table's class

public MainClass() {

}

// ... id, fkey and fClass setters and getters
}

class ForeignClass {

private int id; //primary key
private String name; //a sample column

public ForeignClass() {

}

// .... id and name setters and getters
}

mapping files (.hbm.xml):
MainClass:
<hibernate-mapping>
<class name="MainClass" table="main" lazy="false" >
<id name="id" column="mId" >
<generator class="assigned" >
</id>
<property name="fkey" column="mfkey" type="int" />

<many-to-one name="fClass" class="ForeignClass " column="fkey" insert="false" update="false" />
</class>
<query name="testQuery">select test from MainClass as test left join test.fClass where test.id = 11 </query>
</hibernate-mapping>

ForeignClass :

<hibernate-mapping>
<class name="ForeignClass " table="foreign" lazy="false" >
<id name="id" column="mId" >
<generator class="assigned" >
</id>
<property name="name" column="fname" type="
</class>
</hibernate-mapping>

in your hibernate.cfg.xml file you need to add the address of both of this mapping files:

<mapping resource="src/main.hbm.xml" />
<mapping resource="src/foreign.hbm.xml" />


now to implement them you need to make an instance of MainClass for example:
MainClass mainClass;

then retreive the data in it. then you can have the value of foreign table with mainClass.getForeginClass()...
I didn't describe the way to implement it in the end because you can implement it in many ways. I hope this will help. also I should remind you that this kind of mapping is only for tables which have many to one relations, I mean many to one relations.

have a good time,
Hooman :)

_________________
Hooman Peiro Sajjad


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.