次のクラス構造があるとします。
@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Animal {}
@Entity
public class Dog {}
@Entity
public class Cat {}
Spring Data JPAでは、ジェネリックAnimalリポジトリを使用Animalして実行時に、その種類を知らなくても永続化できAnimalますか?
エンティティごとのリポジトリを使用して、次のように使用できることを知っていますinstanceof。
if (thisAnimal instanceof Dog)
dogRepository.save(thisAnimal);
else if (thisAnimal instanceof Cat)
catRepository.save(thisAnimal);
}
しかし、私は を使用するという悪い習慣に頼りたくありませんinstanceof。
次のような一般的なリポジトリを使用してみました。
public interface AnimalRepository extends JpaRepository<Animal, Long> {}
しかし、これにより、次の例外が発生します: Not an managed type: class Animal。Animalは ではなく、Entityであるからだと思いMappedSuperclassます。
最善の解決策は何ですか?
ところで -Animalに私のクラスの残りの部分がリストされているpersistence.xmlので、それは問題ではありません。