Spring支持通过要查找的对象的示例创建查询。像:通过示例查询跳过原语?
//if not setting the age, it will always look for age=0
Person p = new Person();
p.setLastName("Smith");
List<Person> foundPersons = personRepository.findAll(Example.of(p));
@Entity
public class Person {
private String firstName;
private String lastName;
private LocalDate dob;
private int age;
}
问题:如果@Entity
具有原始字段,然后它们的默认值实际上将用于创建查询。上面的例子将导致:
SELECT * from persons where lastname := 'Smith' and age := 0
在我的例子我有一个数据库字段,其中age
必须充满,这样是不允许被null
。因此该实体具有原始int age
字段。
当然,我现在可以将该字段更改为Integer age
,但那时我会将该字段标记为可选的可空属性,这是不正确的。
那么,如何跳过Example
上未设置的基元?
我会使用整数方法。如果你对年龄有限制,我认为不会有任何问题。 –