私は spring-data-jpa プロジェクトを試していて、いくつかの任意の API を実装し、JPA エンティティ定義を実装クラスに入れました。(1 つの) リポジトリは次のように定義されます。
@Repository
public interface RecipeRepository extends JpaRepository<RecipeEntity, Long>
私のエンティティクラスは次のように定義されています
@Entity
@Table(name = "recipes")
public class RecipeEntity implements Recipe
だから私はこのようなレシピを構築しています:
Recipe pasta = Kitchen.createRecipe("Pasta");
確かに、次のようにレシピを単純に永続化することはできません。
recipeRepository.save(pasta);
したがって、次のようにリポジトリを定義できればいいのですが。
@Repository(targetEntity=RecipeEntity.class)
public interface RecipeRepository extends JpaRepository<Recipe, Long>
RecipeEntity から関連するエンティティを参照するのと同じように、たとえば材料は次のようになります。
@OneToMany(targetEntity=IngredientEntity.class)
private List<Ingredient> ingredients;