Hibernate 1:1 bidirectional mapping failed - nulled entity field

Viewed 50

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 Answers

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.

Related