【问题标题】:findAll() using JpaRepository cause an exception - SpringBootfindAll() 使用 JpaRepository 导致异常 - SpringBoot
【发布时间】:2017-03-24 15:18:07
【问题描述】:

我需要使用 spring-boot 从表中加载数据。所以我尝试如下。

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

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

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

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

在main方法中,我试过了,

@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());
        }
    }
}

我可以通过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

我尝试将spring.jpa.hibernate.ddl-auto 设置为validate 以检查架构错误,但没有任何错误。请帮忙。

【问题讨论】:

  • 我的猜测是,由于您使用“new”运算符创建服务对象,它不会将您的“存储库”对象注入其中。
  • 谢谢。但是如何注入呢?
  • 非常感谢。有效。 :-)
  • 或者你可以使用@Configurable for Spring 将JMXNodeRepository 注入到JMXNodeService 中,当你使用new 操作符创建它时。你可以在这里找到更多:stackoverflow.com/questions/29167500/…

标签: java spring hibernate spring-boot spring-data-jpa


【解决方案1】:

感谢您的帮助。解决我的问题的是,使用 JMXNodeService 的注入实例而不是创建新实例。

【讨论】:

  • 如果你能做到这一点,这是最Spring的做法,如果你必须自己管理你的实例,你可以使用@Inject@Configurable
  • 谢谢。 :-) 我刚刚使用了@Autowired。
猜你喜欢
  • 2020-04-16
  • 2015-10-31
  • 2014-05-31
  • 1970-01-01
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
相关资源
最近更新 更多