Refresh OneToMany list after saving an entity in sleep mode

I have a relationship:

// In A.java class @OneToMany(mappedBy="a", fetch=FetchType.LAZY) @Cascade(CascadeType.SAVE_UPDATE) private List<B> bList; // In B.java class @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="id_a") @Cascade(CascadeType.SAVE_UPDATE) private A a; 

Now let's see:

 A a=new A(); // setting A B b=new B(); // setting B b.setA(a); session.save(b); // this save b and a obviously 

Now the "problem":

  • a.getId () -> current new id OK
  • a.getBList () → more null ...

So why is bList not updated in this case?

I tried rebooting after saving, this way:

 A a=new A(); // setting A B b=new B(); // setting B b.setA(a); session.save(b); A loadedA=(A)session.get(A, a.getId()); 

But the downloaded file still has a NULL bList, for example.

Naturally, to avoid this problem, I do it your way:

 A a=new A(); // setting A B b=new B(); // setting B List<B> bList=new ArrayList<B>(); bList.add(b); a.setBList(bList); session.save(a); // this save a and b 

Thus, everything works well, but my question is: Why is Id updated correctly after the save operation and bList is not? Should I query db with select statement to reload the instance correctly?


UPDATE

I have this exception

StaleStateException: batch update returns unexpected row count from update

when I try to save an ORUpdate a object after removing b from it.

 // first delete old b record Session session=HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); a.getBList().remove(b); b.setA(null); session.delete(b); // session.clear(); // this would solve my problem, but is it correct?? session.getTransaction().commit(); // then insert new b record Session session=HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); B b=new B(); a.getBList().add(b); b.setA(a); session.saveOrUpdate(a); session.getTransaction().commit(); // this throw exception 

these two operations, of course, not in the same way, are triggered by the gui event.

is session.clear a real solution? Im doing (maybe) wrong?

Update

deleting session.delete (b) the problem is solved again ... so what is the corretc method? I know ... im totally noob with hibernate ..

+7
java orm hibernate
source share
2 answers

Oh ... ok

 session.refresh(a); 

This work is good ... it's a decision, isn't it?

+4
source share

When working with bi-directional associations, you must establish a link to both sides of the association:

 A a = new A(); B b = new B(); a.getBList().add(b); b.setA(a); session.persist(b); 

And in fact, the general scheme is to implement protective link management methods, for example:

 public class A { @OneToMany(mappedBy="a", fetch=FetchType.LAZY) @Cascade(CascadeType.SAVE_UPDATE) private List<B> bList = new ArrayList<B>(); protected List<B> getListB() { return bList; } protected void setListB(List bList) { this.bList = bList; } public void addBToBs(B b) { bList.add(b); b.setA(this); } //... } 

And the code will look like this:

 A a = new A(); B b = new B(); a.addBToBs(b); session.persist(b); 

This is discussed in the Hibernate tutorial:

+6
source share

All Articles