JPA 2 introduces a criteria API that you can use to build queries programmatically. By writing a criteria, you define the where clause of a query for a domain class. Taking another step back, these criteria can be regarded as a predicate over the entity that is described by the JPA criteria API constraints.
@Test
void specificationTest() {
Post post = new Post();
final String NAME = "specificationPostName";
final String DESCRIPTION = "specificationPostDescription";
post.setName(NAME);
post.setDescription(DESCRIPTION);
postRepository.save(post);
List<Post> targets = postRepository.findAll(Post.getPostsByCustomSpecification(NAME, DESCRIPTION));
Assertions.assertThat(targets.size()).isEqualTo(1);
Assertions.assertThat(targets.get(0).getName()).isEqualTo(NAME);
Assertions.assertThat(targets.get(0).getDescription()).isEqualTo(DESCRIPTION);
}
select
post0_.id as id1_0_,
post0_.description as descript2_0_,
post0_.name as name3_0_
from
post post0_
where
post0_.name=?
and post0_.description=?
# binding parameter [1] as [VARCHAR] - [specificationPostName]
# binding parameter [2] as [VARCHAR] - [specificationPostDescription]
@Test
void specificationTest() {
Post post = new Post();
final String NAME = "specificationPostName";
final String DESCRIPTION = "specificationPostDescription";
post.setName(NAME);
post.setDescription(DESCRIPTION);
postRepository.save(post);
List<Post> targets = postRepository.findAll(Post.getPostsByCustomSpecification(NAME, null));
Assertions.assertThat(targets.size()).isEqualTo(1);
Assertions.assertThat(targets.get(0).getName()).isEqualTo(NAME);
Assertions.assertThat(targets.get(0).getDescription()).isEqualTo(DESCRIPTION);
}
select
post0_.id as id1_0_,
post0_.description as descript2_0_,
post0_.name as name3_0_
from
post post0_
where
post0_.name=?
@CreatedDate
private Date created;
@LastModifiedDate
private Date updated;
@CreatedBy
@ManyToOne
private Account createdBy;
@LastModifiedBy
@ManyToOne
private Account updatedBy;