Merge pull request #6695 from sumitsg34/master
BAEL-2841 spring data jpa populators
This commit is contained in:
commit
a5d630b175
@ -1,12 +1,13 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-data-jpa-2</artifactId>
|
<artifactId>spring-data-jpa-2</artifactId>
|
||||||
<name>spring-data-jpa</name>
|
<name>spring-data-jpa</name>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>parent-boot-2</artifactId>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
@ -19,12 +20,22 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.h2database</groupId>
|
<groupId>com.h2database</groupId>
|
||||||
<artifactId>h2</artifactId>
|
<artifactId>h2</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-oxm</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean;
|
||||||
|
import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean;
|
||||||
|
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
||||||
|
|
||||||
|
import com.baeldung.entity.Fruit;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class JpaPopulators {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception {
|
||||||
|
Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
|
||||||
|
factory.setResources(new Resource[] { new ClassPathResource("fruit-data.json") });
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() {
|
||||||
|
|
||||||
|
Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
|
||||||
|
unmarshaller.setClassesToBeBound(Fruit.class);
|
||||||
|
|
||||||
|
UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean();
|
||||||
|
factory.setUnmarshaller(unmarshaller);
|
||||||
|
factory.setResources(new Resource[] { new ClassPathResource("apple-fruit-data.xml"), new ClassPathResource("guava-fruit-data.xml") });
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,7 +2,9 @@ package com.baeldung.entity;
|
|||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement
|
||||||
@Entity
|
@Entity
|
||||||
public class Fruit {
|
public class Fruit {
|
||||||
|
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<fruit>
|
||||||
|
<id>1</id>
|
||||||
|
<name>apple</name>
|
||||||
|
<color>red</color>
|
||||||
|
</fruit>
|
@ -0,0 +1,14 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"_class": "com.baeldung.entity.Fruit",
|
||||||
|
"name": "apple",
|
||||||
|
"color": "red",
|
||||||
|
"id": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_class": "com.baeldung.entity.Fruit",
|
||||||
|
"name": "guava",
|
||||||
|
"color": "green",
|
||||||
|
"id": 2
|
||||||
|
}
|
||||||
|
]
|
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<fruit>
|
||||||
|
<id>2</id>
|
||||||
|
<name>guava</name>
|
||||||
|
<color>green</color>
|
||||||
|
</fruit>
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.repository;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
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.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.baeldung.entity.Fruit;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class FruitPopulatorTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FruitRepository fruitRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() {
|
||||||
|
|
||||||
|
List<Fruit> fruits = fruitRepository.findAll();
|
||||||
|
assertEquals("record count is not matching", 2, fruits.size());
|
||||||
|
|
||||||
|
fruits.forEach(fruit -> {
|
||||||
|
if (1 == fruit.getId()) {
|
||||||
|
assertEquals("apple", fruit.getName());
|
||||||
|
assertEquals("red", fruit.getColor());
|
||||||
|
} else if (2 == fruit.getId()) {
|
||||||
|
assertEquals("guava", fruit.getName());
|
||||||
|
assertEquals("green", fruit.getColor());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user