我一直在寻找与此类似的问题,但没有成功。
我的目标:我需要获取创建某个实体的用户名,该用户名存储在通过 Spring 的 @AuditTable 引用的所述实体的审计表中。
有点(希望)解释:在这种特殊情况下,我将添加到使用 org.springframework.data.jpa.repository.JpaSpecificationExecutor 类以及 Predicate 类(javax.persistence.criteria.Predicate)构建的先前存在的查询中. 连接往往使用类的名称(例如 Join),然后 Spring 从中获取表名。我的问题是,有没有办法让 Spring 查看审计表名,而不是实体的表名?
一个例子是:
@Entity @Table(name = "persons")
@Audited @AuditTable(name = "audit_persons")
public class Person {
private String id;
private String name;
private Job job;
/*getters and setters*/
}
@Entity @Table(name = "jobs")
@Audited @AuditTable(name = "audit_jobs")
public class Job {
private String id;
private String name;
/*getters and setters*/
}
public final class PersonSpecification {
private PersonSpecification(){}
public static Specification<Person> findBy(final String attributeName, final Object filterValue) {
return new Specification<Person>() {
@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.equal(root.get(attributeName), filterValue);
}
};
}
public static Specification<Person> findByJob(final Job job) {
return new Specification<Person>() {
@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Join<Person, Job> joinJob = root.join("job", JoinType.LEFT);
return cb.equal(joinJob.<Job>get("job"), job);
}
};
}
/*
public static Specification<Person> findByUsername(final String username) {
return new Specification<Person>() {
@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Join<Person, audit person?> join? = root.join(?, JoinType.LEFT);
return I don't know how to access;
}
};
}
*/
}
import static ().PersonSpecification.findBy;
import static ().PersonSpecification.findByJob;
@Service(value="personService")
public class PersonService {
@Autowired
private PersonDao personDao;
public List<Person> find(String id, String name, Job job /*, String username*/){
Specifications<Person> specifications = null;
if (!StringUtils.isBlank(id) && StringUtils.isNumeric(id)) {
specifications = loadFilter(specifications, findBy("id", id));
}else{
if (!StringUtils.isBlank(name)) {
specifications = loadFilter(specifications, findBy("name", name));
}
if (job != null) {
specifications = loadFilter(specifications, findByJob("job", job));
}
/*
if (!StringUtils.isBlank(username)) {
specifications = I don't know what to do here;
}
*/
}
return personDao.findAll(specifications);
}
}
一个可能的解决方案:我正在考虑添加一个引用被审计实体的新实体,如下所示:
@Entity @Table(name = "audit_persons")
public class AuditedPerson {
private String id;
private Integer rev;
private Integer revtype;
private String username;
/*getters and setters*/
}
然后我的方法是
public static Specification<Person> findByUsername(final String username) {
return new Specification<Person>() {
@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Join<Person, AuditedPerson> joinAudit = root.join(id, JoinType.LEFT);
return cb.and(cb.equal(joinAudit.get("username"), username),
cb.equal(joinAudit.get("revtype"), 0)); //this is so I get the creation instance's username
}
};
}
我会努力完成这项工作。有没有办法做到这一点?我应该只添加一个执行此操作的本机查询吗?谢谢!