为什么Hibernate会返回不可缓存对象的陈旧数据?

时间:2013-09-24 06:32:03

标签: java hibernate ehcache

Hibernate(3.6.5)和EhCache(2.4.2)

VehicleAssembly是一个具有状态转换的persitence类。当对象经历各个阶段时,它将使用最新状态进行更新并保存在数据存储区中。

状态转换示例

enter image description here

现在,在下一步中查询VehicleAssembly时,将返回“09/24/2013 11:00:30 PM”的陈旧状态。

query = sessionFactory.getCurrentSession().createQuery("from VehicleAssembly va where va.assemblyId = ? and assemblyType = ?");
            query.setLong(0, Long.parseLong(refnum));
            query.setString(1, assemblyType);
            VehicleAssembly vaMaster = (VehicleAssembly) query.uniqueResult();
//refNum and assembltType are parameters to the method which has this code  

看看结果看来,hibernate从缓存中返回一个旧的VehicleAssembly对象。请注意,VehicleAssembly类不可缓存(类定义中没有@Cache批注)。此外,所有这些更新都发生在不同的会话中,并且数据存储区中的隔离级别为2(Read committed)。

VehicleAssembly类如下

@Entity
@Table(schema = "VEHICLES", name =  "ASSEMBLY_MASTER")
@SequenceGenerator(name="idGenerator", sequenceName = "VEHICLES.ASSEMBLY_MASTER_ID_SEQ1", allocationSize=1)
public class VehicleAssembly implements java.io.Serializable {

    private long id;
    private long assemblyId;
    private String assemblyType;
    private String status;
    private Date updateTs;

    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 16, scale = 0)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "iddGenerator")
    public long getId() {
        return id;
    }

    @Column(name = "ASSEMBLY_ID", nullable = false, precision = 16, scale = 0)
    public long getAssemblyId() {
        return assemblyId;
    }

    @Column(name = "ASSEMBLY_TYPE", nullable = false, length = 120)
    public String getAssemblyType() {
        return assemblyType;
    }

    @Column(name = "STATUS", nullable = false, length = 120)
    public String getStatus() {
        return status;
    }

    @Column(name = "UPDATED_TS", nullable = false, length = 7)
    public Date getUpdateTs() {
        return updateTs;
    }

    //Not addign setters for cosmetic reasons, actually implementation does have setters

}

在应用程序中启用了Hibernate二级缓存

<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>

任何关于钉住这个幽灵物体的指针都会有很大的帮助。

0 个答案:

没有答案