From 3210112a451ca39ed997a4d418f5508e8a666126 Mon Sep 17 00:00:00 2001 From: abialas Date: Wed, 13 Dec 2017 23:15:59 +0100 Subject: [PATCH] BAEL-1304 add files --- .../spring-data-eclipselink/pom.xml | 79 +++++++++++++++++++ .../EclipselinkSpringDataApplication.java | 12 +++ .../springdata/JpaConfiguration.java | 44 +++++++++++ .../eclipselink/springdata/model/Person.java | 43 ++++++++++ .../springdata/repo/PersonsRepository.java | 14 ++++ .../src/main/resources/application.properties | 2 + .../repo/PersonsRepositoryTest.java | 59 ++++++++++++++ 7 files changed, 253 insertions(+) create mode 100644 persistence-modules/spring-data-eclipselink/pom.xml create mode 100644 persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/EclipselinkSpringDataApplication.java create mode 100644 persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/JpaConfiguration.java create mode 100644 persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/model/Person.java create mode 100644 persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/repo/PersonsRepository.java create mode 100644 persistence-modules/spring-data-eclipselink/src/main/resources/application.properties create mode 100644 persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/repo/PersonsRepositoryTest.java diff --git a/persistence-modules/spring-data-eclipselink/pom.xml b/persistence-modules/spring-data-eclipselink/pom.xml new file mode 100644 index 0000000000..147901dc90 --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/pom.xml @@ -0,0 +1,79 @@ + + + 4.0.0 + + com.baeldung + spring-data-eclipselink + 1.0.0-SNAPSHOT + + spring-data-eclipselink + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + UTF-8 + UTF-8 + 1.8 + 1.5.9.RELEASE + 2.7.0 + 1.4.196 + + + + + org.springframework.boot + spring-boot-dependencies + ${spring.version} + pom + import + + + org.springframework.boot + spring-boot-starter-data-jpa + ${spring.version} + + + org.hibernate + hibernate-entitymanager + + + org.hibernate + hibernate-core + + + + + org.springframework.boot + spring-boot-starter-test + ${spring.version} + test + + + org.eclipse.persistence + org.eclipse.persistence.jpa + ${eclipselink.version} + + + com.h2database + h2 + runtime + ${h2.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/EclipselinkSpringDataApplication.java b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/EclipselinkSpringDataApplication.java new file mode 100644 index 0000000000..63ce778022 --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/EclipselinkSpringDataApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.eclipselink.springdata; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class EclipselinkSpringDataApplication { + + public static void main(String[] args) { + SpringApplication.run(EclipselinkSpringDataApplication.class, args); + } +} diff --git a/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/JpaConfiguration.java b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/JpaConfiguration.java new file mode 100644 index 0000000000..60ff7909be --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/JpaConfiguration.java @@ -0,0 +1,44 @@ +package com.baeldung.eclipselink.springdata; + +import org.eclipse.persistence.config.PersistenceUnitProperties; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration; +import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; +import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers; +import org.springframework.context.annotation.Configuration; +import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver; +import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter; +import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter; +import org.springframework.transaction.jta.JtaTransactionManager; + +import javax.sql.DataSource; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by adam. + */ +@Configuration +public class JpaConfiguration extends JpaBaseConfiguration { + + protected JpaConfiguration(DataSource dataSource, JpaProperties properties, ObjectProvider jtaTransactionManager, ObjectProvider transactionManagerCustomizers) { + super(dataSource, properties, jtaTransactionManager, transactionManagerCustomizers); + } + + @Override + protected AbstractJpaVendorAdapter createJpaVendorAdapter() { + return new EclipseLinkJpaVendorAdapter(); + } + + @Override + protected Map getVendorProperties() { + HashMap map = new HashMap<>(); + map.put(PersistenceUnitProperties.WEAVING, detectWeavingMode()); + map.put(PersistenceUnitProperties.DDL_GENERATION, "drop-and-create-tables"); + return map; + } + + private String detectWeavingMode() { + return InstrumentationLoadTimeWeaver.isInstrumentationAvailable() ? "true" : "static"; + } +} diff --git a/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/model/Person.java b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/model/Person.java new file mode 100644 index 0000000000..75161875bd --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/model/Person.java @@ -0,0 +1,43 @@ +package com.baeldung.eclipselink.springdata.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +/** + * Created by adam. + */ +@Entity +public class Person { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private Long id; + private String firstName; + private String lastName; + + 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; + } +} diff --git a/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/repo/PersonsRepository.java b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/repo/PersonsRepository.java new file mode 100644 index 0000000000..86cd85d5fe --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/main/java/com/baeldung/eclipselink/springdata/repo/PersonsRepository.java @@ -0,0 +1,14 @@ +package com.baeldung.eclipselink.springdata.repo; + +import org.springframework.data.repository.CrudRepository; + +import com.baeldung.eclipselink.springdata.model.Person; + +/** + * Created by adam. + */ +public interface PersonsRepository extends CrudRepository { + + Person findByFirstName(String firstName); + +} diff --git a/persistence-modules/spring-data-eclipselink/src/main/resources/application.properties b/persistence-modules/spring-data-eclipselink/src/main/resources/application.properties new file mode 100644 index 0000000000..549c0b4ada --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.datasource.url=jdbc:h2:mem:test +spring.jpa.show-sql=true \ No newline at end of file diff --git a/persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/repo/PersonsRepositoryTest.java b/persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/repo/PersonsRepositoryTest.java new file mode 100644 index 0000000000..4c24df9ee7 --- /dev/null +++ b/persistence-modules/spring-data-eclipselink/src/test/java/com/baeldung/eclipselink/springdata/repo/PersonsRepositoryTest.java @@ -0,0 +1,59 @@ +package com.baeldung.eclipselink.springdata.repo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.eclipselink.springdata.model.Person; +import com.baeldung.eclipselink.springdata.repo.PersonsRepository; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNull.notNullValue; + +/** + * Created by adam. + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) +public class PersonsRepositoryTest { + + @Autowired + private PersonsRepository personsRepository; + + @Test + public void shouldAddOnePersonToDB() { + // when + personsRepository.save(new Person()); + + // then + assertThat(personsRepository.findAll() + .spliterator() + .getExactSizeIfKnown(), equalTo(1l)); + } + + @Test + public void shouldFindOnePersonWithNameAdam() { + // given + Person person1 = new Person(); + person1.setFirstName("Adam"); + + Person person2 = new Person(); + person2.setFirstName("Dave"); + + personsRepository.save(person1); + personsRepository.save(person2); + + // when + Person foundPerson = personsRepository.findByFirstName("Adam"); + + // then + assertThat(foundPerson.getFirstName(), equalTo("Adam")); + assertThat(foundPerson.getId(), notNullValue()); + } + +}