简体   繁体   中英

Hibernate update one child in one to many

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.

You need to first get B from the database.

B b = this.bService.getBById(BID);
...
//update b
this.bService.updateB(b);
//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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM