When I persist new created entity by JPA-Hibernate, some setter methods are called. Why? Hibernate must read all properties, but why values (not ID) are set back to entity?
CODE
public void run(){
System.out.println("Before transaction");
loadInitialData();
System.out.println("After transaction");
}
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
private void loadInitialData() {
Contract contract = new Contract();
contract.setAmountMonth(new BigInteger("42"));
System.out.println("Before persist");
entityManager.persist(contract);
System.out.println("After persist");
}
OUTPUT
Before transaction
Contract.setAmountMonth null -> 42 ---- OK, my call
Before persist
Contract.getAmountMonth 42 -> 42
Contract.setAmountMonth 42 -> 42 !!!!!! WHY?
After persist
Contract.getAmountMonth 42 -> 42
Contract.getAmountMonth 42 -> 42
Contract.getAmountMonth 42 -> 42
After transaction
SPRING-CONFIGURATION
<persistence-unit name="org.drools.persistence.jpa.local">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>jdbc/dataSource</non-jta-data-source>
<mapping-file>META-INF/orm.xml</mapping-file>
<class>org.drools.persistence.info.SessionInfo</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.transaction.flush_before_completion" value="true" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
</properties>
</persistence-unit>
ENTITY
public class Contract {
private volatile long ceresID;
private volatile BigInteger amountMonth;
public long getCeresID() {
return ceresID;
}
public void setCeresID(long ceresContractID) {
this.ceresID = ceresContractID;
}
public BigInteger getAmountMonth() {
System.out.println("Contract.getAmountMonth " + this.amountMonth + " -> " + amountMonth);
return amountMonth;
}
public void setAmountMonth(BigInteger amountMonth) {
System.out.println("Contract.setAmountMonth " + this.amountMonth + " -> " + amountMonth);
this.amountMonth = amountMonth;
}
}
ENTITY-MAPPINGS
<entity-mappings version="2.0" xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
<entity class=".....Contract" access="PROPERTY">
<attributes>
<id name="ceresID" access="FIELD">
<generated-value strategy="AUTO"/>
</id>
</attributes>
</entity>
</entity-mappings>
Stacktrace of second setAmountMonth
at Contract.setAmountMonth(Contract.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:66)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:583)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:229)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3822)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:299)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:672)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy29.persist(Unknown Source)
at DBPersister.persistOrMerge(DBPersister.java:93)
LIBRARY VERSIONS
- java 1.6.0_29
- hibernate 3.6.0
- spring 3.0.5
With this stacktrace answer can be easily found in Hibernate source.
In order to save an object Hibernate extracts the state from it. That extracted state can be changed during save by custom
Interceptoror to assign value to the version field. If state have been changed, Hibernate writes it back to the object (all fields, it doesn’t track which fields have been changed), that’s what you observe.