How to handle JPA CollectionTable with inheritance?

I have to create a class hierarchy in which the superclass has a @CollectionTablerepresenting map. I tried to implement this, but it only works with one child class.

I want to do the following. I have the current structure on the left and the desired structure on the right. UML diagram of the structure

Stable (working) code is as follows:

@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";
}

This is how I tried to achieve the modification:

@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 creates the table pet_properties, but it only refers to dogsor cats. I was going to create a common table for the properties of both dogs and cats (and Persian cats).

What am I missing or doing wrong?

+5
source share
2

, @CollectionTable(name = "cat_properties", joinColumns = @JoinColumn(name = "cat_id")), Hibernate , cats_properties ( ), - dog_properties. , , , .

0

, , , ( , ). pet_properties Hibernate pet_properties ( ). , , . , , pet_properties dogs cats, , , , .

, , Hibernate ( dogs_properties cats_properties), .

0

Source: https://habr.com/ru/post/1657607/


All Articles