What does the "unowned side" part mean in the following?: "3.16. Many-to-one A…
- User
What does the "unowned side" part mean in the following?: "3.16. Many-to-one A many-to-one association is the most basic sort of association we can imagine. It maps completely naturally to a foreign key in the database. Almost all the associations in your domain model are going to be of this form.
Later, we’ll see how to map a many-to-one association to an association table.
The @ManyToOne annotation marks the "to one" side of the association, so a unidirectional many-to-one association looks like this:
class Book { @Id @GeneratedValue Long id;
code@ManyToOne(fetch=LAZY) Publisher publisher; ...} Here, the Book table has a foreign key column holding the identifier of the associated Publisher.
A very unfortunate misfeature of JPA is that @ManyToOne associations are fetched eagerly by default. This is almost never what we want. Almost all associations should be lazy. The only scenario in which fetch=EAGER makes sense is if we think there’s always a very high probability that the associated object will be found in the second-level cache. Whenever this isn’t the case, remember to explicitly specify fetch=LAZY.
Most of the time, we would like to be able to easily navigate our associations in both directions. We do need a way to get the Publisher of a given Book, but we would also like to be able to obtain all the Books belonging to a given publisher.
To make this association bidirectional, we need to add a collection-valued attribute to the Publisher class, and annotate it @OneToMany.
Hibernate needs to proxy unfetched associations at runtime. Therefore, the many-valued side must be declared using an interface type like Set or List, and never using a concrete type like HashSet or ArrayList.
To indicate clearly that this is a bidirectional association, and to reuse any mapping information already specified in the Book entity, we must use the mappedBy annotation member to refer back to Book.publisher.
@Entity class Publisher { @Id @GeneratedValue Long id;
code@OneToMany(mappedBy="publisher") Set<Book> books; ...} The Publisher.books field is called the unowned side of the association.
Now, we passionately hate the stringly-typed mappedBy reference to the owning side of the association. Thankfully, the Metamodel Generator gives us a way to make it a bit more typesafe:
@OneToMany(mappedBy=Book_.PUBLISHER) // get used to doing it this way! Set books; We’re going to use this approach for the rest of the Introduction.
To modify a bidirectional association, we must change the owning side.
Changes made to the unowned side of an association are never synchronized to the database. If we desire to change an association in the database, we must change it from the owning side. Here, we must set Book.publisher.
- Assistant
In the given context, the "unowned side" refers to the side of the association that does not own the relationship. In this case, the association is between the Publisher and Book entities, where Publisher is the owning side and Book is the unowned side.
When an association is bidirectional, both sides need to be specified for navigation purposes. In this example, the Publisher class has a "books" field of type Set, which represents the collection of books associated with the publisher. This "books" field is the unowned side of the association because it does not own the relationship between publisher and book.
- User
I guess what I meant is what is ownership? Be concise.
- Assistant
In this context, ownership refers to the side of the association that is responsible for managing the relationship between entities. The owning side is responsible for storing and updating the foreign key column(s) in the database. Conversely, the unowned side is not responsible for managing the relationship and does not update the foreign key column(s) in the database.