Hibernate中的Dirty Read

时间:2011-12-08 02:57:38

标签: hibernate

CustomerType实体:

public class CustomerType implements java.io.Serializable {

private Integer customerTypeId;
private String customerDesc;
private Set customerInfos = new HashSet(0);

public CustomerType() {
}

public CustomerType(String customerDesc) {
    this.customerDesc = customerDesc;
}

public CustomerType(String customerDesc, Set customerInfos) {
    this.customerDesc = customerDesc;
    this.customerInfos = customerInfos;
}

public Integer getCustomerTypeId() {
    return this.customerTypeId;
}

public void setCustomerTypeId(Integer customerTypeId) {
    this.customerTypeId = customerTypeId;
}
.....................

CustomerType与此实体有一对多的关系

public class CustomerInfo implements java.io.Serializable {

private Integer customerId;
private CustomerType customerType;
private String name;
    .................................

我在DaoImpl类

中使用了这个方法
 @Override
public int save(Object object) {
    try{
        transaction = session.beginTransaction();
        session.saveOrUpdate(object);
        session.flush();
        transaction.commit();
        return 1;
    }catch (Exception e) {
        transaction.rollback();
        e.printStackTrace();// TODO: handle exception
        return 0;
    }
}

我在更新对象时调用它

public String updateCustomerType(){
    this.customertype = this.daoManager.findCustoType(Integer.parseInt(this.customerTypeID));
    this.customertype.setCustomerDesc(this.custoTypeDesc);
    this.daoManager.save(this.customertype);
}

此方法成功更新数据库,但是当我显示与CustomerType有关的CustomerInfo列表时,CustomerInfo中的CustomerType参数不是udpated

org.hibernate.Query q = session.createQuery("select customerInfo from CustomerInfo customerInfo");
return q.list();

我的错误。等待你的评论。谢谢,

1 个答案:

答案 0 :(得分:0)

我相信您的问题是您想要读取未提交的数据。

我没有针对您的特定问题对此进行测试,但您可以尝试:

session.connection().setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);

还有一个很好的堆栈来帮助解释read_uncommited:

Why use a READ UNCOMMITTED isolation level?