使用@query或命名查询从Spring Boot存储库中从DB获取数据的问题

时间:2019-03-25 08:00:29

标签: java sql-server spring spring-boot

我是Spring Boot的新手,同时面对许多问题,

  1. 首先是将结果从@query Or @NamedQuery转换为模型class,我尝试了从各种论坛获得的所有选择,但无法通过,例如将其转换为Object[] array(like Object[] tuple= (Object[])iterator.next();),但是我遇到了编译错误,到目前为止,我正在运行nativeQuery with resultSetMapping,它运行正常,但是我也希望它也可以运行到@query Or @NamedQuery

模型类:-
       @SqlResultSetMapping(name = "querysmsOutgoing", classes = { @ConstructorResult(targetClass = SMSOutgoing.class, columns = { @ColumnResult(name = "id", type = Long.class), @ColumnResult(name = "receiver", type = String.class), @ColumnResult(name = "msg", type = String.class), @ColumnResult(name = "operator", type = String.class), @ColumnResult(name = "msgtype", type = String.class), @ColumnResult(name = "sender", type = String.class), @ColumnResult(name = "status", type = String.class), @ColumnResult(name = "reference", type = String.class), @ColumnResult(name = "driverId", type = Long.class), @ColumnResult(name = "companyId", type = Long.class), @ColumnResult(name = "createUser", type = Long.class), @ColumnResult(name = "createDate", type = Timestamp.class), @ColumnResult(name = "smstype", type = Integer.class), @ColumnResult(name = "senttime", type = String.class), @ColumnResult(name = "receivedtime", type = String.class) }) })

 @Entity
  @NamedNativeQuery(name = "SMSOutgoing.findByStatus", query = "select out 
   from SMSOutgoing out where LOWER(out.status) = LOWER(?:status)", 
    resultSetMapping = "querysmsOutgoing")
  @Table(name = "ozekimessageout", schema = "dbo")
  public class SMSOutgoing implements Serializable`    

存储库类:-

  //    @Query("select out.id, out.receiver, out.msg, out.operator, 
        out.msgtype, out.sender, out.status, out.reference from SMSOutgoing 
        out where LOWER(out.status)=LOWER(:status)")    
        @Query(nativeQuery = true)
        public List<SMSOutgoing> findByStatus(@Param("status") String 
         status);`

下面的日志跟踪:-

    `Hibernate: select smsoutgoin0_.id as col_0_0_, smsoutgoin0_.receiver as 
     col_1_0_, smsoutgoin0_.msg as col_2_0_, smsoutgoin0_.operator as 
  col_3_0_, smsoutgoin0_.msgtype as col_4_0_, smsoutgoin0_.sender as 
  col_5_0_, smsoutgoin0_.status as col_6_0_, smsoutgoin0_.reference as 
  col_7_0_ from dbo.ozekimessageout smsoutgoin0_ where 
  lower(smsoutgoin0_.status)=lower(?)
  smsOutgoing:size::::6
  Inside test catch block::::::::::
  java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to 
  com.avaal.sms.model.SMSOutgoing
  at com.avaal.sms.service.StatusService.findByStatus(StatusService.java:53)
  at com.avaal.sms.service.StatusService$$FastClassBySpringCGLIB$$f34fcc51.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.avaal.sms.service.StatusService$$EnhancerBySpringCGLIB$$c21d1bad.findByStatus(<generated>)
at com.avaal.sms.service.SmsPluginApplicationTests.sendSMS(SmsPluginApplicationTests.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)`

请协助我解决它。

1 个答案:

答案 0 :(得分:0)

您的第一个问题已通过以下结构解决:

Model.class是简单的变量声明,getter和setter之类的东西。

@Entity
public class Model {
 @Id
 private long id;
 private String name;
 //getter
 //setter
}

存储库类:

public ModelRepo extends CrudRepository<Model, Long>{

    @Query("select out from SMSOutgoing out where out.status = ?0", nativeQuery = true)
    Object[] getData(String status);

}

服务层:

public class ModelServiceImpl implements ModelService {

  @Autowired
  private ModelRepo modelRepo;

  @Overried
  public List<Model> getModels() {
    String status = "XXX";
    Object[] objs = modelRepo.getData(status);
    if (objs != null && objs.length > 0) {
      for(Object obj : objs) {
         Model model = new Model();
         //cast obj to array and get values via index of the column in db and put that in your model
      }
    }
  }

}