I am using Hibernate 4.1 trying to call a PreInsertEventListener to update the entity before inserting it into the database, based on the article here: http://anshuiitk.blogspot.ca/2010/11/hibernate-pre-database-opertaion-event.html
public class PreInsertListener implements PreInsertEventListener {
@Override
public boolean onPreInsert(PreInsertEvent event) {
Product product = (Product)event.getEntity();
String barcode = "B" + product.getProductId();
product.setBarcode(barcode);
// Update the state value that will be persisted
String[] properties = event.getPersister().getEntityMetamodel().getPropertyNames();
List<String> propertiesList = Arrays.asList(properties);
event.getState()[propertiesList.indexOf('barcode')] = barcode;
}
}
When I debug it, it is executing the PreInsertListener code, but the values inserted into the database do not contain the changes from the code. This used to work in Hibernate 3. What am I missing here?