企业网站管理系统最新4湖南岚鸿牛x1 0,莱芜信息招聘平台,最新网游网络游戏,vi设计开题报告现在该继续有关Hibernate的文章了。 最后一个致力于单向OneToOne关联 。 因此#xff0c;今天我将向您展示如何获取双向OneTonOne主键关联 。 本教程中基于前一篇文章的示例。 让我们开始吧。 我将使用以前创建的相同表。 为了建立双向一对一关联#xff0c;我需要更新两个P… 现在该继续有关Hibernate的文章了。 最后一个致力于单向OneToOne关联 。 因此今天我将向您展示如何获取双向OneTonOne主键关联 。 本教程中基于前一篇文章的示例。 让我们开始吧。 我将使用以前创建的相同表。 为了建立双向一对一关联我需要更新两个POJO和保存过程的方式。 让我们考虑一个新版本的Author类 import javax.persistence.*;Entity
Table(nameauthors)
public class Author {IdGeneratedValueprivate Integer id;private String name;OneToOne(mappedByauthor, cascadeCascadeType.ALL)private Biography biography;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public Biography getBiography() {return biography;}public void setBiography(Biography biography) {this.biography biography;}} 变化很小。 我刚刚从传记字段中删除了PrimaryKeyJoinColumn。 在双向关联中出现关联的两个方面- 拥有和反向 。 对于一对一的双向关系拥有方对应于包含相应外键的方。 在我们的情况下拥有方是Author类。 让我们继续。 引用JPA 2规范 双向关系的反面必须通过使用OneToOneOneToMany或ManyToMany批注的maptedBy元素引用其所属的面。 mapledBy元素指定实体中作为关系所有者的属性或字段。 本示例的反面是Biography类。 与Author类相比它需要进行更多必要的更改。 import javax.persistence.*;import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;Entity
Table(namebiographies)
public class Biography {IdColumn(nameauthor_id)GeneratedValue(generatorgen)GenericGenerator(namegen, strategyforeign, parametersParameter(nameproperty, valueauthor))private Integer authorId;private String information;OneToOnePrimaryKeyJoinColumnprivate Author author;public Author getAuthor() {return author;}public void setAuthor(Author author) {this.author author;}public Integer getAuthorId() {return authorId;}public void setAuthorId(Integer authorId) {this.authorId authorId;}public String getInformation() {return information;}public void setInformation(String information) {this.information information;}
} 第一件重要的事情是authorId字段的修饰和其他注释。 ...
GeneratedValue(generatorgen)
GenericGenerator(namegen, strategyforeign,
parametersParameter(nameproperty, valueauthor))
... 在GeneratedValue中我指定生成器的名称“ gen”在GenericGenerator中我定义生成器的策略。 第二件重要的事情是在类中添加具有适当的getter和setter的author 。 ...OneToOnePrimaryKeyJoinColumnprivate Author author;
... 通过这种方式我们获得了双向关联。 现在我们可以从“传记”中访问“作者”反之亦然因为两个对象之间都有相互引用。 现在必须更新对象保存过程 ...public static void main(String[] args) {SessionFactory sessionFactory HibernateUtil.getSessionFactory();Session session sessionFactory.openSession();session.beginTransaction();Author author new Author();author.setName( O. Henry);Biography biography new Biography();biography.setInformation(William Sydney Porter better known as O. Henry...);author.setBiography(biography);biography.setAuthor(author);session.save(author);session.getTransaction().commit();session.close();}
... 请注意现在在添加反面之前我不再坚持拥有面。 但是您可以看到我将传记设置为作者并在以下字符串中将作者设置为传记 。 这是双向关联的主要目的。 代码执行的结果是 Hibernate: insert into authors (name) values (?)
Hibernate: insert into biographies (information, author_id) values (?, ?) 参考 Fruzenshtein的注释博客中来自我们的JCG合作伙伴 Alex Fruzenshtein的双向OneToOne主键关联 。 翻译自: https://www.javacodegeeks.com/2013/03/bidirectional-onetoone-primary-key-association.html