Skip to content
Advertisement

native query join in spring data

I have the class:

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

I want to know if it’s possible to use a native query join in spring data JPA and if the result of query was correctly mapped into entities like the above example.

Can someone show me a complete example to user this. I must to use a native query in my case and I am not sure that work.

Advertisement

Answer

you can do that by using named native query and result set mapping and here is a complete exmaple

Mission Entity

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

}

User Entity

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

}

Repository Function

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

Controller Function

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

and this is The Result

[
  {
    "id": 1,
    "name": "Mission 1",
    "users": [
      {
        "id": 1,
        "name": "Test"
      }
    ]
  },
  {
    "id": 2,
    "name": "Mission 2",
    "users": [
      {
        "id": 1,
        "name": "Test"
      }
    ]
  }
]
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement