我正在为我的一个应用程序使用hibernate过滤器。 意图是在crud操作中使用hibernate过滤器。 Hibernate过滤器可以正常使用select查询但是 即使在会话中启用过滤器后,它也不会在update delete get操作中添加where子句。
以下是我的代码 Employee.java,我在tenantFilter上添加了一个过滤器。
@Entity
@Table(name = "EMPLOYEE")
@FilterDef(name = "tenantFilter", parameters = @ParamDef(name = "tenantId", type = "string"))
@Filters(@Filter(name = "tenantFilter", condition = "tenant_id=:tenantId"))
public class Employee {
@Id
@GeneratedValue
private Long id;
@Column(name = "firstname")
private String firstname;
@Column(name = "lastname")
private String lastname;
@Column(name = "birth_date")
private Date birthDate;
@Column(name = "cell_phone")
private String cellphone;
@Column(name = "tenant_id")
private String tenant_id;
public Employee() {
}
public Employee(String firstname, String lastname, Date birthdate,
String phone, String tenant) {
this.firstname = firstname;
this.lastname = lastname;
this.birthDate = birthdate;
this.cellphone = phone;
this.tenant_id = tenant;
}
public Long getId() {
return id;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public Date getBirthDate() {
return birthDate;
}
public String getCellphone() {
return cellphone;
}
public void setId(Long id) {
this.id = id;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public void setCellphone(String cellphone) {
this.cellphone = cellphone;
}
public String getTenant() {
return tenant_id;
}
public void setTenant(String tenant) {
this.tenant_id = tenant;
}
}
对于列表api(如下所示),过滤器工作正常
private static List list() {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.enableFilter("tenantFilter").setParameter("tenantId", "visa");
List employees = session.createQuery("from Employee").list();
session.close();
return employees;
}
带有where子句
的列表api的Hibernate查询Hibernate:
select
employee0_.id as id0_,
employee0_.birth_date as birth2_0_,
employee0_.cell_phone as cell3_0_,
employee0_.firstname as firstname0_,
employee0_.lastname as lastname0_,
employee0_.tenant_id as tenant6_0_
from
EMPLOYEE employee0_
where
employee0_.tenant_id=?
对于read api(如下所示),过滤器未应用
private static Employee read(Long id) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.enableFilter("tenantFilter").setParameter("tenantId", "visa");
Employee employee = (Employee) session.get(Employee.class, id);
session.close();
return employee;
}
读取api的Hiberbernate查询
Hibernate:
select
employee0_.id as id0_0_,
employee0_.birth_date as birth2_0_0_,
employee0_.cell_phone as cell3_0_0_,
employee0_.firstname as firstname0_0_,
employee0_.lastname as lastname0_0_,
employee0_.tenant_id as tenant6_0_0_
from
EMPLOYEE employee0_
where
employee0_.id=?
更新api(如下所示)过滤器未应用
private static Employee update(Employee employee) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.enableFilter("tenantFilter").setParameter("tenantId", "visa");
session.beginTransaction();
session.merge(employee);
session.getTransaction().commit();
session.close();
return employee;
}
读取api的Hibernate查询
Hibernate:
select
employee0_.id as id0_0_,
employee0_.birth_date as birth2_0_0_,
employee0_.cell_phone as cell3_0_0_,
employee0_.firstname as firstname0_0_,
employee0_.lastname as lastname0_0_,
employee0_.tenant_id as tenant6_0_0_
from
EMPLOYEE employee0_
where
employee0_.id=?
Hibernate:
update
EMPLOYEE
set
birth_date=?,
cell_phone=?,
firstname=?,
lastname=?,
tenant_id=?
where
id=?
删除api也是如此,过滤器也没有应用。
我搜索了hibernate过滤器文档以获取crud操作但无法获取任何描述。 因此,为crud操作应用过滤器是正确的,或者我认为它是错误的。 请协助。 我希望使用过滤器来实现多租户环境,其中单个数据库服务于多个租赁。