Created
December 9, 2015 02:24
-
-
Save SpiceMan/aaa561b30d729971b955 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
@MappedSuperclass | |
@Data | |
public abstract class AbstractBaseEntity implements Serializable { | |
private static final long serialVersionUID = -2700410403541761609L; | |
@Id | |
@GeneratedValue | |
private Long id; | |
@Column(name="UUID", unique=true, length=36) | |
private String uuid; | |
@PrePersist | |
private void generateUuid() { | |
if( uuid == null ) { | |
uuid = UUID.randomUUID().toString(); | |
} | |
} | |
} |
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
@Data | |
@Entity | |
@NoArgsConstructor | |
@Table(name = "role") | |
@Transactional | |
public class Role extends AbstractBaseEntity { | |
private static final long serialVersionUID = -4898422772958486777L; | |
@NotNull | |
@Column(unique=true) | |
private String name; | |
@NotNull | |
@NotNull | |
@Column(unique=true) | |
private String jpName; | |
@NotNull | |
@ManyToMany(mappedBy="roles") | |
private List<User> users; | |
public Role(String name, String jpName ) { | |
this.name = name; | |
this.jpName = jpName; | |
} | |
} |
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
public interface RoleRepository extends JpaRepository<Role, Long> { | |
Role findByName( String name ); | |
} |
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
@Data | |
@NoArgsConstructor | |
@Entity | |
@Table(name="user") | |
@Transactional | |
public class User extends AbstractBaseEntity { | |
private static final long serialVersionUID = -308652147526586990L; | |
@NotNull | |
private String surname; | |
@NotNull | |
private String name; | |
@NotNull | |
@Column(unique=true) | |
private String email; | |
@NotNull | |
@Column(unique=true) | |
private String login; | |
@NotNull | |
private String password; | |
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY) | |
@JoinTable(name="user_role") | |
private List<Role> roles; | |
/** | |
* 姓名とメールアドレス指定コンストラクタ | |
* | |
* @param surname | |
* @param name | |
* @param email | |
* @param login | |
* @param password | |
*/ | |
public User(String surname, String name, String email, String login, String password) { | |
this.surname = surname; | |
this.name = name; | |
this.email = email; | |
this.login = login; | |
this.password = password; | |
} | |
} |
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
public interface UserRepository extends JpaRepository<User, Long>{ | |
User findByLoginAndPassword(String login, String password); | |
User findByEmail(String email); | |
} |
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
public interface UserService { | |
User create(User user); | |
User create(String surname, String name, String email, String login, String password ); | |
User findById(Long id); | |
} |
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
@Service | |
@Transactional | |
public class UserServiceDBImpl implements UserService { | |
private UserRepository userRepo; | |
@Autowired | |
public UserServiceDBImpl(UserRepository urepo, RoleRepository rrepo) { | |
this.userRepo = urepo; | |
} | |
@Override | |
@Transactional | |
public User create(User user) { | |
User managed_user = userRepo.save(user); | |
System.out.println("Role count: " + managed_user.getRoles().size() ); // NullPointerException | |
return managed_user; | |
} | |
@Override | |
@Transactional | |
public User create(String surname, String name, String email, String login, String password ) { | |
return this.create(new User(surname, name, email, login, password )); | |
} | |
@Override | |
@Transactional | |
public User findById(Long id) { | |
return userRepo.findOne(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment