-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from tmslpm/jpa-spec
added example jpa specification
- Loading branch information
Showing
4 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/dal/src/main/java/com/github/tmslpm/hsla/dal/entity/embeddable/Address.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.github.tmslpm.hsla.dal.entity.embeddable; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Embeddable; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* <pre><code>// note: | ||
* public class MyEntity extends BaseEntity { | ||
* {@literal @Embedded @Getter} | ||
* private Address location; | ||
* } | ||
* </code></pre> | ||
*/ | ||
@Embeddable | ||
@SuppressWarnings("unused") | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Data | ||
@Builder | ||
public final class Address { | ||
@Column(name = "address_street", nullable = false) | ||
private String street; | ||
@Column(name = "address_number", nullable = false) | ||
private String number; | ||
@Column(name = "address_locality", nullable = false) | ||
private String locality; | ||
@Column(name = "address_postal_code", nullable = false) | ||
private String code; | ||
@Column(name = "address_country", nullable = false) | ||
private String country; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/dal/src/main/java/com/github/tmslpm/hsla/dal/specification/UserSpecs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.github.tmslpm.hsla.dal.specification; | ||
|
||
import com.github.tmslpm.hsla.dal.entity.UserEntity; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
import java.time.LocalDate; | ||
|
||
public interface UserSpecs { | ||
|
||
static Specification<UserEntity> createdAfter(LocalDate date) { | ||
return (r, q, b) -> b.greaterThan(r.get("createdAt"), date); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/dal/src/test/java/com/github/tmslpm/hsla/dal/specification/UserSpecsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.github.tmslpm.hsla.dal.specification; | ||
|
||
import com.github.tmslpm.hsla.dal.entity.UserEntity; | ||
import com.github.tmslpm.hsla.dal.repository.IUserRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@SpringBootTest() | ||
@TestPropertySource(locations = "classpath:application.test.properties") | ||
class UserSpecsTest { | ||
@Autowired | ||
IUserRepository userRepository; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
userRepository.deleteAll(); | ||
|
||
var arr = List.of( | ||
"UserSpecsTest-A", LocalDate.of(1000, 6, 15), | ||
"UserSpecsTest-B", LocalDate.of(2000, 6, 15), | ||
"UserSpecsTest-C", LocalDate.of(2050, 6, 15), | ||
"UserSpecsTest-D", LocalDate.of(9999, 6, 15) | ||
); | ||
|
||
for (int i = 0; i < arr.size(); i += 2) { | ||
UserEntity user = new UserEntity(arr.get(i).toString()); | ||
user.setActive(true); | ||
user.setCreatedAt((LocalDate) arr.get(i + 1)); | ||
userRepository.save(user); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCreatedAfter() { | ||
List<UserEntity> result = userRepository.findAll( | ||
UserSpecs.createdAfter(LocalDate.of(500, 1, 1))); | ||
|
||
assertEquals(4, result.size()); | ||
} | ||
|
||
} |