What is Hybernet 1

[Hibernate] 1: 1 relationship

A couple of things from me:

The mapping from user to person in the User class looks pretty good:


But the id field should look like this:


Because of the GenerationType.AUTO, I'm not 100% sure because I usually use GenerationType.SEQUENCE with database-side sequences. I have also left out the @NotNull annotation, because as far as I know this is the same as the addition nullable = false in the @ Column / @ JoinColumn annotation. (Please correct if i am wrong)

In the person class, the id field would also have to be changed:


The @NotNull things I would remove all here too.

Again: I've never worked with GenerationType.AUTO. But as I understand it must name = "attribute_id"in the @Column annotation must be the same as in the corresponding table.


And to the suggestion with the mappedBy:
This is what you do when you want to build a bi-directional relationship. Using the example here:

At the moment 1 user object has 1 person object
A user object therefore knows exactly which person object belongs to it. But a person-object knows Notto which user object it belongs ...
In order to achieve this you have to have the following in the Person class:


That is all. A mappedBy on bothSides of the relationship cannot work! The mappedBy must also specify an attribute name (here: person). No table column. This tells Hibernate that the relationship to the user has already been implemented in the user class through the person attribute.


So much for that. I am far from an expert in the field, and what I have written here is based on my own experience with Hibernate.

Please correct if there was something fundamentally wrong!


greeting