Update entity with many-to-one bidirectional mapping with Hibernate session

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 !

Show us the mappings and the data access code so we can see what you are doing.

1 Like

@vlad Thanks for responding! I’ve just edited my question with the code in it.

You need to add the generated SQL statements as well so we can see what happens.

Or, try to replicate it with this test case template.

1 Like

@vlad Ohh yes I will add the generated SQL statements and think more about this.
Nothing but thanks !