Spring数据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);
}
在这里,我创建了一个具有get映射的控制器类,
@GetMapping ("/CmpUsr")
public ResponseEntity<String> cmpLookup() {
USR us = usrRepository.findUsrRecordByUsrNo(12557);
return new ResponseEntity<>(us.toString(), HttpStatus.OK);
}
我会
在使用路径[]的上下文中获得Servlet [DispatcherServiet]的Servlet.Service()for Servlet.Service()投掷异常(请求处理失败;嵌套异常是org.springframework.core.convert.convert.conversionfailedexception:无法从类型[java.lang.object []]转换为type [com.newprof.userapp.domain.usr] 12557,115 Minesota Yellow Rd,US,PH,000991) org.springframework.core.comvert.converternotfoundexception:未发现转换器能够从类型[java.lang.integer转换为type [com.newprof.userapp.domain.usr]]
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
@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
@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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个代码。我认为问题是返回对象与选择值不同。
Try this code. I think the problem is the return object witch is not the same with select values.
更改此:
与此:
在例外情况下,它显示:无法从类型 [java.lang.Object[]] 转换为类型 [com.newprof.userApp.domain.USR]。
这基本上意味着您的查询语句返回一个对象数组:java.lang.Object[]。
所以我的计划是,您在查询中直接调用 USR 构造函数。
编辑
找到了答案。
因此,您将其添加到查询中:
现在它对您说:无法解析构造函数 com.newprof.userApp.domain.USR()。这是正确的。因为不存在这样的构造函数。您需要将其添加到您的实体中。
您需要添加如下内容:
这个全新的构造函数将在语句
new com.newprof.userApp.domain.USR(o.usrNo, o.usrAddress, o.usrCountry, o.usrState, o.usrSt )
:DChange this:
with this:
In exception it says: Failed to convert from type [java.lang.Object[]] to type [com.newprof.userApp.domain.USR].
Which basically means that your query statement returns an object array: java.lang.Object[].
So my plan is, you call your USR constructor straightaway in your query.
EDIT
FOUND THE ANSWER.
So you added this into your query:
And it now says to you: Cannot resolve constructor 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:
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