add relation role

This commit is contained in:
DOHA 2015-10-13 11:42:57 +02:00
parent 96aca9fb7c
commit 714ec1b7d0
6 changed files with 267 additions and 23 deletions

View File

@ -1,8 +1,13 @@
package org.baeldung;
import java.util.Arrays;
import java.util.HashSet;
import javax.annotation.PostConstruct;
import org.baeldung.persistence.dao.RoleRepository;
import org.baeldung.persistence.dao.UserRepository;
import org.baeldung.persistence.model.Role;
import org.baeldung.persistence.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -13,13 +18,23 @@ public class Setup {
@Autowired
private UserRepository userRepository;
@Autowired
private RoleRepository roleRepository;
@PostConstruct
private void setupData() {
final User user = new User();
user.setFirstName("john");
user.setLastName("doe");
user.setEmail("john@test.com");
userRepository.save(user);
Role roleUser = new Role("ROLE_USER");
roleUser = roleRepository.save(roleUser);
Role roleAdmin = new Role("ROLE_ADMIN");
roleAdmin = roleRepository.save(roleAdmin);
final User userJohn = new User("john", "john@test.com");
userJohn.setRoles(new HashSet<Role>(Arrays.asList(roleUser, roleAdmin)));
userRepository.save(userJohn);
final User userTom = new User("tom", "tom@test.com");
userTom.setRoles(new HashSet<Role>(Arrays.asList(roleUser)));
userRepository.save(userTom);
}
}

View File

@ -0,0 +1,8 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.model.Role;
import org.springframework.data.jpa.repository.JpaRepository;
public interface RoleRepository extends JpaRepository<Role, Long> {
}

View File

@ -0,0 +1,42 @@
package org.baeldung.persistence.katharsis;
import io.katharsis.queryParams.RequestParams;
import io.katharsis.repository.ResourceRepository;
import org.baeldung.persistence.dao.RoleRepository;
import org.baeldung.persistence.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RoleResourceRepository implements ResourceRepository<Role, Long> {
@Autowired
private RoleRepository roleRepository;
@Override
public Role findOne(Long id, RequestParams params) {
return roleRepository.findOne(id);
}
@Override
public Iterable<Role> findAll(RequestParams params) {
return roleRepository.findAll();
}
@Override
public Iterable<Role> findAll(Iterable<Long> ids, RequestParams params) {
return roleRepository.findAll(ids);
}
@Override
public <S extends Role> S save(S entity) {
return roleRepository.save(entity);
}
@Override
public void delete(Long id) {
roleRepository.delete(id);
}
}

View File

@ -0,0 +1,65 @@
package org.baeldung.persistence.katharsis;
import io.katharsis.queryParams.RequestParams;
import io.katharsis.repository.RelationshipRepository;
import java.util.HashSet;
import java.util.Set;
import org.baeldung.persistence.dao.RoleRepository;
import org.baeldung.persistence.dao.UserRepository;
import org.baeldung.persistence.model.Role;
import org.baeldung.persistence.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserToRoleRelationshipRepository implements RelationshipRepository<User, Long, Role, Long> {
@Autowired
private UserRepository userRepository;
@Autowired
private RoleRepository roleRepository;
@Override
public void setRelation(User User, Long roleId, String fieldName) {
// not for many-to-many
}
@Override
public void setRelations(User user, Iterable<Long> roleIds, String fieldName) {
final Set<Role> roles = new HashSet<Role>();
roles.addAll(roleRepository.findAll(roleIds));
user.setRoles(roles);
userRepository.save(user);
}
@Override
public void addRelations(User user, Iterable<Long> roleIds, String fieldName) {
final Set<Role> roles = user.getRoles();
roles.addAll(roleRepository.findAll(roleIds));
user.setRoles(roles);
userRepository.save(user);
}
@Override
public void removeRelations(User user, Iterable<Long> roleIds, String fieldName) {
final Set<Role> roles = user.getRoles();
roles.removeAll(roleRepository.findAll(roleIds));
user.setRoles(roles);
userRepository.save(user);
}
@Override
public Role findOneTarget(Long sourceId, String fieldName, RequestParams requestParams) {
// not for many-to-many
return null;
}
@Override
public Iterable<Role> findManyTargets(Long sourceId, String fieldName, RequestParams requestParams) {
final User user = userRepository.findOne(sourceId);
return user.getRoles();
}
}

View File

@ -0,0 +1,96 @@
package org.baeldung.persistence.model;
import io.katharsis.resource.annotations.JsonApiId;
import io.katharsis.resource.annotations.JsonApiResource;
import io.katharsis.resource.annotations.JsonApiToMany;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
@JsonApiResource(type = "roles")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonApiId
private Long id;
@Column(nullable = false, unique = true)
private String name;
@ManyToMany(mappedBy = "roles")
@JsonApiToMany
private Set<User> users;
//
public Role() {
super();
}
public Role(String name) {
super();
this.name = name;
}
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;
}
//
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (id == null ? 0 : id.hashCode());
result = prime * result + (name == null ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Role other = (Role) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Role [id=").append(id).append(", name=").append(name).append("]");
return builder.toString();
}
}

View File

@ -1,12 +1,20 @@
package org.baeldung.persistence.model;
import io.katharsis.resource.annotations.JsonApiId;
import io.katharsis.resource.annotations.JsonApiIncludeByDefault;
import io.katharsis.resource.annotations.JsonApiResource;
import io.katharsis.resource.annotations.JsonApiToMany;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
@JsonApiResource(type = "users")
@ -17,16 +25,26 @@ public class User {
@JsonApiId
private Long id;
private String firstName;
private String lastName;
private String username;
private String email;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
@JsonApiToMany
@JsonApiIncludeByDefault
private Set<Role> roles;
public User() {
super();
}
public User(String username, String email) {
super();
this.username = username;
this.email = email;
}
public Long getId() {
return id;
}
@ -35,28 +53,28 @@ public class User {
this.id = id;
}
public String getFirstName() {
return firstName;
public String getUsername() {
return username;
}
public void setFirstName(final String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(final String lastName) {
this.lastName = lastName;
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(final String username) {
email = username;
public void setEmail(final String email) {
this.email = email;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
@Override
@ -84,7 +102,7 @@ public class User {
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("User [firstName=").append(firstName).append("]").append("[lastName=").append(lastName).append("]").append("[username").append(email).append("]");
builder.append("User [id=").append(id).append(", username=").append(username).append(", email=").append(email).append(", roles=").append(roles).append("]");
return builder.toString();
}