forked from springframeworkguru/spring5webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from JOAONUNES96/spring-data-jpa
Spring data jpa
- Loading branch information
Showing
5 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/guru/springframework/spring5webapp/bootstrap/DataInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
73
src/main/java/guru/springframework/spring5webapp/domain/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
||
|