Skip to content

Latest commit

 

History

History
84 lines (61 loc) · 2.53 KB

spring-data-jpa-create-application.md

File metadata and controls

84 lines (61 loc) · 2.53 KB
ms.dateauthorms.authorms.reviewer
02/22/2023
KarlErickson
karler
seal
  1. Create a new Todo Java class. This class is a domain model mapped onto the todo table that will be created automatically by JPA. The following code ignores the getters and setters methods.

    packagecom.example.demo; importjavax.persistence.Entity; importjavax.persistence.GeneratedValue; importjavax.persistence.Id; @EntitypublicclassTodo { publicTodo() { } publicTodo(Stringdescription, Stringdetails, booleandone) { this.description = description; this.details = details; this.done = done; } @Id@GeneratedValueprivateLongid; privateStringdescription; privateStringdetails; privatebooleandone; }
  2. Edit the startup class file to show the following content.

    importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.boot.context.event.ApplicationReadyEvent; importorg.springframework.context.ApplicationListener; importorg.springframework.context.annotation.Bean; importorg.springframework.data.jpa.repository.JpaRepository; importjava.util.stream.Collectors; importjava.util.stream.Stream; @SpringBootApplicationpublicclassDemoApplication { publicstaticvoidmain(String[] args) { SpringApplication.run(DemoApplication.class, args); } @BeanApplicationListener<ApplicationReadyEvent> basicsApplicationListener(TodoRepositoryrepository) { returnevent->repository .saveAll(Stream.of("A", "B", "C").map(name->newTodo("configuration", "congratulations, you have set up correctly!", true)).collect(Collectors.toList())) .forEach(System.out::println); } } interfaceTodoRepositoryextendsJpaRepository<Todo, Long> { }

    [!INCLUDE spring-default-azure-credential-overview.md]

  3. Start the application. You'll see logs similar to the following example:

    2023-02-01 10:29:19.763 DEBUG 4392 --- [main] org.hibernate.SQL : insert into todo (description, details, done, id) values (?, ?, ?, ?) com.example.demo.Todo@1f
close