我一直在使用Spring Boot的Spring Data。使用JavaConfig文档,我已经设置了一个Spring JPA配置,但是当我在我的存储库中调用save方法时,会抛出一个空指针。
我的存储库:
import org.springframework.data.jpa.repository.JpaRepository;
public interface HouseRepository extends JpaRepository<House, Long> {
}
我的POJO:
AbstractHouse
@MappedSuperclass
@Table(name="houses")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class AbstractHouse implements Serializable {
@NotEmpty
private String houseName;
@NotEmpty
private String houseNumber;
楼
@Entity
public class SmallHouse extends AbstractHouse {
@Id
@GeneratedValue
private Long id;
private Date created;
private Date updated;
然后我得到了一个服务对象,该服务对象采用Autowired存储库并尝试使用通过JPA获得的方法进行保存:
@Service
public class HouseService {
@Autowired
protected HouseRepository houseRepository;
public void save(House house) {
houseRepository.save(house);
}
我的应用程序类包含:
@SpringBootApplication
@EnableScheduling
@EnableJpaRepositories
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
我是Spring Data的新手,所以我确实认为它在配置中我缺少的东西,任何建议都将不胜感激!
- 更新 -
堆栈追踪:
java.lang.NullPointerException
at com.jm.service.HouseService.save(HouseService.java:15)
at com.jm.service.HouseClass.gatherHouses(HouseClass.java:43)
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:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
我称之为方法:
public class HouseMain {
@Autowired
public HouseService houseService;
public void gatherHouse() {
houseService = new HouseService();
House h = new House();
h.setHouseName("House1");
h.setHouseNumber("12");
try {
HouseService.save(vmComp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
然后在我的控制器中,我打电话给house.gatherHouse
答案 0 :(得分:7)
您目前手动实例化HouseService
,而不是通过Spring实例化。在您创建的houseService
对象中,您有HouseRepository houseRepository
字段null
,因为您没有以任何方式初始化它。如果您使用Spring及其Autowired
功能,那么您应该从Spring上下文中获取HouseService
的实例。例如。你可以@Autowired
进入你的控制器类。
答案 1 :(得分:2)
这是不正确的:
houseService = new HouseService();
因为您正在使用Spring
,所以应该允许Spring创建所有bean,这就是使用@Autowired
的原因,在这种情况下,initialised
没有执行,而且houseRepository是null
为public class ConflictItem<T> where T : new()
{
public T CurrentItem { get; set; }
public T RequestedUpdateItem { get; set; }
}
。