Java 休眠过滤器不';我不能如愿工作
我有两个类,其中一个类由用于实现软删除的实体继承。我有一个DAO方法,它使用实体和DeleteEnabledResource过滤器来启用过滤器。然而,Hibernate似乎不考虑应用过滤器,即使它被正确地启用(我可以观察到调试视图中启用的过滤器)。我使用Hibernate4.3.8.Final。添加DAO、类和Hibernate生成的查询Java 休眠过滤器不';我不能如愿工作,java,spring,hibernate,dao,Java,Spring,Hibernate,Dao,我有两个类,其中一个类由用于实现软删除的实体继承。我有一个DAO方法,它使用实体和DeleteEnabledResource过滤器来启用过滤器。然而,Hibernate似乎不考虑应用过滤器,即使它被正确地启用(我可以观察到调试视图中启用的过滤器)。我使用Hibernate4.3.8.Final。添加DAO、类和Hibernate生成的查询 Session session = null; try { session = HibernateUtil.getSessio
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();
}
}
注意:这对于我的其他实体来说非常有效
刀
如果我再添加一个条件,则会启用过滤器,并且在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();
}
}
班级
删除启用的类
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;
}
}
我猜这是因为您只选择了
@naturaid
s。对于这种“直接”阅读,应用额外的过滤器毫无意义,尽管我找不到任何关于何时应用或不应用过滤器的文档。在你的其他实体(你说它在工作的地方)中,你有更多的标准吗?我有一个类似的实体,它只有@naturaid
,并且这个特定的实体工作得很好。同样的,我找不到任何关于这件事的文件!