Skip to content

Commit

Permalink
Merge pull request #3 from tmslpm/jpa-spec
Browse files Browse the repository at this point in the history
added example jpa specification
  • Loading branch information
tmslpm authored Dec 10, 2024
2 parents c66fa03 + 48de372 commit 04d749a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@
import com.github.tmslpm.hsla.bll.mapper.UserMapper;
import com.github.tmslpm.hsla.dal.entity.UserEntity;
import com.github.tmslpm.hsla.dal.repository.IUserRepository;
import com.github.tmslpm.hsla.dal.specification.UserSpecs;
import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Validated
Expand Down Expand Up @@ -69,7 +71,15 @@ public List<UserDTO> findAll() {
.findAll()
.stream()
.map(userMapper::toDTO)
.collect(Collectors.toList());
.toList();
}

public List<UserDTO> findUserCreatedAfter(LocalDate date) {
return userRepository
.findAll(Specification.where(UserSpecs.createdAfter(date)))
.stream()
.map(userMapper::toDTO)
.toList();
}

}
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;
}
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);
}

}
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());
}

}

0 comments on commit 04d749a

Please sign in to comment.