Mapping in parent class: (Admin.java)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "admin")
@Cascade(value={CascadeType.SAVE_UPDATE})
public Set<Phieuxacnhan> getPhieuxacnhans() {
return this.phieuxacnhans;
}
public void setPhieuxacnhans(Set<Phieuxacnhan> phieuxacnhans) {
this.phieuxacnhans = phieuxacnhans;
}
Mapping in child class: (Phieuxacnhan.java)
@ManyToOne(fetch = FetchType.LAZY)
@Cascade(value={CascadeType.ALL})
@JoinColumn(name = "MSCB", updatable=true)
public Admin getAdmin() {
return this.admin;
}
My DAO update admin code: (AdminProcessing.java)
public void saveAdmin(Admin ad) throws HibernateException, ClassNotFoundException {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tran = null;
try {
tran = session.beginTransaction();
session.save(ad);
tran.commit();
}catch(Exception e) {
if(tran!=null)
tran.rollback();
}finally {
session.close();
}
}
My servlet code:
Admin admin = df.getAdmin(mscb); // get an admin by admin id
Phieuxacnhan phieu = df.getPXN(msphieu); // get a phieuxacnhan by phieuxacnhan id
phieu.setAdmin(admin);
admin.getPhieuxacnhans().add(phieu);
AdminProcessing ap = new AdminProcessing();
ap.saveAdmin(admin);
The code run without anr errors or bugs but I do not have any changes in my database.
Thank you for your response !