复合键,如果复合类包含字段中的类类型引用

时间:2013-10-18 05:54:55

标签: hibernate hbmxml

我有一个Driver类:

 public class Driver {

    private String driverId;
    private String driverName;

    //getter and setter

}

和一个DriverEntry类用于存储驱动程序的每日驱动细节,DriverEntry类包含DriverEntryKey,其中包含Driver对象和Date对象。 那么如何在driver.hbm.xml文件中映射这些文件?

 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>

那么我可以在里面填充什么来维护驱动程序--- primary-key-to-foreign-key - in - &gt; DataEntry表???

2 个答案:

答案 0 :(得分:0)

这是你想要实现的目标

<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>

您必须根据您的要求稍微调整一下答案

答案 1 :(得分:0)

<class name="DriverEntry" >
        <composite-id name="key" class="DriverEntryKey">
            <key-many-to-one name="driver" class="Driver" column="driver_id"/>
            <key-property name="drivingDate" type="java.util.Date"></key-property>
        </composite-id>

        <property name="startTime" column="start_time" />
        <property name="endTime" column="end_time" />



    </class>