Skip to content

Commit a5d630b

Browse files
authored
Merge pull request eugenp#6695 from sumitsg34/master
BAEL-2841 spring data jpa populators
2 parents e164ab8 + 029ab02 commit a5d630b

File tree

7 files changed

+121
-7
lines changed

7 files changed

+121
-7
lines changed
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
6-
<groupId>com.baeldung</groupId>
6+
7+
<groupId>com.baeldung</groupId>
78
<artifactId>spring-data-jpa-2</artifactId>
8-
<name>spring-data-jpa</name>
9-
9+
<name>spring-data-jpa</name>
10+
1011
<parent>
1112
<artifactId>parent-boot-2</artifactId>
1213
<groupId>com.baeldung</groupId>
@@ -19,12 +20,22 @@
1920
<groupId>org.springframework.boot</groupId>
2021
<artifactId>spring-boot-starter-data-jpa</artifactId>
2122
</dependency>
22-
23+
2324
<dependency>
2425
<groupId>com.h2database</groupId>
2526
<artifactId>h2</artifactId>
2627
</dependency>
27-
28+
29+
<dependency>
30+
<groupId>com.fasterxml.jackson.core</groupId>
31+
<artifactId>jackson-databind</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework</groupId>
36+
<artifactId>spring-oxm</artifactId>
37+
</dependency>
38+
2839
</dependencies>
2940

3041
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.core.io.ClassPathResource;
6+
import org.springframework.core.io.Resource;
7+
import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean;
8+
import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean;
9+
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
10+
11+
import com.baeldung.entity.Fruit;
12+
13+
@Configuration
14+
public class JpaPopulators {
15+
16+
@Bean
17+
public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception {
18+
Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
19+
factory.setResources(new Resource[] { new ClassPathResource("fruit-data.json") });
20+
return factory;
21+
}
22+
23+
@Bean
24+
public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() {
25+
26+
Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
27+
unmarshaller.setClassesToBeBound(Fruit.class);
28+
29+
UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean();
30+
factory.setUnmarshaller(unmarshaller);
31+
factory.setResources(new Resource[] { new ClassPathResource("apple-fruit-data.xml"), new ClassPathResource("guava-fruit-data.xml") });
32+
return factory;
33+
}
34+
35+
}

persistence-modules/spring-data-jpa-2/src/main/java/com/baeldung/entity/Fruit.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import javax.persistence.Entity;
44
import javax.persistence.Id;
5+
import javax.xml.bind.annotation.XmlRootElement;
56

7+
@XmlRootElement
68
@Entity
79
public class Fruit {
810

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<fruit>
4+
<id>1</id>
5+
<name>apple</name>
6+
<color>red</color>
7+
</fruit>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"_class": "com.baeldung.entity.Fruit",
4+
"name": "apple",
5+
"color": "red",
6+
"id": 1
7+
},
8+
{
9+
"_class": "com.baeldung.entity.Fruit",
10+
"name": "guava",
11+
"color": "green",
12+
"id": 2
13+
}
14+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<fruit>
4+
<id>2</id>
5+
<name>guava</name>
6+
<color>green</color>
7+
</fruit>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.repository;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.List;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
import com.baeldung.entity.Fruit;
14+
15+
@RunWith(SpringRunner.class)
16+
@SpringBootTest
17+
public class FruitPopulatorTest {
18+
19+
@Autowired
20+
private FruitRepository fruitRepository;
21+
22+
@Test
23+
public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() {
24+
25+
List<Fruit> fruits = fruitRepository.findAll();
26+
assertEquals("record count is not matching", 2, fruits.size());
27+
28+
fruits.forEach(fruit -> {
29+
if (1 == fruit.getId()) {
30+
assertEquals("apple", fruit.getName());
31+
assertEquals("red", fruit.getColor());
32+
} else if (2 == fruit.getId()) {
33+
assertEquals("guava", fruit.getName());
34+
assertEquals("green", fruit.getColor());
35+
}
36+
});
37+
}
38+
}

0 commit comments

Comments
 (0)