@ManyToOne would be placed on a single item association of an @Entity (or on the table with a foreign key column in the underlying database).
Defines a single-valued association to another entity class that has @ManyToOne multiplicity. It is not normally necessary to specify the target entity explicitly since it can usually be inferred from the type of the object being referenced, since it is a unidirectional mapping.
If the relationship is bidirectional, the non-owning @OneToMany entity side must used the mappedBy element to specify the relationship field or property of the entity that is the owner of the relationship.
The @ManyToOne annotation may be used within an embeddable class to specify a relationship from the embeddable class to an entity class. If the relationship is bidirectional, the non-owning @OneToMany entity side must use the mappedBy element of the @OneToMany annotation to specify the relationship field or property of the embeddable field or property on the owning side of the relationship. The dot (".") notation syntax must be used in the mappedBy element to indicate the relationship attribute within the embedded attribute.
VERY IMPORTANT : The value of each identifier used with the dot notation is the name of the respective embedded field or property.
Annotations optional elements
cascade : (Optional) The operations that must be cascaded to the target of the association.
fetch : (Optional) Whether the association should be lazily loaded or must be eagerly fetched.
optional : (Optional) Whether the association is optional.
targetEntity : (Optional) The entity class that is the target of the association.
Defines strategies for fetching data from the database. The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed. The implementation is permitted to eagerly fetch data for which the LAZY strategy hint has been specified.
BE CAREFUL : Don’t use unidirectional @OneToMany associations if it is possible!
Bidirectional @OneToMany and both @ManyToOne association mappings are fine. But you should avoid unidirectional @OneToMany mapping in your domain model. Otherwise, Hibernate might create unexpected tables and execute more SQL statements than you expected.
Annotation Type ManyToOne Hibernate
Annotation Type OneToMany Hibernate
BE CAREFUL : sometimes this setter doesn't fit our purposes
Be CAREFUL : there is a tricky game here to make bidirectional solution useful in this particular case: user-book(physical one)
This code allows to duplicate books in several users, in this example book3 is owned by user1 and user2.
That is, book3 is present in both arrayList books from both users (user1 and user2) but book3 only has got one user (and only CAN have one user), in this case the last user who execute the setter: user2