java - Spring JPA - 获取选定列的数据

标签 java spring hibernate jpa

正在使用 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);
}

抛出错误

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

但是如果我从数据库中选择所有数据,那么它就可以正常工作

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

}

这是我的实体...

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

}

我必须做哪些更改才能从多个数据中获取特定列。

提前致谢。

最佳答案

您应该返回 List<Object[]>而不是UserDetails如果是 native 查询。

另一种方法是创建投影。您可以使用 Spring Data JPA http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections 中的投影.

关于java - Spring JPA - 获取选定列的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44946653/

相关文章:

java - Languagetool 示例 : java. lang.NoSuchMethodError : org. apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar

java - 在 tomcat7 上部署为 war 的 Spring Boot 应用程序失败,错误为 "More than one fragment with the name [org_apache_tomcat_websocket] was found"

java - 如何在同一 hibernate session 中从数据库表获取更新的值

java - 处理不同 EAR 上的交易

java - 如何与java代码并行读取 Parquet 文件

java - 如何在java中将天数转换并附加到Period类的小时数中?

java - 如何设置/更改Android Studio 'Fullscreen Activity'背景?

java - Spring MVC 模式给出 SAXParseException

java - Spring Boot + Liquibase - 自定义 LockService 类

java - hibernate createAlias with 子句生成错误的查询