Hibernate 1:1双向映射失败 – 实体字段为空

huangapple go评论71阅读模式
英文:

Hibernate 1:1 bidirectional mapping failed - nulled entity field

问题

我目前正在尝试在Hibernate中实现一对一的双向映射。但是,当我打印出来时,我的子实体类中总是得到一个空的属性。

父实体:

@OneToOne(fetch = FetchType.EAGER)
private Laptop laptop;

子实体:

@Fetch(FetchMode.JOIN)
@OneToOne(mappedBy = "laptop")
private Student student;

希望有人可以帮助我。

迄今为止,
Daniel

英文:

Im currently trying to achive an 1:1 bidirectional mapping in hibernate. But I always get a nulled property in my child entity class, when I printed out.

Student{studentId=2, firstname='Benjamin', lastname='Peterson', email='benjamin.peterson@mailbox.org', term=entities.Term@72906e, laptop=Laptop{laptopId=1, manufacturer='Apple', model='Macbook Pro Retina', student=null}}
Student{studentId=3, firstname='Daniel', lastname='Brooks', email='daniel.brooks@mailbox.org', term=entities.Term@4d7a64ca, laptop=Laptop{laptopId=2, manufacturer='Lenovo', model='Thinkpad T420', student=null}}
Student{studentId=4, firstname='Jonas', lastname='Huber', email='jonas.huber@mailbox.org', term=entities.Term@76ff68c5, laptop=Laptop{laptopId=3, manufacturer='Dell', model='XPS 15', student=null}}
Student{studentId=5, firstname='Julian', lastname='Maier', email='julian.maier@mailbox.org', term=entities.Term@6418e39e, laptop=Laptop{laptopId=5, manufacturer='Lenovo', model='Thinkpad T420', student=null}}
Student{studentId=6, firstname='Erika', lastname='Samson', email='erika.samson@mailbox.org', term=entities.Term@3635099, laptop=Laptop{laptopId=4, manufacturer='Hewlett Packard', model='Envy 17', student=null}}
Student{studentId=7, firstname='Carola', lastname='Walker', email='carola.walker@mailbox.org', term=entities.Term@1da1380b, laptop=Laptop{laptopId=6, manufacturer='Dell', model='XPS 15', student=null}}

The parent entity:

@OneToOne(fetch = FetchType.EAGER)
private Laptop laptop;

and the child entity;

@Fetch(FetchMode.JOIN)
@OneToOne(mappedBy = "laptop")
private Student student;

Hopefully there's someone out there which can help me.

So far,
Daniel

答案1

得分: 1

在学生类中执行以下操作:

@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Laptop laptop;

因为你想要自动存储笔记本电脑以及学生详细信息。

英文:

In the student class do the following:

@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
 private Laptop laptop;

As you want to store the laptop automatically along with Student details.

huangapple
  • 本文由 发表于 2020年8月9日 15:30:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63323662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定