-->
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.  [ 6 posts ] 
Author Message
 Post subject: Many-to-one associations for Hibernate
PostPosted: Fri Apr 20, 2007 10:06 am 
Newbie

Joined: Wed Mar 28, 2007 2:23 pm
Posts: 4
Hi,

I am using Hibernate 3.0 . I need help on retriving info from database where tables are associated with many-to-one relationship.

I have read about an attribute "formula" which will deal with many-to-one relation, but I have no clue how to use. Could anyone help me on this.

Thanks,
message2hero


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 11:41 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Formula and <many-to-one> relationships are two diferrent things. Please specify the table strcutures that you are trying to retrieve and we can help further.

Here's a simple example of bi-directional many-to-one:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
   package="com.test.hibernate.model">

   <class name="Employee" table="EMPLOYEE">
      
      <id name="employeeId" column="EMPLOYEE_ID" unsaved-value="-1">
         <generator class="increment"/>
      </id>
      
      <property name="firstName" column="FIRST_NAME"
              not-null="true"
              length="20"
              />
      <property name="lastName" column="LAST_NAME"
              length="20"
              />
      <property name="address1" column="ADDRESS_1"
              length="20"
              />
      <property name="address2" column="ADDRESS_2"
              length="20"
              />
      <property name="zipCode" column="ZIP_CODE"
              length="20"
              />
      <property name="fullName" formula="FIRST_NAME||LAST_NAME"
              length="20"
              />             
   
      <set name="empHistory" cascade="all" >
         <key column="EMPLOYEE_ID"/>
         <one-to-many class="EmployeeHistory" inverse="true"/>
      </set>
   </class>
   
</hibernate-mapping>




Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
   package="com.test.hibernate.model">

   <class name="EmployeeHistory" table="EMPLOYEE_HISTORY">
      
      <id name="eventId" column="EVENT_ID">
         <generator class="increment"/>
      </id>
      
      <property name="dateOccurred" column="OCCURRED_DT" not-null="true" />
      <property name="eventType" column="EVENT_TYPE"/>
      <property name="comments" column="COMMENTS"/>
      <many-to-one name="employee" />
   </class>
   
</hibernate-mapping>
[/code]

_________________
--------------
Don't forget to Rate the post


Top
 Profile  
 
 Post subject: Many-to-one associations for Hibernate
PostPosted: Fri Apr 20, 2007 1:04 pm 
Newbie

Joined: Wed Mar 28, 2007 2:23 pm
Posts: 4
Hi,


Below is confg file which has FACILITY_ID as primary key from FACILITY.


<class name="htm.dto.Facility" table="FACILITY" schema="dbo" catalog="Aviation">
<id name="facilityId" type="integer">
<column name="FACILITY_ID" />
<generator class="assigned" />
</id>
<many-to-one name="dotDistrict" class="htm.dto.DotDistrict" fetch="select">
<column name="DOT_DIST_ID" />
</many-to-one>

Below is second config file file which has RUNWAY_ID as primary one. It is associated as many-to-one with FACILITYtable .


<hibernate-mapping>
<class name="htm.dto.Runway" table="RUNWAY" schema="dbo" catalog="Aviation">
<id name="runwayId" type="integer">
<column name="RUNWAY_ID" />
<generator class="assigned" />
</id>
<many-to-one name="facility" class="htm.dto.Facility" fetch="select">
<column name="FACILITY_ID" not-null="true" />
</many-to-one>
<many-to-one name="surfaceTreatment" class="htm.dto.SurfaceTreatment" fetch="select">
<column name="SURFACE_TREATMENT_ID" />
</many-to-one>
<many-to-one name="surfaceType" class="htm.dto.SurfaceType" fetch="select">
<column name="SURFACE_TYPE_ID" />
</many-to-one>
<property name="runwayRecordType" type="string">
<column name="RUNWAY_RECORD_TYPE" length="3" />
</property>
<set name="runwayEnds" inverse="true">
<key>
<column name="RUNWAY_ID" not-null="true" />
</key>
<one-to-many class="htm.dto.RunwayEnd" />
</set>
</class>
</hibernate-mapping>


So, how can i get the values from RUNWAY table if know the FACILITY_ID which is primary key for FACILITY table.

Beloy is my class file:

int facilityId = facility.getFacilityId();
System.out.println("Facility Id" + facilityId); //here facilityId value is available and passing it to query to gwt the values from Runway table associated with Facility table
runwayEnd = (RunwayEnd) session.createQuery(" from Runway R where R.facility.facilityId=:facilityId").setInteger("facilityId", facilityId).uniqueResult();

Thanks,
message2hero


Last edited by message2hero on Fri Apr 20, 2007 2:35 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Many-to-one associations for Hibernate
PostPosted: Fri Apr 20, 2007 2:33 pm 
Newbie

Joined: Wed Mar 28, 2007 2:23 pm
Posts: 4
[quote="message2hero"]Hi,


Below is confg file which has FACILITY_ID as primary key from FACILITY.


<class name="htm.dto.Facility" table="FACILITY" schema="dbo" catalog="Aviation">
<id name="facilityId" type="integer">
<column name="FACILITY_ID" />
<generator class="assigned" />
</id>
<many-to-one name="dotDistrict" class="htm.dto.DotDistrict" fetch="select">
<column name="DOT_DIST_ID" />
</many-to-one>

Below is second config file file which has RUNWAY_ID as primary one. It is associated as many-to-one with FACILITYtable .


<hibernate-mapping>
<class name="htm.dto.Runway" table="RUNWAY" schema="dbo" catalog="Aviation">
<id name="runwayId" type="integer">
<column name="RUNWAY_ID" />
<generator class="assigned" />
</id>
<many-to-one name="facility" class="htm.dto.Facility" fetch="select">
<column name="FACILITY_ID" not-null="true" />
</many-to-one>
<many-to-one name="surfaceTreatment" class="htm.dto.SurfaceTreatment" fetch="select">
<column name="SURFACE_TREATMENT_ID" />
</many-to-one>
<many-to-one name="surfaceType" class="htm.dto.SurfaceType" fetch="select">
<column name="SURFACE_TYPE_ID" />
</many-to-one>
<property name="runwayRecordType" type="string">
<column name="RUNWAY_RECORD_TYPE" length="3" />
</property>
<set name="runwayEnds" inverse="true">
<key>
<column name="RUNWAY_ID" not-null="true" />
</key>
<one-to-many class="htm.dto.RunwayEnd" />
</set>
</class>
</hibernate-mapping>


So, how can i get the values from RUNWAY table if know the FACILITY_ID which is primary key for FACILITY table.

Beloy is my class file:

int facilityId = facility.getFacilityId();
System.out.println("Facility Id" + facilityId);
runwayEnd = (RunwayEnd) session.createQuery(" from Runway R where R.facility.facilityId=:facilityId").setInteger("facilityId", facilityId).uniqueResult();

Thanks,
Hero


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 3:05 pm 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Could you post the SQL queries generated while running the code? Are you getting exceptions?

_________________
--------------
Don't forget to Rate the post


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 4:06 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Try this for your HQL:

"from Runway R where R.facility.id=:facilityId"

You may need a join in there, not sure.


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