hibernatehbmxml

composite key if composite class contains class type references in a field


I have one Driver class:

 public class Driver {

    private String driverId;
    private String driverName;

    //getter and setter

}

and one DriverEntry class which is used to store the driver daily driving details and DriverEntry class has DriverEntryKey which contains as Driver object and Date object . So how can I map with this files in driver.hbm.xml file?

 public class DriverEntry {

    private DriverEntryKey key;
    private String startTime;
    private String endTime;
    //getter and setter

}

    public class DriverEntryKey implements Serializable{

    private Driver driver;
    private Date date;


}
   <class name="Driver" table="driver" >
    <id name="driverId" column="driver_id">
    <generator class="assigned"/>
    </id>
    <property name="driverName" column="prod_name" length="20"/>
    </class>


    <class name="DriverEntry" table="DriverEntry" >
    <composite-id name="key" class="DriverEntryKey">
    <key-property name="driver" type=""></key-property>
    <key-property name="date" type=""></key-property>        
    </composite-id>
    </class>

So what can I fill inside which maintains driver---primary-key-to-foreign-key--in--> DataEntry table???


Solution

  • Is this what you are trying to achieve

    <class name="DriverEntry">
    
    <composite-id name="id" class="DriverEntryKey">
        <key-property name="driverId"/>
        <key-property name="date" type="date"/>        
    </composite-id>
    
    <property name="name"/>
    
    <many-to-one name="driver" class="Driver">
        <column name="driverId"/>
        // all other columns
    </many-to-one>
    ....
    
    </class>
    

    You will have to tweak the answer a bit according to your requirements