diff --git a/spring-data-rest/src/main/java/com/baeldung/models/Address.java b/spring-data-rest/src/main/java/com/baeldung/models/Address.java new file mode 100644 index 0000000000..98cf5f0869 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/models/Address.java @@ -0,0 +1,53 @@ +package com.baeldung.models; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class Address { + + @Id + @GeneratedValue + private long id; + + @Column(nullable = false) + private String location; + + @OneToOne(mappedBy = "address") + private Library library; + + public Address() { + } + + public Address(String location) { + super(); + this.location = location; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + public Library getLibrary() { + return library; + } + + public void setLibrary(Library library) { + this.library = library; + } +} diff --git a/spring-data-rest/src/main/java/com/baeldung/models/Author.java b/spring-data-rest/src/main/java/com/baeldung/models/Author.java new file mode 100644 index 0000000000..7025fa4ad3 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/models/Author.java @@ -0,0 +1,59 @@ +package com.baeldung.models; + +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; + +@Entity +public class Author { + + @Id + @GeneratedValue + private long id; + + @Column(nullable = false) + private String name; + + @ManyToMany(cascade = CascadeType.ALL) + @JoinTable(name = "book_author", joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "author_id", referencedColumnName = "id")) + private List books; + + public Author() { + } + + public Author(String name) { + super(); + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public List getBooks() { + return books; + } + + public void setBooks(List books) { + this.books = books; + } +} diff --git a/spring-data-rest/src/main/java/com/baeldung/models/Book.java b/spring-data-rest/src/main/java/com/baeldung/models/Book.java new file mode 100644 index 0000000000..8f836a259a --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/models/Book.java @@ -0,0 +1,70 @@ +package com.baeldung.models; + +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; + +@Entity +public class Book { + + @Id + @GeneratedValue + private long id; + + @Column(nullable = false) + private String title; + + @ManyToOne + @JoinColumn(name = "library_id") + private Library library; + + @ManyToMany(mappedBy = "books") + private List authors; + + public Book() { + } + + public Book(String title) { + super(); + this.title = title; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Library getLibrary() { + return library; + } + + public void setLibrary(Library library) { + this.library = library; + } + + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/models/Library.java b/spring-data-rest/src/main/java/com/baeldung/models/Library.java new file mode 100644 index 0000000000..61eead16ea --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/models/Library.java @@ -0,0 +1,73 @@ +package com.baeldung.models; + +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; + +import org.springframework.data.rest.core.annotation.RestResource; + +@Entity +public class Library { + + @Id + @GeneratedValue + private long id; + + @Column + private String name; + + @OneToOne + @JoinColumn(name = "address_id") + @RestResource(path = "libraryAddress") + private Address address; + + @OneToMany(mappedBy = "library") + private List books; + + public Library() { + } + + public Library(String name) { + super(); + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + public List getBooks() { + return books; + } + + public void setBooks(List books) { + this.books = books; + } + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/AddressRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/AddressRepository.java new file mode 100644 index 0000000000..1cc7527e80 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/AddressRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.repositories; + +import org.springframework.data.repository.CrudRepository; + +import com.baeldung.models.Address; + +public interface AddressRepository extends CrudRepository { + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/AuthorRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/AuthorRepository.java new file mode 100644 index 0000000000..2d470367ef --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/AuthorRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.repositories; + +import org.springframework.data.repository.CrudRepository; + +import com.baeldung.models.Author; + +public interface AuthorRepository extends CrudRepository { + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/BookRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/BookRepository.java new file mode 100644 index 0000000000..f9176032ab --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/BookRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.repositories; + +import org.springframework.data.repository.CrudRepository; + +import com.baeldung.models.Book; + +public interface BookRepository extends CrudRepository { + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/LibraryRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/LibraryRepository.java new file mode 100644 index 0000000000..c00787f03c --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/LibraryRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.repositories; + +import org.springframework.data.repository.CrudRepository; + +import com.baeldung.models.Library; + +public interface LibraryRepository extends CrudRepository { + +} diff --git a/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java b/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java new file mode 100644 index 0000000000..ea2e70a4e4 --- /dev/null +++ b/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java @@ -0,0 +1,103 @@ +package com.baeldung.relationships; + +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.SpringDataRestApplication; +import com.baeldung.models.Address; +import com.baeldung.models.Author; +import com.baeldung.models.Book; +import com.baeldung.models.Library; + +import org.junit.Test; +import static org.junit.Assert.*; + +import org.json.JSONArray; +import org.json.JSONObject; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT) +public class SpringDataRelationshipsTest { + + @Autowired + private TestRestTemplate template; + + private static final String BOOK_ENDPOINT = "http://localhost:8080/books/"; + private static final String AUTHOR_ENDPOINT = "http://localhost:8080/authors/"; + private static final String ADDRESS_ENDPOINT = "http://localhost:8080/addresses/"; + private static final String LIBRARY_ENDPOINT = "http://localhost:8080/libraries/"; + + private static final String LIBRARY_NAME = "My Library"; + private static final String AUTHOR_NAME = "George Orwell"; + + @Test + public void whenSaveOneToOneRelationship_thenCorrect() { + Library library = new Library(LIBRARY_NAME); + template.postForEntity(LIBRARY_ENDPOINT, library, Library.class); + + Address address = new Address("Main street, nr 1"); + template.postForEntity(ADDRESS_ENDPOINT, address, Address.class); + + HttpHeaders requestHeaders = new HttpHeaders(); + requestHeaders.add("Content-type", "text/uri-list"); + HttpEntity httpEntity = new HttpEntity(ADDRESS_ENDPOINT + "/1", requestHeaders); + template.exchange(LIBRARY_ENDPOINT + "/1/libraryAddress", HttpMethod.PUT, httpEntity, String.class); + + ResponseEntity libraryGetResponse = template.getForEntity(ADDRESS_ENDPOINT + "/1/library", Library.class); + assertEquals("library is incorrect", libraryGetResponse.getBody() + .getName(), LIBRARY_NAME); + } + + @Test + public void whenSaveOneToManyRelationship_thenCorrect() { + Library library = new Library(LIBRARY_NAME); + template.postForEntity(LIBRARY_ENDPOINT, library, Library.class); + + Book book1 = new Book("Dune"); + template.postForEntity(BOOK_ENDPOINT, book1, Book.class); + + Book book2 = new Book("1984"); + template.postForEntity(BOOK_ENDPOINT, book2, Book.class); + + HttpHeaders requestHeaders = new HttpHeaders(); + requestHeaders.add("Content-type", "text/uri-list"); + HttpEntity bookHttpEntity = new HttpEntity(LIBRARY_ENDPOINT + "/1", requestHeaders); + template.exchange(BOOK_ENDPOINT + "/1/library", HttpMethod.PUT, bookHttpEntity, String.class); + template.exchange(BOOK_ENDPOINT + "/2/library", HttpMethod.PUT, bookHttpEntity, String.class); + + ResponseEntity libraryGetResponse = template.getForEntity(BOOK_ENDPOINT + "/1/library", Library.class); + assertEquals("library is incorrect", libraryGetResponse.getBody() + .getName(), LIBRARY_NAME); + } + + @Test + public void whenSaveManyToManyRelationship_thenCorrect() { + Author author1 = new Author(AUTHOR_NAME); + template.postForEntity(AUTHOR_ENDPOINT, author1, Author.class); + + Book book1 = new Book("Animal Farm"); + template.postForEntity(BOOK_ENDPOINT, book1, Book.class); + + Book book2 = new Book("1984"); + template.postForEntity(BOOK_ENDPOINT, book2, Book.class); + + HttpHeaders requestHeaders = new HttpHeaders(); + requestHeaders.add("Content-type", "text/uri-list"); + HttpEntity httpEntity = new HttpEntity(BOOK_ENDPOINT + "/1\n" + BOOK_ENDPOINT + "/2", requestHeaders); + template.exchange(AUTHOR_ENDPOINT + "/1/books", HttpMethod.PUT, httpEntity, String.class); + + String jsonResponse = template.getForObject(BOOK_ENDPOINT + "/1/authors", String.class); + JSONObject jsonObj = new JSONObject(jsonResponse).getJSONObject("_embedded"); + JSONArray jsonArray = jsonObj.getJSONArray("authors"); + assertEquals("author is incorrect", jsonArray.getJSONObject(0) + .getString("name"), AUTHOR_NAME); + } +}