Skip to content

Commit

Permalink
Merge pull request #1 from JOAONUNES96/spring-data-jpa
Browse files Browse the repository at this point in the history
Spring data jpa
  • Loading branch information
JOAONUNES96 authored Jun 13, 2023
2 parents bb70907 + d247cce commit 32e23c6
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package guru.springframework.spring5webapp.bootstrap;

import guru.springframework.spring5webapp.domain.Book;
import guru.springframework.spring5webapp.repositories.BookRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
* Created by João Nunes on 12/06/2023.
*/
@Component
public class DataInitializer implements CommandLineRunner {

private final BookRepository bookRepository;

public DataInitializer(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}


@Override
public void run(String... args) throws Exception {
Book bookDDD = new Book("Domain Driven Design", "123", "RandomHouse");

System.out.println("Id: " + bookDDD.getId());

Book savedDDD = bookRepository.save(bookDDD);

System.out.println("Id: " + savedDDD.getId());

Book bookSIA = new Book("Spring in Action", "123", "RandomHouse");

Book savedSIA = bookRepository.save(bookSIA);

bookRepository.findAll().forEach(book -> {
System.out.println("Book Id: " + book.getId());
System.out.println("Book Title: " + book.getTitle());
});
}
}
73 changes: 73 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/domain/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package guru.springframework.spring5webapp.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects;

@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String isbn;
private String publisher;

public Book() {
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Book book = (Book) o;

return Objects.equals(id, book.id);
}

@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}

public Book(String title, String isbn, String publisher) {
this.title = title;
this.isbn = isbn;
this.publisher = publisher;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public String getPublisher() {
return publisher;
}

public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package guru.springframework.spring5webapp.repositories;

import guru.springframework.spring5webapp.domain.Book;
import org.springframework.data.jpa.repository.JpaRepository;


// I could also have used the annotations
public interface BookRepository extends JpaRepository<Book, Long> {
}
16 changes: 16 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#spring.jpa.show-sql=true

#Show SQL
spring.jpa.properties.hibernate.show_sql=true

#Format SQL
spring.jpa.properties.hibernate.format_sql=true

#Show bind values
logging.level.org.hibernate.type.descriptor.sql=trace

spring.h2.console.enabled=true




0 comments on commit 32e23c6

Please sign in to comment.