① 基于外键的单向1--->1关联
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="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/temp</property>
<property name="connection.username">root</property>
<property name="connection.password">mysqladmin</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="db/mapping/Person.hbm.xml"/>
<mapping resource="db/mapping/Address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Person.java :
public class Person {
private int id;
private String name;
private int age;
private Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Address.java :
public class Address {
private Integer addressId;
private String addressDetail;
public Address() {
}
public Address(String addressDetail) {
this.addressDetail = addressDetail;
}
public Integer getAddressId() {
return addressId;
}
public void setAddressId(Integer addressId) {
this.addressId = addressId;
}
public String getAddressDetail() {
return addressDetail;
}
public void setAddressDetail(String addressDetail) {
this.addressDetail = addressDetail;
}
}
Person.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="db.domain">
<class name="Person" table="persons">
<id name="id" column="person_id">
<generator class="identity"/>
</id>
<property name="name" type="string"/>
<property name="age" type="integer"/>
<many-to-one name="address" class="Address" unique="true" cascade="all" column="address_id"/>
</class>
</hibernate-mapping>
Address.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="db.domain">
<class name="Address" table="address">
<id name="addressId" column="address_id">
<generator class="identity"/>
</id>
<property name="addressDetail" type="string"/>
</class>
</hibernate-mapping>
Test.java :
public class Test {
public static void main(String[] args) {
Session session=HibernateSessionFactory.getSession();
Transaction txt=session.beginTransaction();
Person p=new Person();
p.setName("tom");
p.setAge(25);
Address a=new Address("广州天河");
p.setAddress(a);
session.persist(p);
Address a2=new Address("上海虹口");
p.setAddress(a2);
txt.commit();
HibernateSessionFactory.closeSession();
}
}
② 有连接表的单向1--->1关联
Person.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="db.domain">
<class name="Person" table="persons">
<id name="id" column="person_id">
<generator class="identity"/>
</id>
<property name="name" type="string"/>
<property name="age" type="integer"/>
<join table="person_address">
<key column="person_id"/>
<many-to-one name="address" class="Address" unique="true" cascade="all" column="address_id"/>
</join>
</class>
</hibernate-mapping>
其余代码不变。
③ 基于主键的单向1--->1关联:
Person.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="db.domain">
<class name="Person" table="persons">
<id name="id" column="person_id">
<generator class="foreign">
<param name="property">address</param>
</generator>
</id>
<property name="name" type="string"/>
<property name="age" type="integer"/>
<one-to-one name="address"/>
</class>
</hibernate-mapping>