JPA-Hibernateによって新しく作成されたエンティティを永続化すると、いくつかのセッターメソッドが呼び出されます。なんで?Hibernateはすべてのプロパティを読み取る必要がありますが、なぜ値(IDではない)がエンティティに戻されるのですか?
コード
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");
}
出力
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
春の構成
<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>
実在物
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 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>
2番目の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)
ライブラリバージョン
- java 1.6.0_29
- hibernate 3.6.0
- 春3.0.5