1 2 3 4 5 6 7 8 9 10 11 12 | package com.blogspot.na5cent.services; import java.util.List; import com.blogspot.na5cent.model.Project; import com.blogspot.na5cent.model.User; public interface UserService{ public List<User> findUserByProject(Project project); ... ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package com.blogspot.na5cent.services.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; import com.blogspot.na5cent.repositories.UserRepositories; import com.blogspot.na5cent.services.UserService; import com.blogspot.na5cent.model.Project; import com.blogspot.na5cent.model.User; ... ... @Service @Transactional (propagation = Propagation.REQUIRED) public Class UserServiceImpl implements UserService{ @Autowired private UserRepositories userRepo; private List<User> findUserByProject(Project project){ return userRepo.findByProject(project); } ... ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.blogspot.na5cent.repositories; import java.util.List; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; import com.blogspot.na5cent.model.Project; import com.blogspot.na5cent.model.User; ... ... public interface UserRepositories extends JpaRepository<User, Integer>, JpaSpecificationExecutor { @Query ( "SELECT u FROM Project p JOIN p.users u WHERE p = ?1" ) public List<User> findByProject(Project project); ... ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | package com.blogspot.na5cent.model; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.Version; ... ... @Entity @Table (name = "PROJECT" ) public class Project implements Serializable { @Id @Column (name = "PROJECT_ID" ) @GeneratedValue (strategy = GenerationType.AUTO) private Integer id; @Version private Integer version; private String name; private String description; @ManyToMany (mappedBy = "projects" ) private List<User> users; ... ... public Project() { } public Project(Integer id) { this .id = id; } //getter and setter @Override public String toString() { return name; } @Override public int hashCode() { int hash = 7 ; hash = 79 * hash + ( this .id != null ? this .id.hashCode() : 0 ); return hash; } @Override public boolean equals(Object obj) { if (obj == null ) { return false ; } if (getClass() != obj.getClass()) { return false ; } final Project other = (Project) obj; if ( this .id != other.id && ( this .id == null || ! this .id.equals(other.id))) { return false ; } return true ; } ... ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package com.blogspot.na5cent.model; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.Version; ... ... @Entity @Table (name = "USER" ) public class User implements Serializable { @Id @Column (name = "USER_ID" ) @GeneratedValue (strategy = GenerationType.AUTO) private Integer id; @Version private Integer version; private String name; @ManyToMany @JoinTable (name = "PROJECT_USER" , joinColumns = { @JoinColumn (name = "USER" , referencedColumnName = "USER_ID" )}, inverseJoinColumns = { @JoinColumn (name = "PROJECT" , referencedColumnName = "PROJECT_ID" )}) private List<Project> projects; ... ... public User() { } public User(Integer id) { this .id = id; } //getter and setter @Override public int hashCode() { int hash = 7 ; hash = 59 * hash + ( this .id != null ? this .id.hashCode() : 0 ); return hash; } @Override public boolean equals(Object obj) { if (obj == null ) { return false ; } if (getClass() != obj.getClass()) { return false ; } final User other = (User) obj; if ( this .id != other.id && ( this .id == null || ! this .id.equals(other.id))) { return false ; } return true ; } ... ... } |
ไม่มีความคิดเห็น:
แสดงความคิดเห็น