简体   繁体   English

Spring 数据 jpa 异常

[英]Spring data jpa exception

While retrieving records from the DB table we are getting exceptions.从数据库表中检索记录时出现异常。 I tried the same for another table it worked but for this table, it's not working.我对另一个有效的表进行了相同的尝试,但对于该表,它不起作用。 I am using spring-data-jpa我正在使用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);
}

Here I have created a Controller class that has a get mapping这里我创建了一个 Controller class 有一个 get 映射

@GetMapping ("/CmpUsr")
public ResponseEntity<String> cmpLookup() {
USR us = usrRepository.findUsrRecordByUsrNo(12557);
return new ResponseEntity<>(us.toString(), HttpStatus.OK);
}

I am getting this exception我收到这个例外

SEVERE: Servlet.service() for servlet [dispatcherServiet] in context with path[] threw exception (Request processing failed; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [com.newprof.userApp.domain.USR] for value (12557, 115 Minesota Yellow Rd, US, PH, 000991); nested exception is org.springframework.core.comvert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer to type [com.newprof.userApp.domain.USR]] with root cause严重: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]] 的根本原因

Change this:改变这个:

@Query("SELECT o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt FROM USR o WHERE o.usrNo=?1") 
USR findUsrRecordByUsrNo(Integer usrNo);
}

with this:有了这个:

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

In exception it says: Failed to convert from type [java.lang.Object[]] to type [com.newprof.userApp.domain.USR].例外情况是:无法从类型 [java.lang.Object[]] 转换为类型 [com.newprof.userApp.domain.USR]。

Which basically means that your query statement returns an object array : java.lang.Object[].这基本上意味着您的查询语句返回一个object 数组:java.lang.Object[]。

So my plan is, you call your USR constructor straightaway in your query.所以我的计划是,您直接在查询中调用您的USR 构造函数

EDIT编辑

FOUND THE ANSWER.找到答案。

So you added this into your query:所以你把这个添加到你的查询中:

new com.newprof.userApp.domain.USR(o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt)

And it now says to you: Cannot resolve constructor com.newprof.userApp.domain.USR().它现在对您说:无法解析构造函数 com.newprof.userApp.domain.USR()。 IT IS RUGHT.这是正确的。 Because there is no such constructor .因为没有这样的构造函数 You need to add it to your entity.您需要将其添加到您的实体中。

You need to add something like this:您需要添加如下内容:

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

This brand new Constructor will be called in statement new com.newprof.userApp.domain.USR(o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt) :D这个全新的构造函数将在声明中被调用new com.newprof.userApp.domain.USR(o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt) :D

Try this code.试试这段代码。 I think the problem is the return object witch is not the same with select values.我认为问题是返回 object 女巫与 select 值不同。

@Repository
public interface USRRepository extends JpaRepository<USR, Integer> {

   @Query("SELECT o FROM USR o WHERE o.usrNo=?1") 
   USR findUsrRecordByUsrNo(Integer usrNo);

}

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

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