수퍼 클래스에지도를 나타내는 @CollectionTable
이있는 클래스 계층 구조를 만들어야합니다. 구현하려고했지만 하나의 자식 클래스에서만 작동합니다.상속을 사용하여 JPA CollectionTable을 처리하는 방법은 무엇입니까?
다음과 같이하고 싶습니다. 왼쪽에 현재 구조가 있고 오른쪽에 원하는 구조가 있습니다. 는
안정은 (작업) 코드는 다음과 같습니다
@MappedSuperclass
public class Animal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
}
@MappedSuperclass
public abstract class Pet extends Animal {
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "name")
@Column(name = "value")
@CollectionTable(name = "pet_properties", joinColumns = @JoinColumn(name = "pet_id"))
private Map<String, String> properties = new HashMap<>();
}
@Entity(name = "cats")
@Audited
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.CHAR)
@DiscriminatorValue(value = Cat.PET_TYPE)
public class Cat extends Pet {
public static final String PET_TYPE = "C";
}
@Entity(name = "dogs")
@Audited
public class Dog extends Pet {
}
@Audited
@Entity(name = "persiancats")
@DiscriminatorValue(value = PersianCat.PERSIAN_CAT_TYPE)
public class PersianCat extends Pet {
public static final String PERSIAN_CAT_TYPE = "P";
}
Hibernate가 pet_properties
테이블을 생성하지만, 참조 만 중 하나
@MappedSuperclass
public class Animal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
}
@Entity(name = "cats")
@Audited
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.CHAR)
@DiscriminatorValue(value = Cat.PET_TYPE)
public abstract class Cat extends Animal {
public static final String PET_TYPE = "C";
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "name")
@Column(name = "value")
@CollectionTable(name = "cat_properties", joinColumns = @JoinColumn(name = "cat_id"))
private Map<String, String> properties = new HashMap<>();
}
@Audited
@Entity(name = "persiancats")
@DiscriminatorValue(value = PersianCat.PERSIAN_CAT_TYPE)
public class PersianCat extends Cat {
public static final String PERSIAN_CAT_TYPE = "P";
}
이 내가 수정을 달성하기 위해 시도하는 방법이다 dogs
또는 cats
내 의도는 개와 고양이 (및 persiancat) 속성 모두에 공통 테이블을 작성하는 것이 었습니다.
무엇이 잘못되었거나 잘못 되었습니까?
꽤 많은 문제가 있습니다. 다른 해결책을 찾았습니까? 테이블을 10 개 이상 만들 수는 없습니다. – ianaz