In this article we'll look closer to a one-to-one and mapping it in the JPA.
can say that the one-to-one relationship is a special case jeden-do-wielu/wiele-do-jednego discussed by me in the article JPA - the relationship one-to-many and many-to-one . People who are not familiar with the mapping of that relationship, I would encourage you first read that article.
A one-to-one is also in some way connected with the concept of inline objects discussed in the article I JPA - Embedded objects (components) . Well, these embedded objects are interchangeable with a one-to-one to improve performance.
first Example - a problem area
example in this article are largely taken from the JPA - Embedded objects (components) .And here's the object model:
is a system of two classes of
User
and
Address, where User
aggregates completely Address
. It is worth noting that this type of bond, in which the existence of one entity is completely dependent on the existence of another being, is very common in relationships one-to-one. Like the relationship jeden-do-wielu/wiele-do-jednego we can distinguish the parent entity ( User
) and being a child (Address
). As for one being the parent entity is only one child, in order to achieve better performance (at the expense of standardization) are often used instead of the overt embedded objects of a one-to-one. In the relational world sample model will be presented as follows:
This model shows that the implementation of a one-to-one on the side of the database is very similar to the implementation jeden-do-wielu/wiele-do-jednego relationship, but more on that a bit later.
second Bidirectional one-to-one bidirectional relationship
is one in which both objects are mutually aware that they are in a relationship from one object, you can navigate / move to the second. However, before proceeding to discuss the mapping of this relationship, first I would write a few sentences about the database implementation.Well, the database implements is a one-to-one by adding a foreign key to one of the two entities. It is thus quite similar to the implementation jeden-do-wielu/wiele-do-jednego relationship, in which the subordinate entity has a foreign key to the parent entity. With the difference that in this case there is no requirement that the subordinate entity owned bonds and may also be the parent entity. At the same time that there was indeed a one-to-one, and could not degenerate into jeden-do-wielu/wiele-do-jednego relationship, it must be assumed Uniqe constraint'a the foreign key column.
when you map one-to-one fundamental
role annotation @ OneToOne
to be determined field-binding sites are in this relationship. Interestingly, the available attributes for this endorsement is the sum of the attributes available for the annotation @ OneToMany
and @ ManyToOne
, which highlights the similarity of a one-to-one relationship to jeden-do-wielu/wiele-do-jednego and simultaneously enhances the high symmetry bonds in the relationship. Optionally, you can use @ JoinColumn annotation
, which will serve the foreign key specification - the column name and the definition of unique constraintu on this column. Acc. JPA specification defaults column Foreign key to nazywałaby [nazwa_drugiej_tabeli] _ [nazwa_klucza_głównego_drugiej_tabeli] and that was not founded unique constraint when generating the database schema. fragment mapping class
User
: @ Entity @ Table (name = "user") public class User {@ OneToOne (cascade = CascadeType.ALL, optional = false) @ JoinColumn (name = "addr_id", unique = true ) private Address address; ... }fragment mapping class
Address: @ Entity @ Table (name = "address") public class Address {@ OneToOne (mappedBy = "address") private User user; ... Semantics}annotation attributes
@ OneToOne
is the same as for those annotations @ OneToMany
and @ ManyToOne
. I refrain from discussing them again because it is already done in the article JPA - the relationship one-to-many and many-to-one . worth noting that these mappings are correct, assuming that the owner is an entity relationship
User
. However, a slightly different look to the owner if the relationship was Entity Address
. First of all annotation @ JoinColumn
znaczyłaby to check Address.user
not User.address
. A similar conversion would meet the attribute @OneToOne.mappedBy
. third Unidirectional one-to-one Unidirectional relationship
is one in which only one object is aware of being in a relationship with another object, and only from him, you can navigate / move to the second object. Database programmingunidirectional one-to-one and its mapping is in part the same as in the case of two-way relationship. With the difference that we define the mapping for only one of two entities, which must be the owner of bonds - have a foreign key to another entity. What is interesting in one way relationship, as an alternative to annotation
@ OneToOne
can use the @ ManyToOne
- as someone so much likes to obfuscate the model;) True @ ManyToOne
is not possible to determine the attribute mappedBy
, but in the case of one-way relationship and the use of this attribute so it would be impossible. 4th Support Resources
JPA specificationJPA - the relationship one-to-many and many-to-one
JPA - Embedded objects (components)
0 comments:
Post a Comment