- db table
CREATE TABLE HUSBAND
(
HUSBAND_ID NUMBER(5) NOT NULL,
NAME VARCHAR(20)
)
CREATE TABLE WIFE
(
WIFE_ID NUMBER(5) NOT NULL,
HUSBAND_ID NUMBER(5) NOT NULL,
NAME VARCHAR(20)
)
- 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">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@DbServer:port:sid</property>
<property name="hibernate.connection.username">***</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="config/mapping/oneToOneConfig/Husband.hbm.xml" />
<mapping resource="config/mapping/oneToOneConfig/Wife.hbm.xml" />
</session-factory>
</hibernate-configuration>
- Husband.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>
<class name="pojo.oneToOneConfig.Husband" table="HUSBAND">
<id name="husbandId" type="int">
<column name="HUSBAND_ID" precision="5" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="name" length="20" />
</property>
</class>
</hibernate-mapping>
- Wife.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>
<class name="pojo.oneToOneConfig.Wife" table="WIFE">
<id name="wifeId" type="int">
<column name="WIFE_ID" precision="5" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="name" length="20" />
</property>
<many-to-one name="husband" column="HUSBAND_ID" cascade="all" unique="true" />
</class>
</hibernate-mapping>
- POJO
package pojo.oneToOneConfig;
public class Husband {
private int husbandId;
private String name;
private Wife wife;
//getter and setter
}
package pojo.oneToOneConfig;
public class Wife {
private int wifeId;
private String name;
private Husband husband;
//getter and setter
}
- HibernateUtil
略
改成注解的版本:
- 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">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@DbServer:port:sid</property>
<property name="hibernate.connection.username">***</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping class="pojo.oneToOneAnnotation.Wife" />
<mapping class="pojo.oneToOneAnnotation.Husband" />
</session-factory>
</hibernate-configuration>
- POJO
package pojo.oneToOneAnnotation;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="HUSBAND")
public class Husband {
@Id
@Column(name="HUSBAND_ID")
private int husbandId;
@Column(name="NAME")
private String name;
@OneToOne(mappedBy="husband") // the husband property in Wife
private Wife wife;
//getter and setter
}
package pojo.oneToOneAnnotation;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "WIFE")
public class Wife {
@Id
@Column(name = "WIFE_ID")
private int wifeId;
@Column(name = "NAME")
private String name;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "HUSBAND_ID")
private Husband husband;
//getter and setter
}
网友评论