spring boot jpa尝试建模现有数据库

时间:2018-01-25 15:19:27

标签: jpa spring-boot hql

我有一个我试图建模的数据库。有3个表,表循环,一个到多个表循环传感器和一个到多个表传感器。我试图获取最新的循环数据,这是我发现的查询:

 Select c.id, cs.cycles, s.* from BizWareUser.Sensor s Inner Join BizWareUser.cyclesensor cs on s.SensorDataCol = cs.id
    Inner Join BizWareUser.cycles c on c.id = cs.cycles
    Where c.id IN (Select max(id) from BizWareUser.cycles cs)

当我在hql中使用它时,我收到此错误:

  

无法写入JSON:HHH000142:

我希望以这种方式建模数据,以便做出正确的解析:

cycles{
   cyclesensors : [
      cyclesensor:{
        sensors : [
          sensor : {
          }
        ]
      }
   ]
}

我对春季启动很新,但我希望在没有明确编写查询的情况下这样做。以下是我的模特:

循环:

  @Entity
    @Data
    @Table(name="cycles", catalog="bizware_asp_test", schema="BizWareUser")
    public class Cycles {
        @Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        @Column(name="id")
        private Long id;
        @Column(name="event_time")
        private Timestamp event_time;
        @Column(name="recipe")
        private Short recipe;
        @Column(name="cycle_duration")
        private Short cycle_duration;
        @Column(name="gondola")
        private Short gondola;
        @Column(name="part_index")
        private Short part_index;
        @Column(name="part_name")
        private String part_name;
        @Column(name="shift_count")
        private Short shift_count;
        @Column(name="pour_time")
        private Short pour_time;
        @Column(name="shift2")
        private String shift2;
        @Column(name="iron_temp")
        private Short iron_temp;
        @Column(name="alarm")
        private Integer alarm;
        @Column(name="pour_weight")
        private Short pour_weight;
        @Column(name="target_weight")
        private Short target_weight;
        @Column(name="pour_operator_id")
        private String pour_operator_id;
        @Column(name="cluster_weight")
        private Short cluster_weight;
        @Column(name="furnace")
        private Character furnace;
        @Column(name="heat")
        private Character heat;
        @Column(name="tap")
        private Character tap;
        @Column(name="recipe_A")
        private Short recipe_A;
        @Column(name="recipe_B")
        private Short recipe_B;
        @Column(name="partname_A")
        private String partname_A;
        @Column(name="partname_B")
        private String partname_B;
        @Column(name="fadetime")
        private Integer fadetime;
        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "cycles")
        @JsonManagedReference
        private Set<CycleSensor> cycleSensors = new HashSet<>();

CycleSensor:

  @Entity
    @Data
    @Table(name="cyclesensor", catalog="bizware_asp_test", schema="BizWareUser")
    public class CycleSensor {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name="id")
        private Integer Id;
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "cycles", nullable = false)
        @JsonBackReference
        private Cycles cycles;
        @Column(name="compactor")
        private String compactor;
        @OneToMany
        @JoinColumn(name="Id")
        private Set<Sensor> sensors;

        private CycleSensor(){}

传感器:

 @Entity
    @Data
    @Table(name="Sensor", catalog="bizware_asp_test", schema="BizWareUser")
    public class Sensor {
        @Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        @Column(name="Id")
        private Integer Id;
        @Column(name="Channel")
        private Integer Channel;
        @Column(name="DateTime")
        private Timestamp DateTime;
        @Column(name="Unit")
        private String Unit;
        @Column(name="Peak")
        private Float Peak;
        @Column(name="Phase")
        private Float Phase;
        @Column(name="Amplitude")
        private Float Amplitude;
        @ManyToOne
        @JoinColumn(name="SensorDataCol")
        private CycleSensor CycleSensor;

CycleRepository:

  @Repository
    public interface CyclesRepository extends CrudRepository<Cycles, Long> {   
@Query("Select s, cs, c from Sensor s Join s.CycleSensor as cs Join cs.cycles as c Where c.id IN (Select max(id) from Cycles)")
        List<Cycles> findTop1ByOrderByIdDesc();      
    }

SensorRepository:

@Repository
public interface SensorRepository extends CrudRepository<Sensor, Long> {

    @Query("Select s, cs, c from Sensor s Join s.CycleSensor as cs Join cs.cycles as c Where c.id IN (Select max(id) from Cycles)")
    List<Sensor> findTop1ByOrderByIdDesc();


}

当我从循环控制器访问时,我会收到:

  

java.lang.ClassCastException:[Ljava.lang.Object;无法施展   muellerspring.compactionsystemmonitor.Cycles at   muellerspring.compactionsystemmonitor.CyclesController.index(CyclesController.java:16)   〜[classes /:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native   方法)〜[na:1.8.0_151] at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)   〜[na:1.8.0_151] at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)   〜[na:1.8.0_151] at java.lang.reflect.Method.invoke(Method.java:498)   〜[na:1.8.0_151] at   org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)   〜[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)   〜[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)   〜[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)   〜[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)   〜[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)   〜[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)   〜[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)   〜[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)   〜[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)   〜[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   javax.servlet.http.HttpServlet.service(HttpServlet.java:635)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)   〜[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   javax.servlet.http.HttpServlet.service(HttpServlet.java:742)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)   〜[tomcat-embed-websocket-8.5.23.jar:8.5.23] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)   〜[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)   〜[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)   〜[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)   〜[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)   〜[tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:868)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1459)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)   [na:1.8.0_151] at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:624)   [na:1.8.0_151] at   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)   [tomcat-embed-core-8.5.23.jar:8.5.23] at   java.lang.Thread.run(Thread.java:748)[na:1.8.0_151]

当我从传感器控制器运行时,我明白了。

  

无法编写JSON:HHH000142:Javassist Enhancement失败:   muellerspring.compactionsystemmonitor.Cycles;嵌套异常是   com.fasterxml.jackson.databind.JsonMappingException:HHH000142:   Javassist Enhancement失败:   muellerspring.compactionsystemmonitor.Cycles(通过参考链:   的java.util.ArrayList [0] - &GT; muellerspring.compactionsystemmonitor.Sensor [&#34; cycleSensor&#34;] - &GT; muellerspring.compactionsystemmonitor.CycleSensor [&#34;传感器&#34;])

CyclesController

@RestController
public class CyclesController {
    @Autowired
    private CyclesRepository cyclesRepository;

    @RequestMapping(value="/cycles", method=RequestMethod.GET)
    public Cycles index(){
        List<Cycles> cycles = cyclesRepository.findTop1ByOrderByIdDesc();
        return cycles.get(0);
    }

    @RequestMapping(value="/cycles/count", method=RequestMethod.GET)
    public Long Count(){
        return cyclesRepository.count();
    }
}

传感器控制器:

@RestController
public class SensorController {
    @Autowired
    private SensorRepository sensorRepository;
    @RequestMapping(value="/sensors", method=RequestMethod.GET)
    public List<Sensor> index(){
        List<Sensor> sensors = sensorRepository.findTop1ByOrderByIdDesc();
        return sensors;
    }

}

0 个答案:

没有答案