BAEL-1304 add files
This commit is contained in:
+12
@@ -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);
|
||||
}
|
||||
}
|
||||
+44
@@ -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> jtaTransactionManager, ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
|
||||
super(dataSource, properties, jtaTransactionManager, transactionManagerCustomizers);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
|
||||
return new EclipseLinkJpaVendorAdapter();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> getVendorProperties() {
|
||||
HashMap<String, Object> 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";
|
||||
}
|
||||
}
|
||||
+43
@@ -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;
|
||||
}
|
||||
}
|
||||
+14
@@ -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, Long> {
|
||||
|
||||
Person findByFirstName(String firstName);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
spring.datasource.url=jdbc:h2:mem:test
|
||||
spring.jpa.show-sql=true
|
||||
+59
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user