I think I've stumbled on a pretty dangerous bug. Assume the following case:
Code:
static Affiliate affiliate;
@Transactional
@Test
@Rollback(false)
public void testAffiliateCreate() {
affiliate = dataPoolFactory.getAffiliate(); // get an object full of random-filled fields
affiliate.setId(null);
dataLayerWms.saveOrUpdate(affiliate);
// assume DB fails at this point i.e. before the rollback
}
@Test
public void testAffiliateView() {
System.out.println(affiliate.getId());
}
Now assume that the save goes thru in the first method, but then the database performs a rollback. At this point the affiliate object has already been given an ID by hibernate (Affiliate table has an auto-inc key).
As you can see, in the second method, the id has been assigned even though it's not valid anymore. If you just attempt to save it again you can potentially clobber an existing record. The problem is that Hibernate should only set the ID when the transaction has been committed and not assume all will be well upon insert.
Comments?