2016-12-04 84 views
1

我拖類:本地查詢春季數據加入

@Entity 
    public User{ 
    @Id 
    Long id; 
    String name; 

    @ManyToMany 
    List<Mission> missions; 

    } 

    @Entity 
    public Mission{ 
    @Id 
    Long id; 
    String name; 

    @ManyToMany 
    List<User> users; 
    } 

     public interface MissionRepository extends CrudRepository<Mission, Long> { 
      @Query(nativeQuery = true, "select * from mission join user on id = user_id where name = ?1") 
      public List<Mission> findByname(String name); 
     } 

我想知道它是否可以使用本機查詢參加春季數據JPA,如果查詢的結果是正確映射到像實體上面的例子。

有人可以告訴我一個完整的例子,用戶這個。 我必須在我的情況下使用本機查詢,我不知道這工作。

回答

2

你可以做,通過使用命名本機查詢和結果集映射 ,這裏是一個完整的〔實施例

使命實體

package com.ntg.crm.internal.entites; 

import java.math.BigInteger; 
import java.util.List; 

import javax.persistence.Entity; 
import javax.persistence.EntityResult; 
import javax.persistence.FetchType; 
import javax.persistence.FieldResult; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.JoinTable; 
import javax.persistence.ManyToMany; 
import javax.persistence.NamedNativeQuery; 
import javax.persistence.SqlResultSetMapping; 

@SqlResultSetMapping(name = "Mission.findAllMissionsMapping", entities = @EntityResult(entityClass = Mission.class, fields = { 
     @FieldResult(name = "name", column = "mname"), @FieldResult(name = "id", column = "mid") 
})) 
@NamedNativeQuery(name = "Mission.findAllMissions", query = "select m.id as mid,m.name as mname , info.id uid ,info.name uname from Mission m join user_info_missions um on m.id " 
     + "= um.missions_id join user_Info info on info.id = um.user_id where info.name =:userName", resultSetMapping = "Mission.findAllMissionsMapping") 

@Entity 
public class Mission { 

    public Mission() { 

    } 

    public Mission(BigInteger id, String name) { 
     super(); 
     this.id = id; 
     this.name = name; 
    } 

    public Mission(BigInteger id, String name, List<User> users) { 
     super(); 
     this.id = id; 
     this.name = name; 
     this.users = users; 
    } 

    @Id 
    BigInteger id; 
    String name; 

    @ManyToMany(fetch = FetchType.EAGER) 
    @JoinTable(name = "user_info_missions", joinColumns = @JoinColumn(name = "missions_id"), inverseJoinColumns = @JoinColumn(name = "user_id")) 
    List<User> users; 

    public BigInteger getId() { 
     return id; 
    } 

    public void setId(BigInteger id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public List<User> getUsers() { 
     return users; 
    } 

    public void setUsers(List<User> users) { 
     this.users = users; 
    } 

} 

用戶實體

package com.ntg.crm.internal.entites; 

import java.util.List; 

import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.JoinTable; 
import javax.persistence.ManyToMany; 
import javax.persistence.Table; 

import com.fasterxml.jackson.annotation.JsonIgnore; 

@Entity 
@Table(name = "userInfo") 
public class User { 

    @Id 
    long id; 
    String name; 

    @ManyToMany(targetEntity = Mission.class) 
    @JoinTable(name = "user_info_missions", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "missions_id")) 
    @JsonIgnore 
    List<Mission> missions; 

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

    public List<Mission> getMissions() { 
     return missions; 
    } 

    public void setMissions(List<Mission> missions) { 
     this.missions = missions; 
    } 

} 

存儲功能

public List<Mission> findAllMissions(@Param("userName") String userName); 

控制器功能

@Autowired 
    TestRepository testRepo; 

    @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json", consumes = "application/json") 
    @ResponseBody 
    List<Mission> getall() { 
     List<Mission> missions = testRepo.findAllMissions("Test"); 
     return missions; 
    } 

,這是結果

[ 
    { 
    "id": 1, 
    "name": "Mission 1", 
    "users": [ 
     { 
     "id": 1, 
     "name": "Test" 
     } 
    ] 
    }, 
    { 
    "id": 2, 
    "name": "Mission 2", 
    "users": [ 
     { 
     "id": 1, 
     "name": "Test" 
     } 
    ] 
    } 
] 
+0

感謝您的回答,但我想知道,如果查詢結果正確地映射到entitie使命。換句話說,我可以在請求後獲得用戶列表。 – midy62

+0

是的,你可以得到一個用戶列表看到答案更新 –