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);
}
分享到:
相关推荐
在上面的配置中, `<many-to-one>` 标签指定了Person实体与IdCard实体之间的一对一唯一外键关联关系,其中unique="true"指定了多的一端的多重性为一。 Hibernate 一对一唯一外键关联映射的应用 在实际应用中,一对...
其中一个实体通过外键关联到另一个实体的主键(注意要模拟一对一关联必须在外键列上添加唯一约束),(3).通过关联表来保存两个实体之间的连接关系(要模拟一对一关联必须在每一个外键上添加唯一约束)。 1.共享主键的...
hibernate多对一关联映射(Hibernate_Many2One) 7 hibernate一对一主键关联映射(单向关联Person---->IdCard) 8 hibernate一对一主键关联映射(双向关联Person<---->IdCard) 9 hibernate一对一唯一外键关联映射...
通常和<one-to-one>联合起来使用。 * 例:@Id * @GeneratedValue(generator = "idGenerator") * @GenericGenerator(name = "idGenerator", strategy = "foreign", * parameters = { @Parameter(name = "property...
many-to-one:标签中对于cascade的取值delete,delete-orphan,all-delete-orphan(只用unique属性值不为true不能出现)慎用 cascade:级联属性 none:不做任何级联操作 save-update:对当前对象执行save,update, ...
Hibernate映射-单向关联 外键一对一关联 在此示例中,我创建两个表:人员和地址。 这两个表都有一个自动递增的id作为主键。 人员表持有地址表中的外键。
2.2.6. 映射复合主键与外键 2.2.7. 映射二级表(secondary tables) 2.3. 映射查询 2.3.1. 映射EJBQL/HQL查询 2.3.2. 映射本地化查询 2.4. Hibernate独有的注解扩展 2.4.1. 实体 2.4.2. 标识符 2.4.3. 属性 2.4...
7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
2.2.6. 映射复合主键与外键 2.2.7. 映射二级表(secondary tables) 2.3. 映射查询 2.3.1. 映射EJBQL/HQL查询 2.3.2. 映射本地化查询 2.4. Hibernate独有的注解扩展 2.4.1. 实体 2.4.2. 标识符 2.4.3. 属性 2.4.3.1. ...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...
7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...