Last active
September 13, 2017 15:37
-
-
Save BohdanLevchenko/1111088a491d145a153b4604c51a4de3 to your computer and use it in GitHub Desktop.
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
package com.stackoverflow.so46193254; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.data.jpa.domain.AbstractPersistable; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Transactional; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import javax.persistence.*; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
/** | |
* Configuration: | |
* <p> | |
* spring.jpa.open-in-view=false | |
* spring.jpa.show-sql=true | |
* spring.jpa.hibernate.ddl-auto=create | |
* <p> | |
* spring.datasource.url=jdbc:h2:mem:test | |
* spring.datasource.driver-class-name=org.h2.Driver | |
* spring.datasource.username=sa | |
* spring.datasource.password= | |
* <p> | |
* server.error.whitelabel.enabled=false | |
*/ | |
@SpringBootApplication | |
public class So46193254Application { | |
public static void main(String[] args) { | |
SpringApplication.run(So46193254Application.class, args); | |
} | |
@Entity(name = "role") | |
static class Role extends AbstractPersistable<String> { | |
private String name; | |
private Role() { | |
} | |
public Role(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return this.name; | |
} | |
} | |
@Entity(name = "user") | |
static class User extends AbstractPersistable<String> { | |
private String username; | |
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) | |
@JoinColumn(name = "user_id", foreignKey = @ForeignKey(name = "fk_user_roles")) | |
private Set<Role> roles = new HashSet<>(); | |
public Set<Role> getRoles() { | |
return this.roles; | |
} | |
} | |
@Service | |
static class UserService { | |
private final UserDao userDao; | |
UserService(UserDao userDao) { | |
this.userDao = userDao; | |
} | |
@Transactional(readOnly = true) | |
public Set<String> findUserRoles() { | |
final List<User> users = this.userDao.findAll(); | |
return users.stream() | |
.flatMap(user -> user.roles.stream()) | |
.map(Role::getName) | |
.collect(Collectors.toSet()); | |
} | |
} | |
// insert some test data | |
@Bean | |
CommandLineRunner startup(UserDao userDao) { | |
return args -> { | |
final User user = new User(); | |
user.username = "admin"; | |
user.roles.add(new Role("ROLE_ADMIN")); | |
userDao.save(user); | |
}; | |
} | |
// curl -XGET 'http://localhost:8080' | |
@RestController | |
static class UserRolesApi { | |
private final UserService userService; | |
UserRolesApi(UserService userService) { | |
this.userService = userService; | |
} | |
@GetMapping | |
public String index() { | |
return this.userService.findUserRoles().toString(); | |
} | |
} | |
} | |
interface UserDao extends JpaRepository<So46193254Application.User, String> {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment