SpringDataJPAリポジトリポピュレータ
1. 序章
この簡単な記事では、SpringJPAリポジトリポピュレーターについて簡単な例を挙げて説明します。 Spring Data JPAリポジトリー・ポピュレーターは、data.sqlスクリプトの優れた代替手段です。
Spring Data JPAリポジトリポピュレータは、JSONおよびXMLファイル形式をサポートしています。 次のセクションでは、SpringDataJPAリポジトリポピュレータの使用方法を説明します。
2. サンプルアプリケーション
まず、 Fruit エンティティクラスと、データベースに入力するフルーツのインベントリがあるとします。
@Entity
public class Fruit {
@Id
private long id;
private String name;
private String color;
// getters and setters
}
JpaRepository を拡張して、データベースからFruitデータを読み取ります。
@Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
// ...
}
次のセクションでは、JSON形式を使用して、最初のフルーツデータを保存および入力します。
3. JSONリポジトリポピュレーター
Fruitデータを使用してJSONファイルを作成しましょう。 このファイルをsrc/ main / resources に作成し、fruit-data.jsonと呼びます。
[
{
"_class": "com.baeldung.entity.Fruit",
"name": "apple",
"color": "red",
"id": 1
},
{
"_class": "com.baeldung.entity.Fruit",
"name": "guava",
"color": "green",
"id": 2
}
]
エンティティクラス名は、各JSONオブジェクトの_classフィールドに指定する必要があります。残りのキーは、Fruitエンティティの列にマップされます。
次に、jackson-databind依存関係をpom.xmlに追加します。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
最後に、リポジトリポピュレータBeanを追加する必要があります。 このリポジトリポピュレーターBeanは、fruit-data.json ファイルからデータを読み取り、アプリケーションの起動時にデータベースにデータを入力します。
@Bean
public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() {
Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
factory.setResources(new Resource[]{new ClassPathResource("fruit-data.json")});
return factory;
}
これで、構成を単体テストする準備が整いました。
@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());
}
});
}
4. XMLリポジトリポピュレータ
このセクションでは、リポジトリポピュレータでXMLファイルを使用する方法を説明します。 まず、必要なFruitの詳細を含むXMLファイルを作成します。
ここで、XMLファイルは単一の果物のデータを表します。
apple-fruit-data.xml :
<fruit>
<id>1</id>
<name>apple</name>
<color>red</color>
</fruit>
guava-fruit-data.xml :
<fruit>
<id>2</id>
<name>guava</name>
<color>green</color>
</fruit>
ここでも、これらのXMLファイルを src / main / resources。に保存しています。
また、 spring-oxmmaven依存関係をpom.xmlに追加します。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
さらに、エンティティクラスに@XmlRootElementアノテーションを追加する必要があります。
@XmlRootElement
@Entity
public class Fruit {
// ...
}
最後に、リポジトリポピュレータBeanを定義します。 このBeanは、XMLファイルを読み取り、データを入力します。
@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;
}
JSONポピュレーターの場合と同じように、XMLリポジトリポピュレーターの単体テストを行うことができます。
4. 結論
このチュートリアルでは、 SpringDataJPAリポジトリーポピュレーターの使用方法を学びました。 このチュートリアルで使用される完全なソースコードは、GitHubでから入手できます。