我需要使用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
在主要方法中,我试过了,
@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
以检查架构错误,但没有任何错误。请帮忙。
答案 0 :(得分:0)
感谢您的帮助。解决了我的问题的原因是,使用注入的JMXNodeService
实例而不是创建新实例。