hibernate复合(联合)主键的做关联类的例子

  1. public class Station implements java.io.Serializable {
  2. // Fields
  3. private Integer stationId;
    private String stationName;
    private Set stationRecords = new HashSet();
  4. // Constructors
  5. public Set getStationRecords() {
       return stationRecords;
    }
  6. public void setStationRecords(Set stationRecords) {
       this.stationRecords = stationRecords;
    }
  7. /** default constructor */
    public Station() {
    }
  8. /** full constructor */
    public Station(String stationName) {
       this.stationName = stationName;
    }
  9. // Property accessors
  10. public Integer getStationId() {
       return this.stationId;
    }
  11. public void setStationId(Integer stationId) {
       this.stationId = stationId;
    }
  12. public String getStationName() {
       return this.stationName;
    }
  13. public void setStationName(String stationName) {
       this.stationName = stationName;
    }
  14. public void stationDev(Dev dev) {
       RecordId id = new RecordId();
       id.setStation(this);
       id.setDev(dev);
       Record record = new Record();
       record.setId(id);
       record.setRecordTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
         .format(new Date()));
       stationRecords.add(record);
    }
  15. public void removeRecord(Record record) {
       stationRecords.remove(record);
    }
  16. }
  17. public class Dev implements java.io.Serializable {
  18. // Fields
  19. private Integer devId;
    private String devName;
    private Set devRecords = new HashSet();
  20. // Constructors
  21. /** default constructor */
    public Dev() {
    }
  22. /** full constructor */
    public Dev(String devName) {
       this.devName = devName;
    }
  23. // Property accessors
  24. public Integer getDevId() {
       return this.devId;
    }
  25. public void setDevId(Integer devId) {
       this.devId = devId;
    }
  26. public String getDevName() {
       return this.devName;
    }
  27. public void setDevName(String devName) {
       this.devName = devName;
    }
  28. public Set getDevRecords() {
       return devRecords;
    }
  29. public void setDevRecords(Set devRecords) {
       this.devRecords = devRecords;
    }
  30. }
  31. public class RecordId implements java.io.Serializable {
  32. // Fields
  33. private Station station;
  34. private Dev dev;
  35. // Constructors
  36. /** default constructor */
    public RecordId() {
    }
  37. // Property accessors
  38. public Station getStation() {
       return station;
    }
  39. public void setStation(Station station) {
       this.station = station;
    }
  40. public Dev getDev() {
       return dev;
    }
  41. public void setDev(Dev dev) {
       this.dev = dev;
    }
  42. public boolean equals(Object obj) {
       return (obj instanceof RecordId)
         && (this.getStation().equals(((RecordId) obj).getStation()))
         && (this.getDev().equals(((RecordId) obj).getDev()));
    }
  43. public int hashCode() {
       return this.getStation().hashCode() ^ this.getDev().hashCode();
    }
  44. }
  45. public class Record implements java.io.Serializable {
  46. // Fields
  47. private RecordId id;
    private String recordTime;
  48. // Constructors
  49. /** default constructor */
    public Record() {
    }
  50. /** full constructor */
    public Record(RecordId id, String recordTime) {
       this.id = id;
       this.recordTime = recordTime;
    }
  51. // Property accessors
  52. public RecordId getId() {
       return this.id;
    }
  53. public void setId(RecordId id) {
       this.id = id;
    }
  54. public String getRecordTime() {
       return this.recordTime;
    }
  55. public void setRecordTime(String recordTime) {
       this.recordTime = recordTime;
    }
  56. }
  57. Station.hbm.xml
  58. <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!--
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="com..bean.Station" table="Station" schema="dbo" catalog="test">
            <id name="stationId" type="java.lang.Integer">
                <column name="StationId" />
                <generator class="native" />
            </id>
            <property name="stationName" type="java.lang.String">
                <column name="StationName" length="50" not-null="true" />
            </property>
            <set name="stationRecords" inverse="true" lazy="true" cascade="all-delete-orphan">
        <key column="stationId" />
        <one-to-many class="com.dpc.bean.Record"/>
       </set>
           
        </class>
    </hibernate-mapping>
  59. Dev.hbm.xml
  60. <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!--
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="com.bean.Dev" table="Dev" schema="dbo" catalog="test">
            <id name="devId" type="java.lang.Integer">
                <column name="DevId" />
                <generator class="native" />
            </id>
            <property name="devName" type="java.lang.String">
                <column name="DevName" length="50" not-null="true" />
            </property>
            <set name="devRecords" inverse="false" lazy="true" cascade="all-delete-orphan">
        <key column="DevId" />
        <one-to-many class="com.dpc.bean.Record"/>
       </set>
        </class>
    </hibernate-mapping>
  61. Record.hbm.xml
  62. <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!--
    Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
    <class name="com.bean.Record" table="Record" schema="dbo"
       catalog="test">
       <composite-id name="id" class="com.dpc.bean.RecordId"
        unsaved-value="any">
        <key-many-to-one name="station" column="StationId"
         class="com.dpc.bean.Station" />
        <key-many-to-one name="dev" column="DevId"
         class="com.dpc.bean.Dev" />
       </composite-id>
       <property name="recordTime" type="java.lang.String">
        <column name="RecordTime" not-null="true" />
       </property>
    </class>
    </hibernate-mapping>

你可能感兴趣的:(.net,Hibernate,bean,xml,MyEclipse)