有两个表,student(person id,addressid),address(addressid)
StudentPo类:
package com.lovo.po;
public class StudentPo {
private int personid;
private AddressPo address;
public int getPersonid() {
return personid;
}
public void setPersonid(int personid) {
this.personid = personid;
}
public AddressPo getAddress() {
return address;
}
public void setAddress(AddressPo address) {
this.address = address;
}
}
AddressPo类:
package com.lovo.po;
public class AddressPo {
private int addressid;
public int getAddressid() {
return addressid;
}
public void setAddressid(int addressid) {
this.addressid = addressid;
}
}
StudentPo.hbm.xml:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE RootElement PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN " "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
<hibernate-mapping>
<class name= "com.lovo.po.StudentPo " table= "student ">
<id name= "personid " column= "personid ">
<generator class = "increment "/>
</id>
<one-to-one name= "address " class= "AddressPo " cascade= "all "> </one-to-one>
</class>
</hibernate-mapping>
AddressPo.hbm.xml:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE RootElement PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN " "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
<hibernate-mapping>
<class name= "com.lovo.po.AddressPo " table= "address ">
<id name= "addressid " column= "addressid ">
<generator class= "foreign ">
<param name= "property "> student </param>
</generator>
</id>
<one-to-one name= "student " constrained= "true "> </one-to-one>
</class>
</hibernate-mapping>
hibernate.cfg.xml:
<?xml version= '1.0 ' encoding= 'UTF-8 '?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN "
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name= "connection.username "> root </property>