Home > Blockchain >  Spring data jpa exception
Spring data jpa exception

Time:04-14

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

CodePudding user response:

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

CodePudding user response:

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

}
  • Related