หน้าเว็บ

วันศุกร์ที่ 14 กันยายน พ.ศ. 2555

Join entity many to many jpa : java

Services Interface
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);
    
    ...
    ...
}
Service Implementation
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);
    }

    ...
    ...
}
Repositories
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);

    ...
    ...
}
Project Entity
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;
    }

    ...
    ...
}

User Entity
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;
    }

    ...
    ...
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น