- public class Station implements java.io.Serializable {
- // Fields
- private Integer stationId;
private String stationName;
private Set stationRecords = new HashSet(); - // Constructors
- public Set getStationRecords() {
return stationRecords;
} - public void setStationRecords(Set stationRecords) {
this.stationRecords = stationRecords;
} - /** default constructor */
public Station() {
} - /** full constructor */
public Station(String stationName) {
this.stationName = stationName;
} - // Property accessors
- public Integer getStationId() {
return this.stationId;
} - public void setStationId(Integer stationId) {
this.stationId = stationId;
} - public String getStationName() {
return this.stationName;
} - public void setStationName(String stationName) {
this.stationName = stationName;
} - 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);
} - public void removeRecord(Record record) {
stationRecords.remove(record);
} - }
- public class Dev implements java.io.Serializable {
- // Fields
- private Integer devId;
private String devName;
private Set devRecords = new HashSet(); - // Constructors
- /** default constructor */
public Dev() {
} - /** full constructor */
public Dev(String devName) {
this.devName = devName;
} - // Property accessors
- public Integer getDevId() {
return this.devId;
} - public void setDevId(Integer devId) {
this.devId = devId;
} - public String getDevName() {
return this.devName;
} - public void setDevName(String devName) {
this.devName = devName;
} - public Set getDevRecords() {
return devRecords;
} - public void setDevRecords(Set devRecords) {
this.devRecords = devRecords;
} - }
- public class RecordId implements java.io.Serializable {
- // Fields
- private Station station;
- private Dev dev;
- // Constructors
- /** default constructor */
public RecordId() {
} - // Property accessors
- public Station getStation() {
return station;
} - public void setStation(Station station) {
this.station = station;
} - public Dev getDev() {
return dev;
} - public void setDev(Dev dev) {
this.dev = dev;
} - public boolean equals(Object obj) {
return (obj instanceof RecordId)
&& (this.getStation().equals(((RecordId) obj).getStation()))
&& (this.getDev().equals(((RecordId) obj).getDev()));
} - public int hashCode() {
return this.getStation().hashCode() ^ this.getDev().hashCode();
} - }
- public class Record implements java.io.Serializable {
- // Fields
- private RecordId id;
private String recordTime; - // Constructors
- /** default constructor */
public Record() {
} - /** full constructor */
public Record(RecordId id, String recordTime) {
this.id = id;
this.recordTime = recordTime;
} - // Property accessors
- public RecordId getId() {
return this.id;
} - public void setId(RecordId id) {
this.id = id;
} - public String getRecordTime() {
return this.recordTime;
} - public void setRecordTime(String recordTime) {
this.recordTime = recordTime;
} - }
- Station.hbm.xml
- <?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> - Dev.hbm.xml
- <?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> - Record.hbm.xml
- <?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>
hibernate复合(联合)主键的做关联类的例子
最新推荐文章于 2025-08-09 22:38:46 发布