我有两个类,其中一个类由Entity继承,用于实现软删除。我有一个DAO方法,它使用Entity和DeleteEnabledResource过滤器来启用过滤器。然而,即使它正确启用,hibernate也似乎不考虑应用过滤器(我可以观察调试视图中启用的过滤器)。我使用Hibernate 4.3.8.Final。添加DAO,Classes和Hibernate生成的查询。
注意:这适用于我的其他实体。
DAO
如果我再添加一个条件,则启用过滤器,并在Hibernate生成的查询中显示deleted = 0。
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
session.enableFilter("DeleteEnabledResource");
Criteria criteria = session.createCriteria(WfWorkflows.class);
criteria.add(Restrictions.naturalId().set("tenantId", tenantId).set("workflowId", workflowId).set("version", version));
return (WfWorkflows) criteria.uniqueResult();
} catch (Exception exc) {
LOGGER.debug("Tenant Id = {} | Workflow Id ={} | Version = {} | Failed to get", tenantId, workflowId, version, exc);
throw new DataAccessException(exc);
} finally {
if (session != null && session.isOpen()) {
session.close();
}
}
类
DeleteEnabled Class
import org.hibernate.annotations.*;
import javax.persistence.Column;
import javax.persistence.Inheritance;
import javax.persistence.MappedSuperclass;
import java.lang.annotation.Inherited;
@MappedSuperclass
@FilterDef(name = "DeleteEnabledResource", defaultCondition = "deleted = 0")
@Filter(name = "DeleteEnabledResource", condition = "deleted = 0")
public class DeleteEnabled {
public DeleteEnabled() {
}
private boolean deleted = false;
/**
* Getter for property 'deleted'.
*
* @return Value for property 'deleted'.
*/
@Column(name = "deleted", columnDefinition = "BIT default 0")
public boolean isDeleted() {
return deleted;
}
/**
* Setter for property 'deleted'.
*
* @param deleted Value to set for property 'deleted'.
*/
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
}
工作流程类
import org.hibernate.annotations.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Table;
import static javax.persistence.GenerationType.IDENTITY;
/**
* WfWorkflows represents a Workflow
*/
@Entity
@Table(name = "wf_workflows", uniqueConstraints = {@UniqueConstraint(columnNames = {"tenant_id", "workflow_id", "version"})})
public class WfWorkflows extends DeleteEnabled implements java.io.Serializable {
private static final long serialVersionUID = -5901132323282824514L;
private Integer id;
private int version;
private WfWorkflows inheritedId;
private String workflowId;
private long tenantId;
private String flowName;
private String description;
private String workflowData;
private Date dateCreated = new Date();
private String createdBy;
private Date dateModified;
private String modifiedBy;
private Set<WfWorkflows> inheritedIds = new HashSet<WfWorkflows>(0);
private String status = null;
/**
* Constructs a new WfWorkflows.
*/
public WfWorkflows() {
}
public WfWorkflows(String workflowId, int version, long tenantId, String flowName, String description) {
this.workflowId = workflowId;
this.version = version;
this.tenantId = tenantId;
this.flowName = flowName;
this.description = description;
}
public WfWorkflows(String workflowId, int version, long tenantId, String flowName, String description, WfWorkflows inheritedId) {
this.workflowId = workflowId;
this.version = version;
this.tenantId = tenantId;
this.flowName = flowName;
this.description = description;
this.inheritedId = inheritedId;
}
public WfWorkflows(String workflowId, WfWorkflows inheritedId, int version, long tenantId, String flowName, String description, String workflowData) {
this.workflowId = workflowId;
this.inheritedId = inheritedId;
this.version = version;
this.tenantId = tenantId;
this.flowName = flowName;
this.description = description;
this.workflowData = workflowData;
}
public WfWorkflows(int version, WfWorkflows inheritedId, String workflowId, long tenantId, String flowName, String description, Date dateCreated, String createdBy, Date dateModified, String modifiedBy) {
this.version = version;
this.inheritedId = inheritedId;
this.workflowId = workflowId;
this.tenantId = tenantId;
this.flowName = flowName;
this.description = description;
this.dateCreated = dateCreated;
this.createdBy = createdBy;
this.dateModified = dateModified;
this.modifiedBy = modifiedBy;
}
public WfWorkflows(int version, WfWorkflows inheritedId, String workflowId, long tenantId, String flowName, String description, String workflowData, Date dateCreated, String createdBy, Date dateModified, String modifiedBy) {
this.version = version;
this.inheritedId = inheritedId;
this.workflowId = workflowId;
this.tenantId = tenantId;
this.flowName = flowName;
this.description = description;
this.workflowData = workflowData;
this.dateCreated = dateCreated;
this.createdBy = createdBy;
this.dateModified = dateModified;
this.modifiedBy = modifiedBy;
}
/**
* Getter for property 'id'.
*
* @return Value for property 'id'.
*/
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
/**
* Setter for property 'id'.
*
* @param id Value to set for property 'id'.
*/
public void setId(Integer id) {
this.id = id;
}
/**
* Getter for property 'version'.
*
* @return Value for property 'version'.
*/
@NaturalId(mutable = false)
@Column(name = "version", nullable = false, columnDefinition = "INT DEFAULT 1")
public int getVersion() {
return this.version;
}
/**
* Setter for property 'version'.
*
* @param version Value to set for property 'version'.
*/
public void setVersion(int version) {
this.version = version;
}
/**
* Getter for property 'inheritedId'.
*
* @return Value for property 'inheritedId'.
*/
@ManyToOne(fetch = FetchType.EAGER, optional = true)
@JoinColumn(name = "inherited_id", nullable = true, columnDefinition = "INT")
public WfWorkflows getInheritedId() {
return this.inheritedId;
}
/**
* Setter for property 'inheritedId'.
*
* @param inheritedId Value to set for property 'inheritedId'.
*/
public void setInheritedId(WfWorkflows inheritedId) {
this.inheritedId = inheritedId;
}
/**
* Getter for property 'workflowId'.
*
* @return Value for property 'workflowId'.
*/
@NaturalId(mutable = false)
@Column(name = "workflow_id", nullable = false, length = 50)
public String getWorkflowId() {
return this.workflowId;
}
/**
* Setter for property 'workflowId'.
*
* @param workflowId Value to set for property 'workflowId'.
*/
public void setWorkflowId(String workflowId) {
this.workflowId = workflowId;
}
/**
* Getter for property 'tenantId'.
*
* @return Value for property 'tenantId'.
*/
@NaturalId(mutable = false)
@Column(name = "tenant_id", nullable = false)
public long getTenantId() {
return this.tenantId;
}
/**
* Setter for property 'tenantId'.
*
* @param tenantId Value to set for property 'tenantId'.
*/
public void setTenantId(long tenantId) {
this.tenantId = tenantId;
}
/**
* Getter for property 'flowName'.
*
* @return Value for property 'flowName'.
*/
@Column(name = "flow_name", nullable = false, length = 200)
public String getFlowName() {
return this.flowName;
}
/**
* Setter for property 'flowName'.
*
* @param flowName Value to set for property 'flowName'.
*/
public void setFlowName(String flowName) {
this.flowName = flowName;
}
/**
* Getter for property 'description'.
*
* @return Value for property 'description'.
*/
@Column(name = "description", length = 500)
public String getDescription() {
return description;
}
/**
* Setter for property 'description'.
*
* @param description Value to set for property 'description'.
*/
public void setDescription(String description) {
this.description = description;
}
/**
* Getter for property 'workflowData'.
*
* @return Value for property 'workflowData'.
*/
@Column(name = "workflow_data", length = 65535, nullable = true, columnDefinition = "TEXT")
public String getWorkflowData() {
return workflowData;
}
/**
* Setter for property 'workflowData'.
*
* @param workflowData Value to set for property 'workflowData'.
*/
public void setWorkflowData(String workflowData) {
this.workflowData = workflowData;
}
/**
* Getter for property 'dateCreated'.
*
* @return Value for property 'dateCreated'.
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date_created", nullable = false, length = 19, columnDefinition = "TIMESTAMP default CURRENT_TIMESTAMP")
public Date getDateCreated() {
return this.dateCreated;
}
/**
* Setter for property 'dateCreated'.
*
* @param dateCreated Value to set for property 'dateCreated'.
*/
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
/**
* Getter for property 'createdBy'.
*
* @return Value for property 'createdBy'.
*/
@Column(name = "created_by", length = 50)
public String getCreatedBy() {
return this.createdBy;
}
/**
* Setter for property 'createdBy'.
*
* @param createdBy Value to set for property 'createdBy'.
*/
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
/**
* Getter for property 'dateModified'.
*
* @return Value for property 'dateModified'.
*/
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date_modified", length = 19)
public Date getDateModified() {
return this.dateModified;
}
/**
* Setter for property 'dateModified'.
*
* @param dateModified Value to set for property 'dateModified'.
*/
public void setDateModified(Date dateModified) {
this.dateModified = dateModified;
}
/**
* Getter for property 'modifiedBy'.
*
* @return Value for property 'modifiedBy'.
*/
@Column(name = "modified_by", length = 50)
public String getModifiedBy() {
return this.modifiedBy;
}
/**
* Setter for property 'modifiedBy'.
*
* @param modifiedBy Value to set for property 'modifiedBy'.
*/
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
/**
* Getter for property 'inheritedIds'.
*
* @return Value for property 'inheritedIds'.
*/
@OneToMany(fetch = FetchType.EAGER, mappedBy = "inheritedId", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<WfWorkflows> getInheritedIds() {
return this.inheritedIds;
}
/**
* Setter for property 'inheritedIds'.
*
* @param inheritedIds Value to set for property 'inheritedIds'.
*/
public void setInheritedIds(Set<WfWorkflows> inheritedIds) {
this.inheritedIds = inheritedIds;
}
/**
* Getter for property 'status'.
*
* @return Value for property 'status'.
*/
@Column(name = "status")
public String getStatus() {
return status;
}
/**
* Setter for property 'status'.
*
* @param status Value to set for property 'status'.
*/
public void setStatus(String status) {
this.status = status;
}
}
休眠输出(生成的查询,缺少删除= 0条件)
Hibernate: insert into wf_workflows (deleted, created_by, date_created, date_modified, description, flow_name, inherited_id, modified_by, status, tenant_id, version, workflow_data, workflow_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select wfworkflow_.id as id1_21_ from wf_workflows wfworkflow_ where wfworkflow_.tenant_id=? and wfworkflow_.version=? and wfworkflow_.workflow_id=?
Hibernate: select wfworkflow0_.id as id1_21_0_, wfworkflow0_.deleted as deleted2_21_0_, wfworkflow0_.created_by as created_3_21_0_, wfworkflow0_.date_created as date_cre4_21_0_, wfworkflow0_.date_modified as date_mod5_21_0_, wfworkflow0_.description as descript6_21_0_, wfworkflow0_.flow_name as flow_nam7_21_0_, wfworkflow0_.inherited_id as inherit14_21_0_, wfworkflow0_.modified_by as modified8_21_0_, wfworkflow0_.status as status9_21_0_, wfworkflow0_.tenant_id as tenant_10_21_0_, wfworkflow0_.version as version11_21_0_, wfworkflow0_.workflow_data as workflo12_21_0_, wfworkflow0_.workflow_id as workflo13_21_0_, wfworkflow1_.id as id1_21_1_, wfworkflow1_.deleted as deleted2_21_1_, wfworkflow1_.created_by as created_3_21_1_, wfworkflow1_.date_created as date_cre4_21_1_, wfworkflow1_.date_modified as date_mod5_21_1_, wfworkflow1_.description as descript6_21_1_, wfworkflow1_.flow_name as flow_nam7_21_1_, wfworkflow1_.inherited_id as inherit14_21_1_, wfworkflow1_.modified_by as modified8_21_1_, wfworkflow1_.status as status9_21_1_, wfworkflow1_.tenant_id as tenant_10_21_1_, wfworkflow1_.version as version11_21_1_, wfworkflow1_.workflow_data as workflo12_21_1_, wfworkflow1_.workflow_id as workflo13_21_1_ from wf_workflows wfworkflow0_ left outer join wf_workflows wfworkflow1_ on wfworkflow0_.inherited_id=wfworkflow1_.id where wfworkflow0_.id=?
Hibernate: select inheritedi0_.inherited_id as inherit14_21_0_, inheritedi0_.id as id1_21_0_, inheritedi0_.id as id1_21_1_, inheritedi0_.deleted as deleted2_21_1_, inheritedi0_.created_by as created_3_21_1_, inheritedi0_.date_created as date_cre4_21_1_, inheritedi0_.date_modified as date_mod5_21_1_, inheritedi0_.description as descript6_21_1_, inheritedi0_.flow_name as flow_nam7_21_1_, inheritedi0_.inherited_id as inherit14_21_1_, inheritedi0_.modified_by as modified8_21_1_, inheritedi0_.status as status9_21_1_, inheritedi0_.tenant_id as tenant_10_21_1_, inheritedi0_.version as version11_21_1_, inheritedi0_.workflow_data as workflo12_21_1_, inheritedi0_.workflow_id as workflo13_21_1_ from wf_workflows inheritedi0_ where inheritedi0_.inherited_id=?
Hibernate: select wfworkflow0_.id as id1_21_0_, wfworkflow0_.deleted as deleted2_21_0_, wfworkflow0_.created_by as created_3_21_0_, wfworkflow0_.date_created as date_cre4_21_0_, wfworkflow0_.date_modified as date_mod5_21_0_, wfworkflow0_.description as descript6_21_0_, wfworkflow0_.flow_name as flow_nam7_21_0_, wfworkflow0_.inherited_id as inherit14_21_0_, wfworkflow0_.modified_by as modified8_21_0_, wfworkflow0_.status as status9_21_0_, wfworkflow0_.tenant_id as tenant_10_21_0_, wfworkflow0_.version as version11_21_0_, wfworkflow0_.workflow_data as workflo12_21_0_, wfworkflow0_.workflow_id as workflo13_21_0_, wfworkflow1_.id as id1_21_1_, wfworkflow1_.deleted as deleted2_21_1_, wfworkflow1_.created_by as created_3_21_1_, wfworkflow1_.date_created as date_cre4_21_1_, wfworkflow1_.date_modified as date_mod5_21_1_, wfworkflow1_.description as descript6_21_1_, wfworkflow1_.flow_name as flow_nam7_21_1_, wfworkflow1_.inherited_id as inherit14_21_1_, wfworkflow1_.modified_by as modified8_21_1_, wfworkflow1_.status as status9_21_1_, wfworkflow1_.tenant_id as tenant_10_21_1_, wfworkflow1_.version as version11_21_1_, wfworkflow1_.workflow_data as workflo12_21_1_, wfworkflow1_.workflow_id as workflo13_21_1_ from wf_workflows wfworkflow0_ left outer join wf_workflows wfworkflow1_ on wfworkflow0_.inherited_id=wfworkflow1_.id where wfworkflow0_.id=?
Hibernate: select inheritedi0_.inherited_id as inherit14_21_0_, inheritedi0_.id as id1_21_0_, inheritedi0_.id as id1_21_1_, inheritedi0_.deleted as deleted2_21_1_, inheritedi0_.created_by as created_3_21_1_, inheritedi0_.date_created as date_cre4_21_1_, inheritedi0_.date_modified as date_mod5_21_1_, inheritedi0_.description as descript6_21_1_, inheritedi0_.flow_name as flow_nam7_21_1_, inheritedi0_.inherited_id as inherit14_21_1_, inheritedi0_.modified_by as modified8_21_1_, inheritedi0_.status as status9_21_1_, inheritedi0_.tenant_id as tenant_10_21_1_, inheritedi0_.version as version11_21_1_, inheritedi0_.workflow_data as workflo12_21_1_, inheritedi0_.workflow_id as workflo13_21_1_ from wf_workflows inheritedi0_ where inheritedi0_.inherited_id=?
Hibernate: delete from wf_workflows where id=?