spring数据中的本机查询连接

时间:2016-12-04 16:29:29

标签: java spring spring-data spring-data-jpa

我有课:

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

    @ManyToMany
    List<Mission> missions;
}

@Entity
public class 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);
}

我想知道是否可以在spring数据JPA中使用本机查询连接,并且查询结果是否正确映射到上述示例中的实体。

有人可以告诉我一个完整的例子来使用它。 我必须在我的情况下使用本机查询,我不确定是否有效。

2 个答案:

答案 0 :(得分:5)

您可以使用命名本机查询和结果集映射来实现 这是一个完整的例子

使命实体

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"
      }
    ]
  }
]

这是结果

public static void main(String [] args){

Scanner input = new Scanner(System.in);
String word = "give"; {

    while(word.length() == 4){
        word=word;}

    while(word.length() != 4){
        word=input.next();
        }


    int[][] wordArray =  new int[2][2];
    wordArray[0][0] = (int)word.charAt(1);
    wordArray[0][1] = (int)word.charAt(2);
    wordArray[1][0] = (int)word.charAt(3);
    wordArray[1][1] = (int)word.charAt(4);

    int[][] encriptionArray = new int [2][2];
    encriptionArray[0][0] =  1;
    encriptionArray[0][1] = 2;
    encriptionArray[1][0] = 3;
    encriptionArray[1][1] = (4);


    int[][] printArray = new int [2][2];
    printArray[0][0]= wordArray[0][0]*encriptionArray[0][0]+ wordArray[0][1]*encriptionArray[0][1];
    printArray[0][1]= wordArray[0][1]*encriptionArray[0][1]+ wordArray[0][1]*encriptionArray[1][1];
    printArray[1][0]= wordArray[1][0]*encriptionArray[0][0]+ wordArray[1][1]*encriptionArray[1][0];
    printArray[1][1]= wordArray[0][1]*encriptionArray[1][0]+ wordArray[1][1]*encriptionArray[0][1];

    System.out.print(printArray[0][0]);
    System.out.print(printArray[0][1]);
    System.out.print(printArray[1][0]);
    System.out.print(printArray[1][1]);

}


}

答案 1 :(得分:0)

@ middy62,您可以使用以下查询。我已使用您的示例编写查询。希望对别人有帮助。


@Query(nativeQuery = true, value = "select m from Mission join m.user u where name =:name") public List<Mission> findByname(@Param("name") String name);

请注意,当您在域实体模型上正确映射时,查询才起作用。