双向@OneToOne主键关联

现在该继续有关Hibernate的文章了。 最后一个致力于单向@OneToOne关联 。 因此,今天我将向您展示如何获取双向@OneTonOne主键关联 。 本教程中基于前一篇文章的示例。 让我们开始吧。

我将使用以前创建的相同表。 为了建立双向一对一关联,我需要更新两个POJO和保存过程的方式。 让我们考虑一个新版本的Author类:

import javax.persistence.*;

@Entity
@Table(name='authors')
public class Author {

	@Id
	@GeneratedValue
	private Integer id;

	private String name;

	@OneToOne(mappedBy='author', cascade=CascadeType.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规范:

双向关系的反面必须通过使用OneToOne,OneToMany或ManyToMany批注的maptedBy元素引用其所属的面。 mapledBy元素指定实体中作为关系所有者的属性或字段。

本示例的反面是Biography类。 与Author类相比,它需要进行更多必要的更改。

import javax.persistence.*;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name='biographies')
public class Biography {

	@Id
	@Column(name='author_id')
	@GeneratedValue(generator='gen')
	@GenericGenerator(name='gen', strategy='foreign', 
	parameters=@Parameter(name='property', value='author'))
	private Integer authorId;

	private String information;

	@OneToOne
	@PrimaryKeyJoinColumn
	private 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(generator='gen')
@GenericGenerator(name='gen', strategy='foreign', 
parameters=@Parameter(name='property', value='author'))
...

在@GeneratedValue中,我指定生成器的名称(“ gen”),在@GenericGenerator中,我定义生成器的策略。 第二件重要的事情是在类中添加具有适当的getter和setter的author

...
	@OneToOne
	@PrimaryKeyJoinColumn
	private 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值