Merge branch 'master' into master
This commit is contained in:
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.associations.biredirectional;
|
||||
import jakarta.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Course {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(name = "course_student",
|
||||
joinColumns = @JoinColumn(name = "course_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "student_id"))
|
||||
private List<Student> students;
|
||||
|
||||
public List<Student> getStudents() {
|
||||
return students;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setStudents(List<Student> students) {
|
||||
this.students = students;
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.associations.biredirectional;
|
||||
|
||||
import java.util.List;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
|
||||
@Entity
|
||||
public class Department {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@OneToMany(mappedBy = "department")
|
||||
private List<Employee> employees;
|
||||
|
||||
public List<Employee> getEmployees() {
|
||||
return employees;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.associations.biredirectional;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
|
||||
@Entity
|
||||
public class Employee {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "department_id")
|
||||
private Department department;
|
||||
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Department getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
public void setDepartment(Department department) {
|
||||
this.department = department;
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.associations.biredirectional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Student {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToMany(mappedBy = "students")
|
||||
private List<Course> courses;
|
||||
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Course> getCourses() {
|
||||
return courses;
|
||||
}
|
||||
|
||||
public void setCourses(List<Course> courses) {
|
||||
this.courses = courses;
|
||||
}
|
||||
}
|
||||
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.associations.unidirectional;
|
||||
import jakarta.persistence.*;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
public class Author {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "authors")
|
||||
private Set<Book> books;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<Book> getBooks() {
|
||||
return books;
|
||||
}
|
||||
|
||||
public void setBooks(Set<Book> books) {
|
||||
this.books = books;
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.associations.unidirectional;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "book_author",
|
||||
joinColumns = @JoinColumn(name = "book_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "author_id"))
|
||||
private Set<Author> authors;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Set<Author> getAuthors() {
|
||||
return authors;
|
||||
}
|
||||
|
||||
public void setAuthors(Set<Author> authors) {
|
||||
this.authors = authors;
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.associations.unidirectional;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Department {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "department_id")
|
||||
private List<Employee> employees;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<Employee> getEmployees() {
|
||||
return employees;
|
||||
}
|
||||
|
||||
public void setEmployees(List<Employee> employees) {
|
||||
this.employees = employees;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.associations.unidirectional;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Employee {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@OneToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "parking_spot_id")
|
||||
private ParkingSpot parkingSpot;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "department_id")
|
||||
private Department department;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public ParkingSpot getParkingSpot() {
|
||||
return parkingSpot;
|
||||
}
|
||||
|
||||
public void setParkingSpot(ParkingSpot parkingSpot) {
|
||||
this.parkingSpot = parkingSpot;
|
||||
}
|
||||
|
||||
public Department getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
public void setDepartment(Department department) {
|
||||
this.department = department;
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.associations.unidirectional;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class ParkingSpot {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.countrows;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AccountStatsApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AccountStatsApplication.class, args);
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.countrows.entity;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import java.security.PrivateKey;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
|
||||
@Entity
|
||||
@Table(name = "ACCOUNTS")
|
||||
public class Account {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accounts_seq")
|
||||
@SequenceGenerator(name = "accounts_seq", sequenceName = "accounts_seq", allocationSize = 1)
|
||||
@Column(name = "user_id")
|
||||
private int userId;
|
||||
private String username;
|
||||
private String password;
|
||||
private String email;
|
||||
private Timestamp createdOn;
|
||||
private Timestamp lastLogin;
|
||||
|
||||
@OneToOne
|
||||
@JoinColumn(name = "permissions_id")
|
||||
private Permission permission;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Timestamp getCreatedOn() {
|
||||
return createdOn;
|
||||
}
|
||||
|
||||
public void setCreatedOn(Timestamp createdOn) {
|
||||
this.createdOn = createdOn;
|
||||
}
|
||||
|
||||
public void setLastLogin(Timestamp lastLogin) {
|
||||
this.lastLogin = lastLogin;
|
||||
}
|
||||
|
||||
public Permission getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
public void setPermission(Permission permission) {
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Account{" + "userId=" + userId + ", username='" + username + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + ", createdOn=" + createdOn + ", lastLogin=" + lastLogin + ", permission=" + permission + '}';
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.countrows.entity;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "PERMISSIONS")
|
||||
public class Permission {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "permissions_id_sq")
|
||||
@SequenceGenerator(name = "permissions_id_sq", sequenceName = "permissions_id_sq", allocationSize = 1)
|
||||
private int id;
|
||||
|
||||
private String type;
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Permission{" + "id=" + id + ", type='" + type + '\'' + '}';
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.countrows.repository;
|
||||
|
||||
import com.baeldung.countrows.entity.Account;
|
||||
import com.baeldung.countrows.entity.Permission;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface AccountRepository extends JpaRepository<Account, Integer> {
|
||||
|
||||
long countByUsername(String username);
|
||||
|
||||
long countByPermission(Permission permission);
|
||||
|
||||
long countByPermissionAndCreatedOnGreaterThan(Permission permission, Timestamp ts);
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.countrows.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.countrows.entity.Permission;
|
||||
|
||||
@Repository
|
||||
public interface PermissionRepository extends JpaRepository<Permission, Integer> {
|
||||
Permission findByType(String type);
|
||||
}
|
||||
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
package com.baeldung.countrows.service;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.criteria.*;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.countrows.entity.Account;
|
||||
import com.baeldung.countrows.entity.Permission;
|
||||
import com.baeldung.countrows.repository.AccountRepository;
|
||||
import com.baeldung.countrows.repository.PermissionRepository;
|
||||
|
||||
@Service
|
||||
public class AccountStatsLogic {
|
||||
@Autowired
|
||||
private AccountRepository accountRepository;
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Autowired
|
||||
private PermissionRepository permissionRepository;
|
||||
|
||||
public long getAccountCount() {
|
||||
return accountRepository.count();
|
||||
}
|
||||
|
||||
public long getAccountCountByUsername(String username) {
|
||||
return accountRepository.countByUsername(username);
|
||||
}
|
||||
|
||||
public long getAccountCountByPermission(Permission permission) {
|
||||
return accountRepository.countByPermission(permission);
|
||||
}
|
||||
|
||||
public long getAccountCountByPermissionAndCreatedOn(Permission permission, Date date) throws ParseException {
|
||||
return accountRepository.countByPermissionAndCreatedOnGreaterThan(permission, new Timestamp(date.getTime()));
|
||||
}
|
||||
|
||||
public long getAccountsUsingCQ() throws ParseException {
|
||||
// creating criteria builder and query
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
|
||||
Root<Account> accountRoot = criteriaQuery.from(Account.class);
|
||||
|
||||
// select query
|
||||
criteriaQuery.select(builder.count(accountRoot));
|
||||
|
||||
// execute and get the result
|
||||
return entityManager.createQuery(criteriaQuery)
|
||||
.getSingleResult();
|
||||
}
|
||||
|
||||
public long getAccountsByPermissionUsingCQ(Permission permission) throws ParseException {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
|
||||
Root<Account> accountRoot = criteriaQuery.from(Account.class);
|
||||
|
||||
List<Predicate> predicateList = new ArrayList<>(); // list of predicates that will go in where clause
|
||||
predicateList.add(builder.equal(accountRoot.get("permission"), permission));
|
||||
|
||||
criteriaQuery.select(builder.count(accountRoot))
|
||||
.where(builder.and(predicateList.toArray(new Predicate[0])));
|
||||
|
||||
return entityManager.createQuery(criteriaQuery)
|
||||
.getSingleResult();
|
||||
}
|
||||
|
||||
public long getAccountsByPermissionAndCreateOnUsingCQ(Permission permission, Date date) throws ParseException {
|
||||
// creating criteria builder and query
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder(); // create builder
|
||||
CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);// query instance
|
||||
Root<Account> accountRoot = criteriaQuery.from(Account.class); // root instance
|
||||
|
||||
// list of predicates that will go in where clause
|
||||
List<Predicate> predicateList = new ArrayList<>();
|
||||
predicateList.add(builder.equal(accountRoot.get("permission"), permission));
|
||||
predicateList.add(builder.greaterThan(accountRoot.get("createdOn"), new Timestamp(date.getTime())));
|
||||
|
||||
// select query
|
||||
criteriaQuery.select(builder.count(accountRoot))
|
||||
.where(builder.and(predicateList.toArray(new Predicate[0])));
|
||||
|
||||
// execute and get the result
|
||||
return entityManager.createQuery(criteriaQuery)
|
||||
.getSingleResult();
|
||||
}
|
||||
|
||||
public long getAccountsUsingJPQL() throws ParseException {
|
||||
Query query = entityManager.createQuery("SELECT COUNT(*) FROM Account a");
|
||||
return (long) query.getSingleResult();
|
||||
}
|
||||
|
||||
public long getAccountsByPermissionUsingJPQL(Permission permission) throws ParseException {
|
||||
Query query = entityManager.createQuery("SELECT COUNT(*) FROM Account a WHERE a.permission = ?1");
|
||||
query.setParameter(1, permission);
|
||||
return (long) query.getSingleResult();
|
||||
}
|
||||
|
||||
public long getAccountsByPermissionAndCreatedOnUsingJPQL(Permission permission, Date date) throws ParseException {
|
||||
Query query = entityManager.createQuery("SELECT COUNT(*) FROM Account a WHERE a.permission = ?1 and a.createdOn > ?2");
|
||||
query.setParameter(1, permission);
|
||||
query.setParameter(2, new Timestamp(date.getTime()));
|
||||
return (long) query.getSingleResult();
|
||||
}
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
package com.baeldung.boot.countrows.accountstatslogic;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.countrows.AccountStatsApplication;
|
||||
import com.baeldung.countrows.entity.Account;
|
||||
import com.baeldung.countrows.entity.Permission;
|
||||
import com.baeldung.countrows.repository.AccountRepository;
|
||||
import com.baeldung.countrows.repository.PermissionRepository;
|
||||
import com.baeldung.countrows.service.AccountStatsLogic;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest(classes = AccountStatsApplication.class)
|
||||
class AccountStatsUnitTest {
|
||||
|
||||
@Autowired
|
||||
private PermissionRepository permissionRepository;
|
||||
|
||||
@Autowired
|
||||
private AccountRepository accountRepository;
|
||||
|
||||
@Autowired
|
||||
private AccountStatsLogic accountStatsLogic;
|
||||
|
||||
@AfterEach
|
||||
public void afterEach() {
|
||||
accountRepository.deleteAll();
|
||||
permissionRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccountInTable_whenPerformCount_returnsAppropriateCount() {
|
||||
savePermissions();
|
||||
saveAccount();
|
||||
assertThat(accountStatsLogic.getAccountCount()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccountInTable_whenPerformCountByUsernameOrPermission_returnsAppropriateCount() {
|
||||
savePermissions();
|
||||
Account account = saveAccount();
|
||||
assertThat(accountStatsLogic.getAccountCountByUsername(account.getUsername())).isEqualTo(1);
|
||||
assertThat(accountStatsLogic.getAccountCountByPermission(account.getPermission())).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccountInTable_whenPerformCountByPermissionAndCreatedOn_returnsAppropriateCount() throws ParseException {
|
||||
savePermissions();
|
||||
Account account = saveAccount();
|
||||
long count = accountStatsLogic.getAccountCountByPermissionAndCreatedOn(account.getPermission(), account.getCreatedOn());
|
||||
assertThat(count).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccountInTable_whenPerformCountUsingCQ_returnsAppropriateCount() throws ParseException {
|
||||
savePermissions();
|
||||
saveAccount();
|
||||
long count = accountStatsLogic.getAccountsUsingCQ();
|
||||
assertThat(count).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccountInTable_whenPerformCountByPermissionUsingCQ_returnsAppropriateCount() throws ParseException {
|
||||
savePermissions();
|
||||
Account account = saveAccount();
|
||||
long count = accountStatsLogic.getAccountsByPermissionUsingCQ(account.getPermission());
|
||||
assertThat(count).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccountInTable_whenPerformCountByPermissionAndCreatedOnUsingCQ_returnsAppropriateCount() throws ParseException {
|
||||
savePermissions();
|
||||
Account account = saveAccount();
|
||||
long count = accountStatsLogic.getAccountsByPermissionAndCreateOnUsingCQ(account.getPermission(), account.getCreatedOn());
|
||||
assertThat(count).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccountInTable_whenPerformCountUsingJPQL_returnsAppropriateCount() throws ParseException {
|
||||
savePermissions();
|
||||
saveAccount();
|
||||
long count = accountStatsLogic.getAccountsUsingJPQL();
|
||||
assertThat(count).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccountInTable_whenPerformCountByPermissionUsingJPQL_returnsAppropriateCount() throws ParseException {
|
||||
savePermissions();
|
||||
Account account = saveAccount();
|
||||
long count = accountStatsLogic.getAccountsByPermissionUsingJPQL(account.getPermission());
|
||||
assertThat(count).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccountInTable_whenPerformCountByPermissionAndCreatedOnUsingJPQL_returnsAppropriateCount() throws ParseException {
|
||||
savePermissions();
|
||||
Account account = saveAccount();
|
||||
long count = accountStatsLogic.getAccountsByPermissionAndCreatedOnUsingJPQL(account.getPermission(), account.getCreatedOn());
|
||||
assertThat(count).isEqualTo(1);
|
||||
}
|
||||
|
||||
private Account saveAccount() {
|
||||
return accountRepository.save(getAccount());
|
||||
}
|
||||
|
||||
private void savePermissions() {
|
||||
Permission editor = new Permission();
|
||||
editor.setType("editor");
|
||||
permissionRepository.save(editor);
|
||||
|
||||
Permission admin = new Permission();
|
||||
admin.setType("admin");
|
||||
permissionRepository.save(admin);
|
||||
}
|
||||
|
||||
private Account getAccount() {
|
||||
Permission permission = permissionRepository.findByType("admin");
|
||||
Account account = new Account();
|
||||
String seed = UUID.randomUUID()
|
||||
.toString();
|
||||
account.setUsername("username_" + seed);
|
||||
account.setEmail("username_" + seed + "@gmail.com");
|
||||
account.setPermission(permission);
|
||||
account.setPassword("password_q1234");
|
||||
account.setCreatedOn(Timestamp.from(Instant.now()));
|
||||
account.setLastLogin(Timestamp.from(Instant.now()));
|
||||
return account;
|
||||
}
|
||||
}
|
||||
@@ -169,14 +169,6 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>dynamodb-local</id>
|
||||
<name>DynamoDB Local Release Repository</name>
|
||||
<url>${dynamodblocal.repository.url}</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<!-- The main class to start by executing java -jar -->
|
||||
<start-class>com.baeldung.Application</start-class>
|
||||
@@ -186,9 +178,7 @@
|
||||
<aws-java-sdk-dynamodb.version>1.11.64</aws-java-sdk-dynamodb.version>
|
||||
<bootstrap.version>3.3.7-1</bootstrap.version>
|
||||
<sqlite4java.version>1.0.392</sqlite4java.version>
|
||||
<dynamodb.version>1.11.106</dynamodb.version>
|
||||
<dynamodblocal.version>1.11.86</dynamodblocal.version>
|
||||
<dynamodblocal.repository.url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</dynamodblocal.repository.url>
|
||||
<dynamodblocal.version>1.21.1</dynamodblocal.version>
|
||||
<maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
|
||||
<spring-boot.version>2.4.7</spring-boot.version>
|
||||
<log4j2.version>2.17.1</log4j2.version>
|
||||
|
||||
@@ -6,4 +6,5 @@ This module contains articles about Spring Data JPA.
|
||||
- [New CRUD Repository Interfaces in Spring Data 3](https://www.baeldung.com/spring-data-3-crud-repository-interfaces)
|
||||
- [How to Persist a List of String in JPA?](https://www.baeldung.com/java-jpa-persist-string-list)
|
||||
- [Hibernate Natural IDs in Spring Boot](https://www.baeldung.com/spring-boot-hibernate-natural-ids)
|
||||
- [Correct Use of flush() in JPA](https://www.baeldung.com/spring-jpa-flush)
|
||||
- More articles: [[<-- prev]](../spring-data-jpa-repo-2)
|
||||
|
||||
Reference in New Issue
Block a user