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();
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();
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();
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 ..
java orm hibernate
blow
source share