3

当我通过 JPA-Hibernate 持久化新创建的实体时,会调用一些 setter 方法。为什么?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>

第二个 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
  • 休眠 3.6.0
  • 春天 3.0.5
4

1 回答 1

0

有了这个 stacktrace 答案可以很容易地在 Hibernate 源代码中找到。

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

于 2012-04-25T16:20:06.107 回答