diff --git a/spring-data-javaslang/.gitignore b/spring-data-javaslang/.gitignore new file mode 100644 index 0000000000..7ee5423d14 --- /dev/null +++ b/spring-data-javaslang/.gitignore @@ -0,0 +1,5 @@ +/target/ +/project/ +.idea +.classpath +.eclipse diff --git a/spring-data-javaslang/pom.xml b/spring-data-javaslang/pom.xml new file mode 100644 index 0000000000..c265e893cc --- /dev/null +++ b/spring-data-javaslang/pom.xml @@ -0,0 +1,133 @@ + + + 4.0.0 + spring-data-javaslangb + spring-data-javaslangb + 0.0.1-SNAPSHOT + + UTF-8 + 2.19.1 + 3.1 + 2.9.1-01 + 2.3.7-01 + 2.4.8 + 2.0.5 + 4.12 + 1.5.1.RELEASE + 4.3.6.RELEASE + 4.3.6.RELEASE + 4.3.6.RELEASE + ${basedir}/src/test/java + + + org.springframework.boot + spring-boot-starter-parent + 1.5.1.RELEASE + + + + test-app + + verify + + + org.springframework.boot + spring-boot-maven-plugin + + + spring-boot-run + verify + + run + + false + + + + + + + + + + + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + ${project.build.testSourceDirectory} + + + + + + + org.springframework.boot + spring-boot-devtools + true + + + + com.h2database + h2 + 1.4.193 + + + + io.javaslang + javaslang + ${javaslang.version} + + + + org.springframework.data + spring-data-jpa + 1.11.0.RELEASE + + + + org.springframework.boot + spring-boot + ${spring-boot.version} + + + + org.springframework.boot + spring-boot-starter-data-jpa + ${spring-boot.version} + + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot.version} + + + + org.springframework + spring-context + ${spring-context.version} + + + + org.springframework + spring-core + ${spring-core.version} + + + + junit + junit + ${junit.version} + test + + + \ No newline at end of file diff --git a/spring-data-javaslang/src/main/java/com/baeldung/spring_data/model/Book.java b/spring-data-javaslang/src/main/java/com/baeldung/spring_data/model/Book.java new file mode 100644 index 0000000000..95653abb6c --- /dev/null +++ b/spring-data-javaslang/src/main/java/com/baeldung/spring_data/model/Book.java @@ -0,0 +1,45 @@ +package com.baeldung.spring_data.model; + +import javaslang.collection.Seq; + +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.CollectionTable; +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; + +@Entity +@Table(name = "book") +public class Book { + + @GeneratedValue + @Id + private Long id; + + private String title; + + private Seq authors; + + + public void setTitle(String title){ + this.title = title; + } + + public String getTitle(){ + return this.title; + } + + public Long getId(){ + return this.id; + } + + public void setAuthors(Seq authors){ + this.authors = authors; + } + + public Seq getAuthors(){ + return this.authors; + } +} diff --git a/spring-data-javaslang/src/main/java/com/baeldung/spring_data/model/JavaBook.java b/spring-data-javaslang/src/main/java/com/baeldung/spring_data/model/JavaBook.java new file mode 100644 index 0000000000..ab99b0d929 --- /dev/null +++ b/spring-data-javaslang/src/main/java/com/baeldung/spring_data/model/JavaBook.java @@ -0,0 +1,41 @@ +package com.baeldung.spring_data.model; + +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +import java.util.List; + +@Entity +@Table(name = "java_book") +public class JavaBook { + + @GeneratedValue + @Id + private Long id; + + private String title; + + @ElementCollection + private List authors; + + + public void setAuthors(List authors){ + this.authors = authors; + } + + public void setTitle(String title){ + this.title = title; + } + + public String getTitle(){ + return this.title; + } + + public Long getId(){ + return this.id; + } +} + diff --git a/spring-data-javaslang/src/main/java/com/baeldung/spring_data/repository/BookRepository.java b/spring-data-javaslang/src/main/java/com/baeldung/spring_data/repository/BookRepository.java new file mode 100644 index 0000000000..75b6d0b426 --- /dev/null +++ b/spring-data-javaslang/src/main/java/com/baeldung/spring_data/repository/BookRepository.java @@ -0,0 +1,19 @@ +package com.baeldung.spring_data.repository; + +import com.baeldung.spring_data.model.Book; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import javaslang.collection.Seq; +import javaslang.control.Option; + +@Repository +public interface BookRepository extends JpaRepository{ + Book save(Book book); + + Option findById(Long id); + + Option> findByTitleContaining(String title); + +} diff --git a/spring-data-javaslang/src/main/java/com/baeldung/spring_data/repository/JavaBookRepository.java b/spring-data-javaslang/src/main/java/com/baeldung/spring_data/repository/JavaBookRepository.java new file mode 100644 index 0000000000..a4aeab85ee --- /dev/null +++ b/spring-data-javaslang/src/main/java/com/baeldung/spring_data/repository/JavaBookRepository.java @@ -0,0 +1,17 @@ +package com.baeldung.spring_data.repository; + +import com.baeldung.spring_data.model.JavaBook; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface JavaBookRepository extends JpaRepository{ + JavaBook save(JavaBook book); + + JavaBook findById(Long id); + + List findByTitleContaining(String title); +} \ No newline at end of file diff --git a/spring-data-javaslang/src/main/java/com/baeldung/spring_data_app/MainApp.java b/spring-data-javaslang/src/main/java/com/baeldung/spring_data_app/MainApp.java new file mode 100644 index 0000000000..d8a194e92e --- /dev/null +++ b/spring-data-javaslang/src/main/java/com/baeldung/spring_data_app/MainApp.java @@ -0,0 +1,21 @@ +package com.baeldung.spring_data_app; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@EnableJpaRepositories("com.baeldung.spring_data.repository") +@EnableTransactionManagement +@EntityScan("com.baeldung.spring_data.model") +@SpringBootApplication +public class MainApp { + public static void main(String[] args){ + SpringApplication.run(MainApp.class, args); + } +} diff --git a/spring-data-javaslang/src/test/java/com/baeldung/spring_data_tests/SpringTests.java b/spring-data-javaslang/src/test/java/com/baeldung/spring_data_tests/SpringTests.java new file mode 100644 index 0000000000..59a6c120fa --- /dev/null +++ b/spring-data-javaslang/src/test/java/com/baeldung/spring_data_tests/SpringTests.java @@ -0,0 +1,86 @@ +package com.baeldung.spring_data_tests; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.baeldung.spring_data_app.MainApp; +import com.baeldung.spring_data.model.Book; +import com.baeldung.spring_data.model.JavaBook; +import com.baeldung.spring_data.repository.BookRepository; +import com.baeldung.spring_data.repository.JavaBookRepository; + +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.test.context.junit4.SpringRunner; + +import javaslang.collection.Seq; +import javaslang.collection.List; +import javaslang.control.Option; + +import java.util.ArrayList; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = MainApp.class,webEnvironment = WebEnvironment.NONE) +public class SpringTests { + + @Autowired + JavaBookRepository javaRepository; + + @Autowired + BookRepository repository; + + @Test + public void should_return_seq(){ + Seq authors = List.of("author1","author2"); + Book testBook = new Book(); + testBook.setTitle("Javaslang in Spring Data Seq Test Return"); + testBook.setAuthors(authors); + Book book = repository.save(testBook); + Option> books = repository.findByTitleContaining("Seq Test"); + assert(!books.isEmpty()); + } + + + @Test + public void should_return_option_with_book(){ + Seq authors = List.of("author1","author2"); + Book testBook = new Book(); + testBook.setTitle("Javaslang in Spring Data"); + testBook.setAuthors(authors); + Book book = repository.save(testBook); + Option retBook = repository.findById(1L); + assert(retBook.isDefined() && !retBook.isEmpty()); + assert(retBook.get() != null); + } + + @Test + public void should_return_list(){ + ArrayList authors = new ArrayList(); + authors.add("author1"); + authors.add("author2"); + JavaBook testBook = new JavaBook(); + testBook.setTitle("Javaslang in Spring Data Seq Return"); + testBook.setAuthors(authors); + JavaBook book = javaRepository.save(testBook); + java.util.List books = javaRepository.findByTitleContaining("Seq"); + assert(!books.isEmpty()); + assert(books.size() == 1); + assert(books.get(0).getTitle().equals("Javaslang in Spring Data Seq Return")); + } + + @Test + public void should_return_book(){ + ArrayList authors = new ArrayList(); + authors.add("author1"); + authors.add("author2"); + JavaBook testBook = new JavaBook(); + testBook.setTitle("Javaslang in Spring Data"); + testBook.setAuthors(authors); + JavaBook book = javaRepository.save(testBook); + JavaBook retBook = javaRepository.findById(1L); + assert(retBook != null); + assert(retBook.getId() == 1L); + assert(retBook.getTitle().contains("Data")); + } +} \ No newline at end of file