1

I'm new to hibernate so I don't understand some basic things. I have Entity A and B. And it's a one to many relationship. So A can have multiple B's. Below is code to save when adding a new B to A. That's working.

    A a= this.aService.getAById(AID);
    b.setA(a);
    a.getBSet().add(b);
    this.aService.saveA(a);

But how can I edit one B entity? Do I first have to remove the B entity that I want to edit from the Set? Really sorry if it's an obvious question. But I already searched with Google and the only examples I can find is when you make new entities and not edit.

Urban
  • 585
  • 2
  • 13
  • 28

2 Answers2

0

You need to first get B from the database.

B b = this.bService.getBById(BID);
...
//update b
this.bService.updateB(b);
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
  • So retrieve B that I want to edit. update B and that's it? Don't i have to edit the set that A which contains the B that I want to edit? – Urban Oct 31 '17 at 12:24
  • You might as well want to upvote and accept this answer so that it could be of help to others ;) – Abdullah Khan Nov 02 '17 at 08:30
-1
//Whether you want update entity B:
Public void updateBEntity(Integer idB) {
 B b = session.get(B.class, idB);
//For edit you only have to use the set's methods:
b.setName(anything);
b.setPosition(2);
//final y, that's all
session.merge(b);
} 
//In your class controller or Action 

More information about merge/persist:

What is the difference between persist() and merge() in Hibernate?

SiHa
  • 7,830
  • 13
  • 34
  • 43
peterzinho16
  • 919
  • 1
  • 15
  • 18