IdCard
package com.wxh.dto;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class IdCard {
private int num;
private String name;
private People people;
public IdCard() {
}
public IdCard(int num, String name) {
this.num = num;
this.name = name;
}
@Id
@Column(name="cardNum")
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//配置一对一的关系,主动放弃维护关系(由另一方维护)
@OneToOne(fetch=FetchType.EAGER,mappedBy="idcard")
public People getPeople() {
return people;
}
public void setPeople(People people) {
this.people = people;
}
}
package com.wxh.dto;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
@Entity
public class People {
private Integer pid;
private String sex;
private String phone;
private String address;
private IdCard idcard;
public People() {
}
public People(Integer pid, String sex, String phone, String address,
IdCard idcard) {
super();
this.pid = pid;
this.sex = sex;
this.phone = phone;
this.address = address;
this.idcard = idcard;
}
@Id
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//配置一对一的唯一外键关联
@OneToOne(fetch=FetchType.EAGER,cascade={CascadeType.ALL})
@JoinColumn(name="num",referencedColumnName="cardNum",unique=true)
public IdCard getIdcard() {
return idcard;
}
public void setIdcard(IdCard idcard) {
this.idcard = idcard;
}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@127.0.0.1:1521:orcl
</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">oracle</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 开启缓存 -->
<property name="cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
<!-- 本地事务 -->
<!-- <property name="current_session_context_class">thread</property> -->
<mapping class="com.wxh.dto.People"/>
<mapping class="com.wxh.dto.IdCard"/>
<class-cache usage="read-only" class="com.wxh.dto.People"/>
</session-factory>
</hibernate-configuration>
Test
package test;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import com.wxh.dto.IdCard;
import com.wxh.dto.People;
import com.wxh.sessionfactory.HibernateSessionFactory;
public class Test {
public static void main(String[] args) {
Session session=HibernateSessionFactory.getSession();
IdCard card=new IdCard(1001,"张三");
People people=new People(1,"男","10086","湖北武汉",card);
//session.save(card);
// session.save(people);
// session.beginTransaction();
// session.getTransaction().commit();
IdCard c=session.get(IdCard.class, 1001);
System.out.println(c.getPeople().getPhone());
}
}