@ManyToOne and @OneToOne on the same entity

jpa cascade persist one-to-many example
one to one mapping in hibernate
jpa one-to-one unidirectional
hibernate onetoone
jpa many-to-many
jpa many-to-many bidirectional example
many-to-many mapping
onetomany(mappedby)

Let's say we have these two entities:

@Entity
class Address{
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long bookId;

   @ManyToOne
   @OneToOne
   private User user;

   ...
}

@Entity
class User{
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long userId;

   @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
   private List<Address> addresses;

   @OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
   private Address principalAddress;

   ...
}

As you can see I have two annotation on top of User entity inside Address class (@ManyToOne and @OneToOne). The point is, I know it is wrong but I don't know how to map it right. Is there a design problem? The logic is that a User has a list of addresses and one and only principal address. How can I map it correctly? any idea?

In situations like this what you can do is have flag "isPrincipalAddress".

@Entity
class Address{
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long bookId;

   private Boolean isPrincipalAddress;

   @ManyToOne
   private User user;

   ...
}

@Entity
class User{
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long userId;

   @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
   private List<Address> addresses;

   ...
}

Hibernate, Both entities share now the same value and it is not necessary any more the use of @GeneratedValue for the generation of new ids in tuition. When you model your database, you will most likely define several many-to-one or one-to-many associations. And it’s, of course, the same when you model your entities. It’s quite easy to do that with JPA and Hibernate. You just need an attribute that represents the association and annotate it with a @ManyToOne or @OneToMany association. But as easy as it seems, there are several pitfalls that you can avoid by following a few best practices.

Just create a boolean isPrimaryAddress field in the Address class

Using the same entity in both OneToMany and OneToOne � Issue #9 , I have found an error while versioning entities like: (error is while auditing Customer entity) class Address { /** * @ORM\Column */ protected� The @ManyToOne Annotation As we've seen in section 2, we can specify a many-to-one relationship by using the @ManyToOne annotation. A many-to-one mapping means that many instances of this entity are mapped to one instance of another entity – many items in one cart. The @ManyToOne annotation lets us create bidirectional relationships, too.

Adding a flag in the library class is not a good solution. It's a very bad proposition. All Users rely on one library class Address. And all of them have (or may have) different 'primary address'. So, you need to create a relation structure (table) or a field in User class. I think your initial variant is better, just use in @OneToOne option 'mappedBy' which relies on the User's field (for example, 'primaryAddress' - a reference to Address(bookId)).

Addition: if you use an annotation @OneToOne (and other too) it's not necessary to have two-direction relation, you can have @OneToOne in User class but do not have it in Address class, usually it does not have any sense.

@OneToMany/ManyToOne and @OneToOne relationship between , @OneToMany/ManyToOne and @OneToOne relationship between same entities . Post by: Adriano Meis , Greenhorn. Dec 10, 2007 16:03:00. I explain quickly� By default, the @ManyToOne association assumes that the parent-side entity identifier is to be used to join with the client-side entity Foreign Key column. However, when using a non-Primary Key association, the referencedColumnName should be used to instruct Hibernate which column should be used on the parent side to establish the many-to-one database relationship.

Best Practices for Many-To-One and One-To-Many Association , And it's, of course, the same when you model your entities. It's quite Bidirectional one-to-many and both many-to-one association mappings are fine. But you� Introduction There are many ways you can map a one-to-one relationship with Hibernate. In this post, I’m going to demonstrate which mapping is the most efficient one from a database perspective. Domain Model For the following examples, I’m going to use the following Post and PostDetails classes: The Post entity is the parent, while the PostDetails is the child association because the

Ultimate Guide, JPA and Hibernate support the same associations as you know from your one- to-one associations,; many-to-one associations and; many-to-many associations. Let's take a look at the unidirectional mapping on the OrderItem entity first. @Target(value={METHOD,FIELD}) @Retention(value=RUNTIME) public @interface OneToOne. Defines a single-valued association to another entity that has one-to-one multiplicity. It is not normally necessary to specify the associated target entity explicitly since it can usually be inferred from the type of the object being referenced.

How to synchronize bidirectional entity associations with JPA and , Learn how to synchronize bidirectional @OneToMany, @OneToOne, and @ ManyToMany entity associations when using JPA (Java Persistence API) and Hibernate. LAZY , for the very same reason we explained before. Here we go. Note that this is not self-referencing as the original issue mentions but the same problem exists. When creating a new thread the workflow is: Post a new comment to a route that advises it is a new thread rather than a reply; Create a new Thread Entity; Create a new Comment Entity; Associate the Comment with the Thread

Comments
  • I guess you should consider to add additional foreign key to User table that will refer to Address table for the principal address. Imagine the following situation: you have two users - u1, u2. Both have addresses - adr1, adr2. And adr1 is principal for u1, but adr2 is principal for u2.