Spring 数据jpa异常
Spring data jpa exception
从数据库 table 检索记录时出现异常。我对另一个 table 尝试了同样的方法,它起作用了,但是对于这个 table,它不起作用。我正在使用 spring-data-jpa
@Entity
@Table(name=USR.TABLE_NAME)
public class USR implements Serializable {
private static final long serialVersionUID = 1L;
public final static String TABLE_NAME = "usr_profile";
@Id
@Column (name="USR_NO")
private Integer usrNo;
@Column (name="USR_Address", length=20, nullable=true, unique=false)
private Integer usrAddress;
@Column (name="USR_COUNTRY", nullable=true, unique=false)
private String usrCountry;
other fields constructor, no-arg constructor, getter and setter removed for brevity
@Repository
public interface USRRepository extends JpaRepository<USR, Integer> {
@Query("SELECT o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt FROM USR o WHERE o.usrNo=?1")
USR findUsrRecordByUsrNo(Integer usrNo);
}
我在这里创建了一个控制器class,它有一个获取映射
@GetMapping ("/CmpUsr")
public ResponseEntity<String> cmpLookup() {
USR us = usrRepository.findUsrRecordByUsrNo(12557);
return new ResponseEntity<>(us.toString(), HttpStatus.OK);
}
我遇到了这个异常
SEVERE:Servlet.service() for servlet [dispatcherServiet] in context with path[] throw exception (Request processing failed; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] 为值键入 [com.newprof.userApp.domain.USR] (12557, 115 Minesota Yellow Rd, US, PH, 000991);嵌套异常是
org.springframework.core.comvert.ConverterNotFoundException:找不到能够从类型 [java.lang.Integer 转换为类型 [com.newprof.userApp.domain.USR]] 的转换器,根本原因是
改变这个:
@Query("SELECT o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt FROM USR o WHERE o.usrNo=?1")
USR findUsrRecordByUsrNo(Integer usrNo);
}
有了这个:
@Query("SELECT new com.newprof.userApp.domain.USR(o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt) FROM USR o WHERE o.usrNo=?1")
USR findUsrRecordByUsrNo(Integer usrNo);
}
在异常情况下显示:无法从类型 [java.lang.Object[]] 转换为类型 [com.newprof.userApp.domain.USR]。
这基本上意味着您的查询语句 returns 一个 对象数组 : java.lang.Object[].
所以我的计划是,您直接在查询中调用 USR 构造函数。
编辑
找到答案。
所以您将此添加到您的查询中:
new com.newprof.userApp.domain.USR(o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt)
它现在对您说:无法解析构造函数 com.newprof.userApp.domain.USR()。这是正确的。因为没有这样的构造函数。您需要将其添加到您的实体中。
您需要添加如下内容:
public USR (Integer usrNo, Integer usrAddress, String usrCountry, String usrState, String usrSt) {
this.usrNo = usrNo;
this.usrAddress = usrAddress;
this.usrCountry = usrCountry;
this.usrState = usrState;
this.usrSt = isrSt;
}
这个全新的构造函数将在声明中调用 new com.newprof.userApp.domain.USR(o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt)
:D
试试这个代码。我认为问题是 return 对象 与 select 值不同。
@Repository
public interface USRRepository extends JpaRepository<USR, Integer> {
@Query("SELECT o FROM USR o WHERE o.usrNo=?1")
USR findUsrRecordByUsrNo(Integer usrNo);
}
从数据库 table 检索记录时出现异常。我对另一个 table 尝试了同样的方法,它起作用了,但是对于这个 table,它不起作用。我正在使用 spring-data-jpa
@Entity
@Table(name=USR.TABLE_NAME)
public class USR implements Serializable {
private static final long serialVersionUID = 1L;
public final static String TABLE_NAME = "usr_profile";
@Id
@Column (name="USR_NO")
private Integer usrNo;
@Column (name="USR_Address", length=20, nullable=true, unique=false)
private Integer usrAddress;
@Column (name="USR_COUNTRY", nullable=true, unique=false)
private String usrCountry;
other fields constructor, no-arg constructor, getter and setter removed for brevity
@Repository
public interface USRRepository extends JpaRepository<USR, Integer> {
@Query("SELECT o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt FROM USR o WHERE o.usrNo=?1")
USR findUsrRecordByUsrNo(Integer usrNo);
}
我在这里创建了一个控制器class,它有一个获取映射
@GetMapping ("/CmpUsr")
public ResponseEntity<String> cmpLookup() {
USR us = usrRepository.findUsrRecordByUsrNo(12557);
return new ResponseEntity<>(us.toString(), HttpStatus.OK);
}
我遇到了这个异常
SEVERE:Servlet.service() for servlet [dispatcherServiet] in context with path[] throw exception (Request processing failed; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] 为值键入 [com.newprof.userApp.domain.USR] (12557, 115 Minesota Yellow Rd, US, PH, 000991);嵌套异常是 org.springframework.core.comvert.ConverterNotFoundException:找不到能够从类型 [java.lang.Integer 转换为类型 [com.newprof.userApp.domain.USR]] 的转换器,根本原因是
改变这个:
@Query("SELECT o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt FROM USR o WHERE o.usrNo=?1")
USR findUsrRecordByUsrNo(Integer usrNo);
}
有了这个:
@Query("SELECT new com.newprof.userApp.domain.USR(o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt) FROM USR o WHERE o.usrNo=?1")
USR findUsrRecordByUsrNo(Integer usrNo);
}
在异常情况下显示:无法从类型 [java.lang.Object[]] 转换为类型 [com.newprof.userApp.domain.USR]。
这基本上意味着您的查询语句 returns 一个 对象数组 : java.lang.Object[].
所以我的计划是,您直接在查询中调用 USR 构造函数。
编辑
找到答案。
所以您将此添加到您的查询中:
new com.newprof.userApp.domain.USR(o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt)
它现在对您说:无法解析构造函数 com.newprof.userApp.domain.USR()。这是正确的。因为没有这样的构造函数。您需要将其添加到您的实体中。
您需要添加如下内容:
public USR (Integer usrNo, Integer usrAddress, String usrCountry, String usrState, String usrSt) {
this.usrNo = usrNo;
this.usrAddress = usrAddress;
this.usrCountry = usrCountry;
this.usrState = usrState;
this.usrSt = isrSt;
}
这个全新的构造函数将在声明中调用 new com.newprof.userApp.domain.USR(o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt)
:D
试试这个代码。我认为问题是 return 对象 与 select 值不同。
@Repository
public interface USRRepository extends JpaRepository<USR, Integer> {
@Query("SELECT o FROM USR o WHERE o.usrNo=?1")
USR findUsrRecordByUsrNo(Integer usrNo);
}