恐怕这是不可能的,因为如果可能的话,会有一些附带影响。
例如,如果您尝试使用您的想法进行分页,则计数结果将与返回的实体结果数不同:
// all rows will be count, with problem or not
select count (*) from Person
// will ignore the problematic rows and bring less results than the count
select p From Person p
最好的选择似乎更灵活,并为您的代码带来复杂性,在您的实体映射中更加灵活。
例如,如果您的 Enumerator 有问题,因为数据库中的 enumerator 值无效,最好将列映射为 String 而不是 Enumerator 并处理代码中的问题。
所以,如果你有这样的事情:
@Column(name = "status", nullable = false, length = 30)
@Enumerated(EnumType.STRING)
private Status status;
改为:
@Column(name = "status", nullable = false, length = 30)
private String status;
并忽略代码中的结果。有了这种灵活性,您还可以忽略查询中的有问题的结果。喜欢:
SELECT p FROM Person p WHERE status NOT IN (:validStatusAsString)