Spring数据jpa异常

发布于 2025-01-21 00:29:27 字数 1554 浏览 0 评论 0原文

在从数据库表中检索记录时,我们正在获得异常。我尝试了另一个桌子的工作,但是对于这张桌子,它不起作用。我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2025-01-28 00:29:27

试试这个代码。我认为问题是返回对象与选择值不同。

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

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

}

Try this code. I think the problem is the return object witch is not the same with select values.

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

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

}
旧人哭 2025-01-28 00:29:27

更改此:

@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]。

这基本上意味着您的查询语句返回一个对象数组: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

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].

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:

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(). 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文