Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1c79e3a
adding JPA example
springframeworkguru May 16, 2017
af9c8e8
adding JPA example
springframeworkguru May 16, 2017
80bf60b
adding bootstrap class
springframeworkguru May 16, 2017
b3c04f4
Updating Spring Boot Version
springframeworkguru Jun 19, 2017
405e6a6
Updating Spring Boot Version
springframeworkguru Jun 19, 2017
248ef06
Updating Spring Boot Version
springframeworkguru Jun 19, 2017
ee5272e
Updating Spring Boot Version
springframeworkguru Jul 31, 2017
a0f05ef
Updating Spring Boot Version
springframeworkguru Jul 31, 2017
6648cfb
Updating Spring Boot Version
springframeworkguru Jul 31, 2017
57ad1ae
Corrected typo Worx to Wrox
tiagomestreteixeira Aug 24, 2017
b2dafef
Merge pull request #4 from tiagomestreteixeira/bootstrap
springframeworkguru Aug 24, 2017
3692bd4
adding equals, hash, and toString
springframeworkguru Sep 7, 2017
f7549bc
Merge branch 'equals' into spring-data-jpa
springframeworkguru Sep 7, 2017
f2aa6be
Merge branch 'equals' into bootstrap
springframeworkguru Sep 7, 2017
0b8c61e
Merge remote-tracking branch 'origin/bootstrap' into bootstrap
springframeworkguru Sep 7, 2017
cade2fb
fix to Rod mapping
springframeworkguru Oct 4, 2017
474102a
Merge branch 'master' into jpa-entities
springframeworkguru Nov 10, 2017
6cc1801
Merge branch 'jpa-entities' into equals
springframeworkguru Nov 10, 2017
fea25c5
Merge branch 'equals' into spring-data-jpa
springframeworkguru Nov 10, 2017
3f577d9
Merge branch 'spring-data-jpa' into bootstrap
springframeworkguru Nov 10, 2017
4da7a2d
Updated to 2.0.0.M7
springframeworkguru Dec 9, 2017
a13d2c3
Updated to Spring Boot 2.0.0.RELEASE
springframeworkguru Mar 17, 2018
edf5247
Upgraded to Spring Boot 2.1.0.RELEASE
springframeworkguru Dec 3, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0.0.M5</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,51 @@
package guru.springframework.spring5webapp.bootstrap;

import guru.springframework.spring5webapp.model.Author;
import guru.springframework.spring5webapp.model.Book;
import guru.springframework.spring5webapp.repositories.AuthorRepository;
import guru.springframework.spring5webapp.repositories.BookRepository;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
* Created by jt on 5/16/17.
*/
@Component
public class DevBootstrap implements ApplicationListener<ContextRefreshedEvent> {

private AuthorRepository authorRepository;
private BookRepository bookRepository;

public DevBootstrap(AuthorRepository authorRepository, BookRepository bookRepository) {
this.authorRepository = authorRepository;
this.bookRepository = bookRepository;
}

@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
initData();
}

private void initData(){

//Eric
Author eric = new Author("Eric", "Evans");
Book ddd = new Book("Domain Driven Design", "1234", "Harper Collins");
eric.getBooks().add(ddd);
ddd.getAuthors().add(eric);

authorRepository.save(eric);
bookRepository.save(ddd);


//Rod
Author rod = new Author("Rod", "Johnson");
Book noEJB = new Book("J2EE Development without EJB", "23444", "Wrox" );
rod.getBooks().add(noEJB);
noEJB.getAuthors().add(rod);

authorRepository.save(rod);
bookRepository.save(noEJB);
}
}
92 changes: 92 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/model/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package guru.springframework.spring5webapp.model;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
* Created by jt on 5/16/17.
*/
@Entity
public class Author {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;

@ManyToMany(mappedBy = "authors")
private Set<Book> books = new HashSet<>();

public Author() {
}

public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public Author(String firstName, String lastName, Set<Book> books) {
this.firstName = firstName;
this.lastName = lastName;
this.books = books;
}

public Long getId() {
return id;
}

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Set<Book> getBooks() {
return books;
}

public void setBooks(Set<Book> books) {
this.books = books;
}

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

Author author = (Author) o;

return id != null ? id.equals(author.id) : author.id == null;
}

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

@Override
public String toString() {
return "Author{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", books=" + books +
'}';
}
}
105 changes: 105 additions & 0 deletions src/main/java/guru/springframework/spring5webapp/model/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package guru.springframework.spring5webapp.model;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
* Created by jt on 5/16/17.
*/
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String isbn;
private String publisher;

@ManyToMany
@JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"),
inverseJoinColumns = @JoinColumn(name = "author_id"))
private Set<Author> authors = new HashSet<>();

public Book() {
}

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

public Book(String title, String isbn, String publisher, Set<Author> authors) {
this.title = title;
this.isbn = isbn;
this.publisher = publisher;
this.authors = authors;
}

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;
}

public Set<Author> getAuthors() {
return authors;
}

public void setAuthors(Set<Author> authors) {
this.authors = authors;
}

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

Book book = (Book) o;

return id != null ? id.equals(book.id) : book.id == null;
}

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

@Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", isbn='" + isbn + '\'' +
", publisher='" + publisher + '\'' +
", authors=" + authors +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package guru.springframework.spring5webapp.repositories;

import guru.springframework.spring5webapp.model.Author;
import org.springframework.data.repository.CrudRepository;

/**
* Created by jt on 5/16/17.
*/
public interface AuthorRepository extends CrudRepository<Author, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package guru.springframework.spring5webapp.repositories;

import guru.springframework.spring5webapp.model.Book;
import org.springframework.data.repository.CrudRepository;

/**
* Created by jt on 5/16/17.
*/
public interface BookRepository extends CrudRepository<Book, Long> {
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.h2.console.enabled=true