는 다음 클래스 구조를 감안할 때 : 스프링 데이터 JPA으로스프링 데이터 JPA를 사용하여 MappedSuperClass의 모든 하위 항목에 일반 저장소를 사용할 수 있습니까?
@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Animal {}
@Entity
public class Dog {}
@Entity
public class Cat {}
을, 그것이이다 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
입니다.
최상의 솔루션은 무엇입니까?
BTW - Animal
은 내 수업 외 나머지 부분과 함께 persistence.xml
에 나열되어 있으므로 문제가되지 않습니다.
'Animal'은'persistence.xml' 내 영속성 단위에 클래스로 나열되어야합니까? 제안 된 변경으로 인해 'EntityManagerFactory를 빌드 할 수 없습니다'라는 새로운 예외가 발생합니다. –
@CFL_Jeff : 일반적으로 주석에만 의존하므로 확실하지 않습니다. 'Caused by '를 포함한 어딘가에 전체 스택 추적을 게시 할 수 있습니까? –
마치 다른 문제가있는 것 같습니다. 나는 당신이 내가 당면한 문제를 해결할 수 있도록 도와 주었다고 믿으며, 나는 새로운 문제에 대해 연구 할 것이다. 감사! –