-->
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.  [ 3 posts ] 
Author Message
 Post subject: Many-to-Many Properties
PostPosted: Tue Aug 08, 2006 6:23 pm 
Newbie

Joined: Fri May 05, 2006 12:09 am
Posts: 11
I think I may have mapped these wrong but I can't figure out a good way of doing it. I have a many to many relationship of a Vehicle and Inspections. There are multiple Vehicles that each can have different inspections. The problem is I need an expiration date on the associate table so I can figure out when each Vehicle has an expired schedule. I am wondering if there is a better way that to create a separate collectioin and then have to programmatically figure out which inspection goes with a vehicle and then get the LastChecked date.

Vehicle(VehicleID,Make,Model)
Inspection(InspectionID,RegulatoryBody,InspectionName)
Vehicle_Inspection(VehicleID,InspectionID,Lastchecked)

Mapping Files
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

  <class name="FleetManagement.BusinessLayer.Inspection, FleetManagement.BusinessLayer" table="RegulatoryInspection">

    <id name="InspectionID" column="InspectionID" type ="String" length="50">
      <generator class="assigned" />
    </id>

    <property name="RegulatoryBody" />
    <property name="NameOfInspection" />
    <property name="TypeOfSchedule" />
    <property name="Period" />
   
    <set name="Vehicles" lazy="true" cascade="none" table="Vehicle_Schedule">
      <key column="InspectionID" />
      <many-to-many class="FleetManagement.BusinessLayer.Vehicle, FleetManagement.BusinessLayer"
                   column="VehicleID" outer-join="auto" />
    </set>

    <bag name="Vehicle_Schedules" table="Vehicle_Schedule" lazy ="false" cascade="save-update">
      <key column="InspectionID" />

      <one-to-many class="FleetManagement.BusinessLayer.Vehicle_Schedule, FleetManagement.BusinessLayer" />

    </bag>

  </class>
</hibernate-mapping>


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

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

  <class name="FleetManagement.BusinessLayer.Vehicle, FleetManagement.BusinessLayer" table="Vehicle">

    <id name="VehicleID" column="VehicleID" type="String" length="50">
      <generator class="assigned" />
    </id>

    <!--Mapp properties I'd like to be persist/fetch
        Assume colum = property name-->
    <property name="UnitID" />
    <property name="Body_Type" />
    <property name="Make" />
    <property name="Model" />
    <property name="Year" />
    <property name="FunctionCode" />
    <property name="EnterpriseNO" />
    <property name="VIN" />
    <property name="Plate" />
    <property name="Location" />
    <property name="DriverName" />
    <property name="Description" />
   
   
    <bag name="Inspection" table="Vehicle_Inspections" lazy="false" cascade="save-update" >
      <key column="VehicleID" />
      <many-to-many class="FleetManagement.BusinessLayer.Inspection, FleetManagement.BusinessLayer"
                     column="InspectionID" outer-join="auto" />
    </bag>

    <bag name="Vehicle_Inspection" table="Vehicle_Inspection" lazy ="false" cascade="save-update">
      <key column="VehicleID" />
     
      <one-to-many class="FleetManagement.BusinessLayer.Vehicle_Inspections, FleetManagement.BusinessLayer" />
       
    </bag>


Code:
  <class name="FleetManagement.BusinessLayer.Vehicle_Inspection, FleetManagement.BusinessLayer" table="Vehicle_Inspection">

    <composite-id name="VehicleInspectionID" class="FleetManagement.BusinessLayer.IVehicleinspectionID, FleetManagement.BusinessLayer">
        <key-property name="VehicleID" />
        <key-property name="InspectionID" />
    </composite-id>

    <property name="LastChecked" />
    <property name="NextCheckDate" />
   
   
  </class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 11:02 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Remove the Vehicles collection from Inspection, so that Inspection contains only a set of VehicleInspections (one-to-many). Change the composite-id of VehicleInspections to
Code:
<composite-id name="VehicleInspectionID" class="FleetManagement.BusinessLayer.IVehicleinspectionID, FleetManagement.BusinessLayer">
    <key-many-to-one name="Inspection" column="InspectionID" class="Inspection"/>
    <key-many-to-one name="Vehicle" column="VehicleID" class="Vehicle"/>
</composite-id>
Change your VehicleInspectionID implementation class accordingly, so that it contains a Vehicle and Inspection, instead their IDs. Then you can use code like (excuse the javaness, I don't speak Microsoft)
Code:
for (VehicleInspection vi : inspection.getVehicleInspections())
{
  Vehicle v = vi.getVehicleInspectionID().getVehicle();
  Date lastChecked = vi.getLastChecked();
  // Your code...
}
Obviously, putting a getVehicle() method into Inspection (that returns getVehicleInpsectionID().getVehicle()) would be a good thing.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 4:56 pm 
Newbie

Joined: Fri May 05, 2006 12:09 am
Posts: 11
Thanks tenwit that worked perfect. Did not know I could do the composite keys like that. I think I am still thinking in terms of tables instead on objects.


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