Browse Source

Extend UserRoleRepository methods: add findByCompositeId

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.4-alpha
Pekka Helenius 4 years ago
parent
commit
4c50e3085d
3 changed files with 63 additions and 1 deletions
  1. +1
    -1
      bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepository.java
  2. +14
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryCustom.java
  3. +48
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryImpl.java

+ 1
- 1
bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepository.java View File

@ -19,5 +19,5 @@ import org.springframework.data.rest.core.annotation.RepositoryRestResource;
itemResourceRel = "userroles",
exported = true
)
public interface UserRoleRepository extends CrudRepository<UserRole, Long> {
public interface UserRoleRepository extends CrudRepository<UserRole, Long>, UserRoleRepositoryCustom {
}

+ 14
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryCustom.java View File

@ -0,0 +1,14 @@
//Pekka Helenius <fincer89@hotmail.com>, 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);
}

+ 48
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepositoryImpl.java View File

@ -0,0 +1,48 @@
//Pekka Helenius <fincer89@hotmail.com>, 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<UserRole> 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;
}
}
}

Loading…
Cancel
Save