0

休眠 (3.6.5) 和 EhCache (2.4.2)

VehicleAssembly 是一个具有状态转换的持久性类。随着对象经历各个阶段,它会更新为最新状态并保存在数据存储中。

状态转换示例

在此处输入图像描述

现在,在下一步查询 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(已提交)。

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>

任何关于确定这个幽灵对象的指针都会有很大帮助。

4

0 回答 0