Spring boot 使用空id保存实体JPA

Spring boot 使用空id保存实体JPA,spring-boot,jpa,Spring Boot,Jpa,我正在尝试创建一个注册用户的函数 @Service public class RegistrationService { private AccountRepository accountRepository; private UserRepository userRepository; private final Logger logger = LogManager.getLogger(RegistrationService.class); publi

我正在尝试创建一个注册用户的函数

@Service
public class RegistrationService {
    private AccountRepository accountRepository;
    private UserRepository userRepository;
    private final Logger logger = LogManager.getLogger(RegistrationService.class);
    
    public Boolean createNewUser(Registration registration) {
        User user = new User();
        user.setUserName(registration.getUserName());
        user.setPassword(registration.getPassword());
        logger.info("attempting to create user: {}", user);
        
        try {
            userRepository.save(user);          
        } catch(Exception e) {
            logger.error("Failed to register user: {}", e.getStackTrace());
            return false;
        }
        
        Account account = new Account();
        account.setUserId(user.getId());
        account.setBillingAddress(registration.getBillingAddress());
        account.setBillingCountry(registration.getBillingCountry());
        account.setBillingProvince(registration.getBillingProvince());
        account.setPhoneNumber(registration.getPhoneNumber());
        account.setZip(registration.getZip());
        logger.info("attempting to create account: {}", account);
        
        try {
            account = accountRepository.save(account);          
        } catch(Exception e) {
            logger.error("Failed to register account: {}", e.getStackTrace());
            return false;
        }
        
        
        return true;
    }
}
我听从了这篇帖子的建议,但这个解决方案对我来说并不管用。 这是我的用户类

package com.company.app.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Data;

@Entity
@Table(name="user")
@Data
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String userName;

    private String password;

    
    public Long getId() {
        return this.id;
    }
    
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public String getUserName() {
        return this.userName;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getPassword() {
        return this.password;
    }
}
这是我的会计课

package com.company.app.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Data;

@Entity
@Table(name="account")
@Data
public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private Long userId;

    private String billingAddress;
    
    private String billingProvince;
    
    private String billingCountry;

    private String phoneNumber;

    private String zip;
    
    public Long getId() {
        return this.id;
    }
    
    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getUserId() {
        return userId;
    }

    public String getBillingAddress() {
        return billingAddress;
    }

    public void setBillingAddress(String billingAddress) {
        this.billingAddress = billingAddress;
    }

    public String getBillingProvince() {
        return billingProvince;
    }

    public void setBillingProvince(String billingProvince) {
        this.billingProvince = billingProvince;
    }

    public String getBillingCountry() {
        return billingCountry;
    }

    public void setBillingCountry(String billingCountry) {
        this.billingCountry = billingCountry;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }
}

我的日志显示以下输出

2021-04-14 14:40:09.265  INFO 15092 --- [nio-8080-exec-1] c.c.app.service.RegistrationService      : attempting to create user: User(id=null, userName=evan, password=test123)
2021-04-14 14:40:09.266 ERROR 15092 --- [nio-8080-exec-1] c.c.app.service.RegistrationService      : Failed to register user: com.company.app.service.RegistrationService.createNewUser(RegistrationService.java:26)
在此方面的任何帮助都将不胜感激。这是堆栈跟踪 java.lang.IllegalArgumentException:未知返回值类型:org.springframework.http.HttpStatus 在org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.HandlerReturnValue(HandlerMethodReturnValueHandlerComposite.java:80)~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.ServletinInvocableHandlerMethod.invokeAndHandle(ServletinInvocableHandlerMethod.java:124)~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在javax.servlet.http.HttpServlet.service(HttpServlet.java:660)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)~[tomcat-embed-websocket-9.0.27.jar:9.0.27] 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)~[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)[tomcat-embed-core-9.0.27.jar:9.0.27] 位于org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)[tomcat-embed-core-9.0.27.jar:9.0.27] 位于org.apache.tomcat.util.net.niodendpoint$SocketProcessor.doRun(niodendpoint.java:1579)[tomcat-embed-core-9.0.27.jar:9.0.27] 在org.apache.t
`ID` bigint(20) NOT NULL AUTO_INCREMENT,