From 4c50e3085d9eef3fa4cdf889cb39a81a140d3075 Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Mon, 12 Oct 2020 21:32:35 +0300 Subject: [PATCH] Extend UserRoleRepository methods: add findByCompositeId Signed-off-by: Pekka Helenius --- .../model/auth/UserRoleRepository.java | 2 +- .../model/auth/UserRoleRepositoryCustom.java | 14 ++++++ .../model/auth/UserRoleRepositoryImpl.java | 48 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryCustom.java create mode 100644 bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryImpl.java diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepository.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepository.java index 3c3b0fb..2a73f28 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepository.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepository.java @@ -19,5 +19,5 @@ import org.springframework.data.rest.core.annotation.RepositoryRestResource; itemResourceRel = "userroles", exported = true ) -public interface UserRoleRepository extends CrudRepository { +public interface UserRoleRepository extends CrudRepository, UserRoleRepositoryCustom { } \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryCustom.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryCustom.java new file mode 100644 index 0000000..1881194 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryCustom.java @@ -0,0 +1,14 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.model.auth; + +/** +* +* This interface defines additional methods for UserRoleRepository. +* +* @author Pekka Helenius +*/ + +public interface UserRoleRepositoryCustom { + UserRole findByCompositeId(Long userId, Long roleId); +} \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryImpl.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryImpl.java new file mode 100644 index 0000000..0cfcfda --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryImpl.java @@ -0,0 +1,48 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.model.auth; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; + +import org.springframework.transaction.annotation.Transactional; + +/** + * + * This class implements methods defined in UserRoleRepositoryCustom interface. + * + * @author Pekka Helenius + */ + +@Transactional +public class UserRoleRepositoryImpl implements UserRoleRepositoryCustom { + + @PersistenceContext + EntityManager entityManager; + + /* + * Find unique USER_ROLE entry by user & role IDs + */ + + @Override + public UserRole findByCompositeId(Long userId, Long roleId) { + + TypedQuery query = entityManager.createQuery( + "SELECT i FROM UserRole i" + + " WHERE user_id = :user_id" + + " AND role_id = :role_id", + UserRole.class + ); + + query.setParameter("user_id", userId); + query.setParameter("role_id", roleId); + + try { + return query.getResultList().get(0); + } catch (IndexOutOfBoundsException ignored) { + return null; + } + } + +} \ No newline at end of file