【译文】【学习】Hibernate 一对一案例
【目标读者】
本教程为那些需要理解Hibernate框架以及使用Hibernate框架应用的java程序员而设计。
【前置条件】
你需要先懂得java语言以及sql基本知识。
【教程目录】
Introduction to hibernate framework
Hibernate hello world example in eclipse
Difference between openSession and getCurrentSession
Hibernate one to one mapping example
Hibernate one to many mapping example
Hibernate many to many mapping example
Hibernate inheritance:Table per class hierarchy
Hibernate inheritance:table per subclass
Hibernate inheritance:Table per concrete class
Difference between openSession and getCurrentSession
Difference between get and load
Spring MVC Hibernate MySQL CRUD example
【Hibernate 映射关系】
关联映射的一些定义
单向一对多:一方有集合属性,包含多个多方,而多方没有一方的引用。用户--->电子邮件
单向多对一:多方有一方的引用,一方没有多方的引用。论文类别---> 类别
双向一对多:两边都有多方的引用,方便查询。班级---> 学生
双向多对一:两边都有多方的引用,方便查询。
单向多对多:需要一个中间表来维护两个实体表。论坛--->文章
单向一对一:数据唯一,数据库数据也是一对一。舰船---> 水手
主键相同的一对一:使用同一个主键,省掉外键关联。客户---> 地址
单向:关系写哪边,就由谁管理。
双向:一般由多方管理。
【注意:】
只有是双向关联关系,都要求加上mappedby,该属性在@ManyToOne关联映射关系中没有,因为@ManyToOne通常定义外键关联。@OneToMany通过此属性将控制权交给多端。
双向的不改变数据库的结构,只影响Java对象间的关系(互相引用),双向可以让双方互相引用,更好的查询。
【Hibernate 一对一案例】
举例:一个国家有一个首都,如下显示关系图:

Country 有一个外键Capital_Name指向Capital。
在主控表的实体类中定义受控表的实体类字段,同时使用@OneToOne以及@JointColumn注解该字段,指明在主控表的外键名字,主控表外键字段关联到受控表主键。
Country.java
package org.arpit.javapostsforlearning; 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="COUNTRY") public class Country { @Id @Column(name="Country_Name") String countryName ; @OneToOne @JoinColumn(name="Capital_Name") Capital capital; @Column(name="Country_Population") long countryPopulation; public Country() { } public Country(String countryName, long countryPopulation) { this.countryName = countryName; this.countryPopulation = countryPopulation; } public long getCountryPopulation() { return countryPopulation; } public void setCountryPopulation(long countryPopulation) { this.countryPopulation = countryPopulation; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public Capital getCapital() { return capital; } public void setCapital(Capital capital) { this.capital = capital; } } Read more at http://www.java2blog.com/2013/02/hibernate-one-to-one-mapping-example.html#J54gfJ22CxSf8sZD.99
Capital.java
package org.arpit.javapostsforlearning;import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="CAPITAL") public class Capital { @Id @Column(name="Capital_Name") String capitalName; @Column(name="Capital_Population") long capitalPopulation; public Capital() { } public Capital(String capitalName, long capitalPopulation) { super(); this.capitalName = capitalName; this.capitalPopulation = capitalPopulation; } public String getCapitalName() { return capitalName; } public void setCapitalName(String capitalName) { this.capitalName = capitalName; } public long getCapitalPopulation() { return capitalPopulation; } public void setCapitalPopulation(long capitalPopulation) { this.capitalPopulation = capitalPopulation; } } Read more at http://www.java2blog.com/2013/02/hibernate-one-to-one-mapping-example.html#J54gfJ22CxSf8sZD.99
一对一关系中,双方都可以建立外键,但一般是只在一方建立外键,具体哪一方看程序以及设计如何选择。
在关联方式中有单向和双向之分,我们在上面说的是单向的关系。如果要变为双向的关联,为了防止受控方也生成一个外键,需要在受控方(Capital)使用@OneToOne(mappedBy属性来指定主控方的属性,当然,首先受控方需要增加一个主控方的对象变量。
举例如下:
package ai.dolphin.testhibernate.entitys; /** * @author pengys * @date 2017/1/4 */ import javax.persistence.*; @Entity @Table(name = "COUNTRY") public class Country { @Id @Column(name = "Country_Name") String countryName; @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "Capital_Name") Capital capital; @Column(name = "Country_Population") long countryPopulation; public Country() { } public Country(String countryName, long countryPopulation) { this.countryName = countryName; this.countryPopulation = countryPopulation; } public long getCountryPopulation() { return countryPopulation; } public void setCountryPopulation(long countryPopulation) { this.countryPopulation = countryPopulation; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public Capital getCapital() { return capital; } public void setCapital(Capital capital) { this.capital = capital; } }
package ai.dolphin.testhibernate.entitys; /** * @author pengys * @date 2017/1/4 */ import javax.persistence.*; @Entity @Table(name = "CAPITAL") public class Capital { @Id @Column(name = "Capital_Name") String capitalName; @Column(name = "Capital_Population") long capitalPopulation; // 这里的capital是主控方OneToOne注解过的字段, 如果大家都是在get或者set方法注解的话都要改成在方法上注解,mappedBy需要指向属性名 // 去掉get和set,且首字母是小写 @OneToOne(mappedBy = "capital") Country country; public Capital() { } public Capital(String capitalName, long capitalPopulation) { super(); this.capitalName = capitalName; this.capitalPopulation = capitalPopulation; } public String getCapitalName() { return capitalName; } public void setCapitalName(String capitalName) { this.capitalName = capitalName; } public long getCapitalPopulation() { return capitalPopulation; } public void setCapitalPopulation(long capitalPopulation) { this.capitalPopulation = capitalPopulation; } }

浙公网安备 33010602011771号