逻辑删除在休眠的一个常见的地方 [英] Logical delete at a common place in hibernate

查看:109
本文介绍了逻辑删除在休眠的一个常见的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的应用程序使用Spring和Hibernate。

我只允许在我的应用程序中进行逻辑删除,我需要设置字段isActive = false。我没有在所有实体中重复相同的字段,而是使用属性创建了一个Base Class,并为'isActive'创建了getter-setter。

因此,在删除期间,我调用update()方法并将isActive设置为false。



我无法得到这个工作。如果任何人有任何想法,请让我知道。



基本实体

  public abstract class BaseEntity< TId extends Serializable>实现IEntity< TId> {

@Basic
@Column(name =IsActive)
protected boolean isActive;

public Boolean getIsActive(){
return isActive;
}

public void setIsActive(Boolean isActive){
isActive = isActive;


$ / code $ / pre
$ b

子实体 p>

  @Entity(name =Role)
@Table(schema =dbo)
public class MyEntity扩展了BaseEntity {
//剩下的实体
}

Hibernate Util Class

  public void remove(TEntity entity){

//注意:企业数据不应该被删除。
entity.setIsActive(false);
sessionFactory.getCurrentSession()。update(entity);
}


解决方案

setIsActive 方法:

  public void setIsActive(Boolean isActive){
this.isActive = isActive;
}

在您的代码中使用变量名称而不是 this



我想你还应该添加 @MappedSuperclass 注释到你的抽象类来实现字段继承。


I am using Spring and Hibernate for my application.

I am only allowing logical delete in my application where I need to set the field isActive=false. Instead of repeating the same field in all the entities, I created a Base Class with the property and getter-setter for 'isActive'.

So, during delete, I invoke the update() method and set the isActive to false.

I am not able to get this working. If any one has any idea, please let me know.

Base Entity

public abstract class BaseEntity<TId extends Serializable> implements IEntity<TId> {

    @Basic
    @Column(name = "IsActive")
    protected boolean isActive;

    public Boolean getIsActive() {
        return isActive;
    }

    public void setIsActive(Boolean isActive) {
        isActive= isActive;
    }
}

Child Entity

@Entity(name="Role")
@Table(schema = "dbo")
public class MyEntity extends BaseEntity {
    //remaining entities
}

Hibernate Util Class

public void remove(TEntity entity) {

    //Note: Enterprise data should be never removed.
    entity.setIsActive(false);
    sessionFactory.getCurrentSession().update(entity);
}

解决方案

Try to replace the code in setIsActive method with:

public void setIsActive(Boolean isActive) {
    this.isActive = isActive;
}

in your code the use of variable name without this could be ambiguos...

I think you should also add @MappedSuperclass annotation to your abstract class to achieve field inheritance.

这篇关于逻辑删除在休眠的一个常见的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆