为什么在实体持续时设置属性?

时间:2012-04-25 14:10:49

标签: java spring hibernate jpa persistence

当我通过JPA-Hibernate持久保存新创建的实体时,会调用一些setter方法。为什么? Hibernate必须读取所有属性,但为什么值(而不是ID)被设置回实体?

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");
}

输出

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

弹簧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-映射

<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>

第二个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
  • spring 3.0.5

1 个答案:

答案 0 :(得分:0)

使用此堆栈跟踪可以在Hibernate源代码中轻松找到答案。

为了保存对象,Hibernate从中提取状态。可以在保存期间通过自定义Interceptor更改提取状态,或者将值分配给版本字段。如果状态已被更改,Hibernate会将其写回对象(所有字段,它不会跟踪哪些字段已被更改),这是您观察到的内容。