1.两个实体类一个personanno一个phonenumber都设置为@Entity并且都有自己的@Id
2.在personanno类里新增一个属性phonenumber并且有getset方法
3.在personanno类的getPhoneNumber方法上注解@OneToOne
4.在hibernate.cfg.xml配置两个实体类映射关系
<mapping class="com.meiyoudao.domain.PersonAnno"/>
<mapping class="com.meiyoudao.domain.PhoneNumber"/>
5.常用的hibernate创建语句方法
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
第一个参数,表示是否在数据库中执行.第二个参数,是否在控制台中打印.
package com.meiyoudao.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class PersonAnno {
private int id;
private String name;
private String weight;
private PhoneNumber phoneNumber;
@OneToOne
public PhoneNumber getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(PhoneNumber phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Id
@GeneratedValue
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 String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
package com.meiyoudao.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author User
*
*/
@Entity
public class PhoneNumber {
private int id;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String number;
private String telphone;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
}
<!--hibernate.cfg.xml 配置-->
<mapping class="com.meiyoudao.domain.PersonAnno"/>
<mapping class="com.meiyoudao.domain.PhoneNumber"/>
XML 版本 one2one外键关联
package com.meiyoudao.domain;
/**
* @author meiyoudao
*
*/
public class Person {
private int id;
private String name;
private String weight;
private PhoneNumber phoneNumber;
public PhoneNumber getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(PhoneNumber phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
package com.meiyoudao.domain;
/**
* @author meiyoudao
*
*/
public class PhoneNumber {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String number;
private String telphone;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
}
<?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="com.meiyoudao.domain">
<class name="PhoneNumber">
<id name="id"></id>
<property name="number"/>
<property name="telphone"/>
</class>
</hibernate-mapping>
<?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="com.meiyoudao.domain">
<class name="Person">
<id name="id">
</id>
<property name="name"/>
<property name="weight"/>
<!-- one2one 在XML中配置也是用many-to-one -->
<!-- 只要unique="true"为true就可以了.唯一属性为true-->
<many-to-one name="phoneNumber" unique="true"></many-to-one>
</class>
</hibernate-mapping>
<!--hibernate.cfg.xml中的映射配置-->
<mapping resource="com/meiyoudao/domain/Person.hbm.xml"/>
<mapping resource="com/meiyoudao/domain/PhoneNumber.hbm.xml"/>
需要注意的测试方法
hibernate中是先把两个对象都insert进表中,然后再进行一次update使这两条记录关联起来.
@Test
public void saveUnit(){
session.beginTransaction();
Person person = new Person();
PhoneNumber pn = new PhoneNumber();
pn.setNumber("1234567890");
pn.setTelphone("IPHONE");
person.setPhoneNumber(pn);
person.setName("daodao1");
person.setId(3);
//两个对象都要save
session.save(person);
session.save(pn);
}