简体   繁体   English

Spring JPA-数据获取所选列

[英]Spring JPA - Data fetching selected columns

Am using Spring JPA repository 我正在使用Spring JPA存储库

public String getNextULAID();
 // Selecting particular columns from a table
  @Query(value="select dbts,firstName from user_details  
  where ULA_ID= :ulaID and DEL_FLG = 'N'" ,nativeQuery=true)

  public UserDetails fetchUser(@Param("ulaID")String ulaId);
}

Throws error 抛出错误

{
    "timestamp": 1499336001602,
    "status": 500,
    "error": "Internal Server Error",

    "exception": "org.springframework.dao.InvalidDataAccessResourceUsageException",

    "message": "could not extract ResultSet; SQL [n/a]; 
    nested exception is org.hibernate.exception.SQLGrammarException: 
    could not extract ResultSet",

    "path": "/viewProfile/1"
}

But if I select all data from the database,then its working perfectly 但是,如果我从数据库中选择所有数据,那么它可以完美地工作

public String getNextULAID();
  // selecting all records
  @Query(value="select * from user_details  
  where ULA_ID= :ulaID and DEL_FLG = 'N'" ,nativeQuery=true)
  public UserDetails fetchUser(@Param("ulaID")String ulaId);

}

Here is my entity... 这是我的实体...

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

// End of user code

 @Entity
 @Table(name="user_details")
 public class UserDetails {
/**
 * Description of the property dbts.
 */
private Integer dbts;

/**
 * Description of the property ulaId.
 */
@Id
private String ulaId = "";

/**
 * Description of the property firstName.
 */
private String firstName = "";

/**
 * Description of the property lastName.
 */
private String lastName = "";

/**
 * Description of the property emailId.
 */
private String emailId = "";

/**
 * Description of the property mobileNo.
 */
private String mobileNo = "";

/**
 * Description of the property gender.
 */
private String gender = "";

/**
 * Description of the property dateOfBirth.
 */
private String dateOfBirth = "";

/**
 * Description of the property address1.
 */
private String address1 = "";

/**
 * Description of the property address2.
 */
private String address2 = "";

/**
 * Description of the property city.
 */
private String city = "";

/**
 * Description of the property state.
 */
private String state = "";

/**
 * Description of the property country.
 */
private String country = "";

/**
 * Description of the property pincode.
 */
private String pincode = "";

/**
 * Description of the property profileGroup.
 */
private String profileGroup = "";

/**
 * Description of the property delFlg.
 */
private String delFlg = "";

/**
 * Description of the property remarks.
 */
private String remarks = "";

/**
 * Description of the property rCreTime.
 */
private String rCreTime = "";

/**
 * Description of the property rModTime.
 */
private String rModTime = "";

// Start of user code (user defined attributes for UserDetails)

// End of user code

/**
 * The constructor.
 */
public UserDetails() {
    // Start of user code constructor for UserDetails)
    super();
    // End of user code
}

// Start of user code (user defined methods for UserDetails)

// End of user code
/**
 * Returns dbts.
 * @return dbts 
 */
public Integer getDbts() {
    return this.dbts;
}

/**
 * Sets a value to attribute dbts. 
 * @param newDbts 
 */
public void setDbts(Integer newDbts) {
    this.dbts = newDbts;
}

/**
 * Returns ulaId.
 * @return ulaId 
 */
public String getUlaId() {
    return this.ulaId;
}

/**
 * Sets a value to attribute ulaId. 
 * @param newUlaId 
 */
public void setUlaId(String newUlaId) {
    this.ulaId = newUlaId;
}

/**
 * Returns firstName.
 * @return firstName 
 */
public String getFirstName() {
    return this.firstName;
}

/**
 * Sets a value to attribute firstName. 
 * @param newFirstName 
 */
public void setFirstName(String newFirstName) {
    this.firstName = newFirstName;
}

/**
 * Returns lastName.
 * @return lastName 
 */
public String getLastName() {
    return this.lastName;
}

/**
 * Sets a value to attribute lastName. 
 * @param newLastName 
 */
public void setLastName(String newLastName) {
    this.lastName = newLastName;
}

/**
 * Returns emailId.
 * @return emailId 
 */
public String getEmailId() {
    return this.emailId;
}

/**
 * Sets a value to attribute emailId. 
 * @param newEmailId 
 */
public void setEmailId(String newEmailId) {
    this.emailId = newEmailId;
}

/**
 * Returns mobileNo.
 * @return mobileNo 
 */
public String getMobileNo() {
    return this.mobileNo;
}

/**
 * Sets a value to attribute mobileNo. 
 * @param newMobileNo 
 */
public void setMobileNo(String newMobileNo) {
    this.mobileNo = newMobileNo;
}

/**
 * Returns gender.
 * @return gender 
 */
public String getGender() {
    return this.gender;
}

/**
 * Sets a value to attribute gender. 
 * @param newGender 
 */
public void setGender(String newGender) {
    this.gender = newGender;
}

/**
 * Returns dateOfBirth.
 * @return dateOfBirth 
 */
public String getDateOfBirth() {
    return this.dateOfBirth;
}

/**
 * Sets a value to attribute dateOfBirth. 
 * @param newDateOfBirth 
 */
public void setDateOfBirth(String newDateOfBirth) {
    this.dateOfBirth = newDateOfBirth;
}

/**
 * Returns address1.
 * @return address1 
 */
public String getAddress1() {
    return this.address1;
}

/**
 * Sets a value to attribute address1. 
 * @param newAddress1 
 */
public void setAddress1(String newAddress1) {
    this.address1 = newAddress1;
}

/**
 * Returns address2.
 * @return address2 
 */
public String getAddress2() {
    return this.address2;
}

/**
 * Sets a value to attribute address2. 
 * @param newAddress2 
 */
public void setAddress2(String newAddress2) {
    this.address2 = newAddress2;
}

/**
 * Returns city.
 * @return city 
 */
public String getCity() {
    return this.city;
}

/**
 * Sets a value to attribute city. 
 * @param newCity 
 */
public void setCity(String newCity) {
    this.city = newCity;
}

/**
 * Returns state.
 * @return state 
 */
public String getState() {
    return this.state;
}

/**
 * Sets a value to attribute state. 
 * @param newState 
 */
public void setState(String newState) {
    this.state = newState;
}

/**
 * Returns country.
 * @return country 
 */
public String getCountry() {
    return this.country;
}

/**
 * Sets a value to attribute country. 
 * @param newCountry 
 */
public void setCountry(String newCountry) {
    this.country = newCountry;
}

/**
 * Returns pincode.
 * @return pincode 
 */
public String getPincode() {
    return this.pincode;
}

/**
 * Sets a value to attribute pincode. 
 * @param newPincode 
 */
public void setPincode(String newPincode) {
    this.pincode = newPincode;
}

/**
 * Returns profileGroup.
 * @return profileGroup 
 */
public String getProfileGroup() {
    return this.profileGroup;
}

/**
 * Sets a value to attribute profileGroup. 
 * @param newProfileGroup 
 */
public void setProfileGroup(String newProfileGroup) {
    this.profileGroup = newProfileGroup;
}

/**
 * Returns delFlg.
 * @return delFlg 
 */
public String getDelFlg() {
    return this.delFlg;
}

/**
 * Sets a value to attribute delFlg. 
 * @param newDelFlg 
 */
public void setDelFlg(String newDelFlg) {
    this.delFlg = newDelFlg;
}

/**
 * Returns remarks.
 * @return remarks 
 */
public String getRemarks() {
    return this.remarks;
}

/**
 * Sets a value to attribute remarks. 
 * @param newRemarks 
 */
public void setRemarks(String newRemarks) {
    this.remarks = newRemarks;
}

/**
 * Returns rCreTime.
 * @return rCreTime 
 */
public String getRCreTime() {
    return this.rCreTime;
}

/**
 * Sets a value to attribute rCreTime. 
 * @param newRCreTime 
 */
public void setRCreTime(String newRCreTime) {
    this.rCreTime = newRCreTime;
}

/**
 * Returns rModTime.
 * @return rModTime 
 */
public String getRModTime() {
    return this.rModTime;
}

/**
 * Sets a value to attribute rModTime. 
 * @param newRModTime 
 */
public void setRModTime(String newRModTime) {
    this.rModTime = newRModTime;
}

} }

What changes i have to do, to get specific columns from multiple data. 我要做什么,才能从多个数据中获取特定的列。

Thanks in advance. 提前致谢。

You should return List<Object[]> instead of UserDetails in case of native query. 如果是本机查询,则应返回List<Object[]>而不是UserDetails

Another way is to create projection. 另一种方法是创建投影。 You can use projections from Spring Data JPA http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections . 您可以使用来自Spring Data JPA的预测http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM