JPA Annotations and Mapping

I am working on a java application and am using JPA to interact with a database, I have two important questions:

  • I want to make bi-directional communication between two classes, since I need to access data from both sides. Take the case of two classes A and B with A * -1 B (like a UML diagram, A has a single B and B has multiple A ...).

in A:

@ManyToOne
private B attribute; 

      

in B

@OneToMany
private List<A> list;

      

Is it enough to make a two-way link? or is it necessary to use mappedBy?

  1. Which brings us to my second question, if mappedBy is placed on the wrong side, will it just have a performance impact or worse? (data is not saved)? For example, in the case of cardinality 1- * we have no choice, MappedBy should be on the OneToMany side, in which case:

in B

@OneToMany(mappedBy = "attribute")
private List<A> list;

      

Knowing that I will not create class B and create a list and assign objects, I will not do anything in side B. I will just create multiple classes A and every time I assign an object B to it, so I can have multiple classes A that have the same B affected, and I want B to automatically update this link and its list A.

+3


source to share


3 answers


Is it enough to make a two-way link? or is it necessary to use mappedBy?

This Bi-directional

is a must for a relationship .

Which brings us to my second question, if mappedBy is placed on the wrong side, will it just affect performance or worse? (data is not saved)?



Worse - it won't work, but it won't be silent, you will see exceptions.

Having said that, it's easy to understand. He goes with @OneToMany

.

This may help you understand this more.

+2


source


  • mappedBy

    should be added to an object that has no foreign key in its table (most likely B

    in this case).
  • If it mappedBy

    's on the wrong side, you should see an exception.


+1


source


Is it enough to make a two-way link? or is it necessary to use mappedBy?

Not really. You need an attribute MappedBy

for a many-to-one bidirectional relationship on the back - this is the side that has no foreign key and is always one of the parties in the one-to-one relationship. You also need joincolumn information for many sides of the relationship.

So, in short:

The relationship between the two parties is the own side of the relationship - @JoinColumn . This must be specified in both unidirectional and bidirectional relationships

The one-to-many side โ€” the back side โ€” is mapped to an attribute on that side. This must be specified if the relationship is bidirectional.

@Entity
public class A โ€ฆโ€ฆ..
//Owning side of the relationship with the @JoinColumn annotation.
    @ManyToOne
    // Assume TABLEPK column holds PK of B table
    @JoinColumn (name = "TABLEBPK")
    private B attribute;


@Entity
public class B โ€ฆโ€ฆ

//Inverse side of the relationship with the MappedBy attribute.
    @OneToMany(MappedBy = "attribute")
    private List<A> list;

      

Which brings us to my second question, if mappedBy is placed on the wrong side, will it just affect performance or worse?

It won't work. Just put him on the other side of the relationship.

Knowing the fact that I will not create class B and create List and assign objects, I will not do anything in side B. I will just create repeats of classes A and every time I assign object B to it, so I can have multiple classes A that have the same object B affected, and I want B to automatically update this link and its list.

In this case, you create a class A with an attribute field populated with instance B. Now when you save A is a new instance, it will contain an instance of B in the attribute field, which may or may not be new. We want JPA to continue with A and then go through the relationship and save B as well. If B already exists in the context of perssitence, then ignore it. Adding CascadeType.PERSIST

will achieve this.

@Entity
public class A โ€ฆโ€ฆ..
//Owning side of the relationship with the @JoinColumn annotation.
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn (name = "TABLEBPK")
    private B attribute;

      

These guys write very well about it ...   "Pro JPA 2 Mastering the Java Persistence API" by Mike Keith and Merrick Schnikariol.

+1


source







All Articles