In Spring Data Jpa we have a flexibility to map the required fields query result to a VO object with out exposing the entity object outside.
DB Used :: mysql
1st way ::
@Query("SELECT new CountryVO(c.id,c.name) FROM Country c")
public List<CountryVO> getCountries();
//Country Entity
public class Country{
private long id;
private String name;
@DBRef
private State state;
}
// State Entity
public class State{
private long id;
private String name;
}
//Country VO
public class CountryVO{
private long id;
private String name;
private String stateName;
}
2nd way ::
@Query("SELECT DISTINCT c.name FROM Country c")
public List<String> getNames();
Now my point is I am using Spring Data mongoDB with mongoDB database and here the querying way is different like below
@Query(value = "{}", fields = "{'id':1,'name':1,'state.name':1}")
List<CountryVO> getAllCountries();
In the above query we can mention what ever fields we want in the fields attribute and the remaining fields will come as null values, but I want to map the output result to a VO like Spring Data Jpa.
Please let me know any possibilities
Thanks in Advance
Aucun commentaire:
Enregistrer un commentaire