Home > Back-end >  Question about JPA Owner and Non-Owner @OneToMany
Question about JPA Owner and Non-Owner @OneToMany

Time:06-14

I'm starting to study jpa now and I have a question about the concept of owner and non-owner, according to an article I read, the non-owner will be every attribute that doesn't have mappedBy, but when there is a bidirectional relationship, without the mappedBy notation, I am not sure if there is an owner of a relationship, I also noticed that the mappedBy option only appears when it is @OneToMany and not @ManyToOne, that is, what really defines the owner of the relationship and how it affects in practice

initial data which is loaded every time when running the application

Sql

insert into client (id, name) values (null, 'Marina');
insert into client (id, name) values (null, 'Joana');

insert into demand(id, name) values(null, "Banana");
insert into demand(id, name) values(null, "Apple");
insert into demand(id, name) values(null, "jabuticaba");

Demand.java

@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Entity
@Table(name = "demand")
public class Demand {

    @Id
    @EqualsAndHashCode.Include
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne
    private Client client;

    private String name;
}

Client.java

@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Entity
@Table(name = "client")
public class Client {

    @EqualsAndHashCode.Include
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;

    @OneToMany
    private List<Demand> demand;
}

Main.java

public class Main {

public static void main(String[] args) {
    
     EntityManagerFactory entityManagerFactory = 
              Persistence.createEntityManagerFactory("unit");
    
     EntityManager entityManager = entityManagerFactory.createEntityManager();
     Client client = entityManager.find(Client.class, 1);
    
     Demand demand1 = entityManager.find(Demand.class, 1);
     Demand demand2 = entityManager.find(Demand.class, 2);
     Demand demand3 = entityManager.find(Demand.class, 3);
    
     entityManager.getTransaction().begin();
     client.setDemand(Arrays.asList(demand1, demand2, demand3));
     entityManager.getTransaction().commit();
    
     client = entityManager.find(Client.class, 1);
     System.out.println("Client name: "   client.getName()   " Demand name "   
     client.getDemand().get(0).getName());
     System.out.println("Client name: "   client.getName()   " Demand name "   
     client.getDemand().get(1).getName());
   }
}

My final question is, who is the owner of the relation, since I didn't use mappeBy ? Another thing, I noticed that it creates an intermediate table, when I don't use mappedBy, if anyone can give me many details on how mappedBy works, I would be very grateful.

CodePudding user response:

There is no any owner when you don't use mappedBy annotation. Its just relation between two table with independent one. Hibernate doesn't know which column exactly is, so you have to show it which is which.

CodePudding user response:

Your assumption is wrong. Any field without a mappedby IS the owner - mappedby signifies the 'other side' defines the relationship, and so the other side owns it. It is only used when using java bidirectional relationships to map over database unidirectional foreign keys.

In your

@ManyToOne
private Client client;

and

@OneToMany
private List<Demand> demand;

Example, you've set up two separate relationships that have no connection to each other. Both will 'own' their specific relationship, and the tables will require two separate FK relationships to map; Demand table will have a FK to represent its client, while the Demand table will have another FK setup to represent the Client 1:M to demand relationship.

Now, if you defined them to use the same foreign key, you'd have (or should have) warnings and errors stating that you have two writable mappings for the same foreign key. Both sides would attempt to write into the same foreign key, causing inefficiencies and issues if the object model isn't in synch.

  • Related