简体   繁体   English

使用JpaRepository的findAll()导致异常-SpringBoot

[英]findAll() using JpaRepository cause an exception - SpringBoot

I need to load data from a table using spring-boot. 我需要使用spring-boot从表中加载数据。 So I tried it like below. 所以我尝试如下。

JMXNode.java JMXNode.java

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

    @Entity
    @Table(name = "jmx_nodes")
    public class JMXNode {

        @Column(name = "node_id")
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long nodeId;

        @Column(name = "node_name")
        @NotNull
        @Size(max = 30)
        private String nodeName;

        @Column(name = "hostname")
        @NotNull
        @Size(max = 50)
        private String hostname;

        @Column(name = "port")
        @NotNull
        private int port;

        @Column(name="username")
        @Size(max = 50)
        private String username;

        @Column(name="password")
        @Size(max = 50)
        private String password;

        @Column(name="auth_required")
        @NotNull
        private boolean authRequired;

        @Column(name = "ssl_required")
        @NotNull
        private boolean sslRequired;

        protected JMXNode() {}

        public JMXNode(String nodeName,
                       String hostname,
                       int port,
                       boolean authRequired,
                       boolean sslRequired) {
            this.nodeName = nodeName;
            this.hostname = hostname;
            this.port = port;
            this.authRequired = authRequired;
            this.sslRequired = sslRequired;
        }

        public long getNodeId() {
            return nodeId;
        }

        public String getNodeName() {
            return nodeName;
        }

        public String getHostname() {
            return hostname;
        }

        public Integer getPort() {
            return port;
        }

        public String getUsername() {
            return username;
        }

        public String getPassword() {
            return password;
        }

        public boolean isAuthRequired() {
            return authRequired;
        }

        public boolean isSslRequired() {
            return sslRequired;
        }

        public void setNodeId(long nodeId) {
            this.nodeId = nodeId;
        }

        public void setNodeName(String nodeName) {
            this.nodeName = nodeName;
        }

        public void setHostname(String hostname) {
            this.hostname = hostname;
        }

        public void setPort(Integer port) {
            this.port = port;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public void setAuthRequired(boolean authRequired) {
            this.authRequired = authRequired;
        }

        public void setSslRequired(boolean sslRequired) {
            this.sslRequired = sslRequired;
        }
    }

JMXNodeRepository.java JMXNodeRepository.java

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
public interface JMXNodeRepository extends JpaRepository<JMXNode, Long> {

    // Returns a list of JMXNode objects
    List<JMXNode> findAll();

    // Find JMX node information by node id
    JMXNode findByNodeId(long nodeId);

    // Delete JMX node entry from database
    Long removeByNodeId(long nodeId);

    // Add new JMX node information to database
    JMXNode save(JMXNode jmxNode);
}

JMXNodeService.java JMXNodeService.java

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

public class JMXNodeService {

    @Autowired
    private JMXNodeRepository jmxNodeRepository;

    // Returns list of JMXNode objects
    public List<JMXNode> findAll() {
        return jmxNodeRepository.findAll();
    }

    // Find JMXNode object by node ID
    public JMXNode findByNodeId(long nodeId) {
        return jmxNodeRepository.findByNodeId(nodeId);
    }

    // Delete JMXNode entry from database
    public Long removeByNodeId(long nodeId) {
        return jmxNodeRepository.removeByNodeId(nodeId);
    }

    // Save JMXNode entry in database
    public JMXNode save(JMXNode jmxNode) {
        return jmxNodeRepository.save(jmxNode);
    }
}

application.properties application.properties

spring.datasource.url=jdbc:h2:file:~/db
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.datasource.username=sa
spring.datasource.password=

spring.h2.console.enabled=true
spring.h2.console.path=/console

spring.jpa.show_sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy

spring.datasource.initialize=true
spring.datasource.continue-on-error=false

And in the main method, I tried, 在主要方法中,我尝试了

@SpringBootApplication
public class DepliApplication extends AsyncConfigurerSupport {

    public static NodeDataMap nodeDataMap = new NodeDataMap();

    public static void main(String[] args) throws IOException {
        SpringApplication.run(DepliApplication.class, args);

        JMXNodeService jmxNodeService = new JMXNodeService();
        List<JMXNode> jmxNodes = jmxNodeService.findAll();

        for(int i = 1; i < jmxNodes.size(); i++) {
            System.out.println(jmxNodes.get(i).getPort());
        }
    }
}

I can clearly see the data through h2 console, but output pumps this exception, 我可以通过h2控制台清楚地看到数据,但是输出会泵出此异常,

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
    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.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException
    at com.depli.service.JMXNodeService.findAll(JMXNodeService.java:21)
    at com.depli.DepliApplication.main(DepliApplication.java:28)
    ... 5 more

I tried setting spring.jpa.hibernate.ddl-auto to validate to check for schema errors, but there were not any errors. 我尝试设置spring.jpa.hibernate.ddl-auto进行validate以检查架构错误,但是没有任何错误。 Please help. 请帮忙。

Thanks for the help. 谢谢您的帮助。 What fixed my problem was, using the injected instance of JMXNodeService instead of creating a new instance. 解决我问题的是,使用注入的JMXNodeService实例而不是创建新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 <SpringBoot / Hibernate>调用 JpaRepository.findAll 时的 InvocationException(示例) - <SpringBoot / Hibernate> InvocationException on calling JpaRepository.findAll(Example example) 在 ModelAndView 中使用 Jparepository 的 FindAll 方法 - Using Jparepository's FindAll method with ModelAndView 如何使用springboot findAll()方法不写子句 - how to write not in clause using springboot findAll() method 使用 Springboot JpaRepository 获取存储在数据库中的最后一个元素 - Using Springboot JpaRepository to get the last element stored in the database JpaRepository findAll() 正在获取嵌套对象 - JpaRepository findAll() is fetching nested objects JpaRepository findAll() 返回空结果 - JpaRepository findAll() returns empty result 如果没有抛出异常,JpaRepository 的 findAll() 是否保证返回非空结果? - Is JpaRepository's findAll() guaranteed to return a non-null result if an exception isn't thrown? 如何使用谓词通过JpaRepository.findAll获得对象的不同列表? - How do I get a distinct list of objects with JpaRepository.findAll using a Predicate? 在 JpaRepository findAll() 方法中返回对象的浅拷贝 - Return a shallow copy of an object in JpaRepository findAll() method 如何在 JpaRepository 中定义自定义 findAll 方法? - How to define custom findAll methods in JpaRepository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM