Spring Data Jpa Repository 没有将实体保存到数据库

Spring Data Jpa Repository not saving entities to database

我在使用 jpa 时遇到了一些困难。我没有收到任何异常,但我无法将任何内容保存到数据库中。我从 Hibernate 转到 Jpa,一切都在那里完美运行。以下是我的文件

application.properties:

logging.level.org.springframework.web=DEBUG
logging.level.ch.generali=DEBUG
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.url=jdbc:h2:mem:party;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
spring.jpa.hibernate.ddl-auto=update

型号:

@Getter
@Setter
@Builder
public class NewClientFormWrapper {
   //Entity
   NaturalPerson person;
   //Entity
   Address[] addresses;
   //Entity
   CommunicationChannel[] communicationChannels;

}

存储库:

public interface NaturalPersonRepository extends JpaRepository<NaturalPerson, Integer> {

    Optional<List<NaturalPerson>> getNaturalPersonByFirstname(String inputFirstname);

    Optional<NaturalPerson> getNaturalPersonById(int id);

    void deleteNaturalPersonById(int id);

}

服务:

@RestController
@RequestMapping("/partner")
public class PartnerService {

@Autowired
NaturalPersonRepository naturalPersonRepository;

@PostMapping(value = "/client")
@Transactional
public ResponseEntity<NewClientFormWrapper> newClient(@RequestBody @Valid NewClientFormWrapper 
request) {

    request.getPerson().setAddresses(Arrays.asList(request.getAddresses()));
    request.getPerson().setCommunicationChannels(Arrays.
       asList(request.getCommunicationChannels()));
    NaturalPerson naturalPerson = naturalPersonRepository.save(request.getPerson());

    NewClientFormWrapper response = NewClientFormWrapper.builder()
            .person(naturalPerson)
            .addresses(naturalPerson.getAddresses().toArray(new Address[0]))
            .communicationChannels(naturalPerson.getCommunicationChannels().toArray(new 
             CommunicationChannel[0]))
            .build();
    return ResponseEntity.ok(response);
    }
  }

提交表单时收到 200 响应,但在数据库中找不到数据

您正在使用 运行 时间数据库 (H2),因此当您调用 APi 时,数据会从内存中显示。实际数据库中没有数据持久化。要将数据保存在实际数据库中,您需要使用实际数据库 url 和驱动程序

设置以下属性
spring.datasource.url=jdbc:h2:mem:party;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
spring.datasource.driver-class-name=org.h2.Driver