mixing joined and single table inheritance and querying for all objects

六眼飞鱼酱① 提交于 2019-12-01 10:31:17

问题


I have a webapp that is working with the following configuration (I changed the entity names):

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "animals")
public abstract class Animal { ...

@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class Mammal extends Animal { ...

@Entity
@Table(name = "mammals")
@PrimaryKeyJoinColumn(name = "mammal_id")
@DiscriminatorValue(value = "dog")
public class Dog extends Mammal { ...

@Entity
@Table(name = "mammals")
@PrimaryKeyJoinColumn(name = "mammal_id")
@DiscriminatorValue(value = "cat")
public class Cat extends Mammal { ...

So I have 2 tables: animals and mammals. This configuration is working, unless I don't completely understand how. The inheritance type is changed from JOINED to SINGLE_TABLE in Mammal, so cats and dogs are stored in mammals table joined with animals table by mammal_id column.

The problem is that is not possible to retrieve a mammal (cat or dog) in a polymorphic way:

Mammal mammal = (Mammal) hibernateTemplate.get(Mammal.class, id);

or a list of mammals (dogs and cats) with a single Hibernate query:

List<Mammal> list = (List<Mammal>) hibernateTemplate.find("from Mammal");

I get: "Unknown entity: Mammal", because Mammal is not an entity, it is just a mapped super class.

I tried several changes in configuration for example the one below. This seems to make sense, but in that case I got "Could not load entity: Mammal" because it looks for tables cats and dogs (which doesn't exist).

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "animals")
public abstract class Animal { ...

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "mammals")
@PrimaryKeyJoinColumn(name = "mammal_id")
@DiscriminatorColumn(name = "type")
public abstract class Mammal extends Animal { ...

@Entity
@DiscriminatorValue(value = "dog")
public class Dog extends Mammal { ...

@Entity
@DiscriminatorValue(value = "cat")
public class Cat extends Mammal { ...

So, to summarize: I want to keep the webapp working the same way (2 tables: animals and mammals) but at the same time be able to do a polymorphic query: retrieve a list of mammals (cats and dogs) with one query.

I hope I was clear enough and thanks in advance for any help.

ANOTHER TRY:

"Foreign key circularity dependency involving the following tables: "

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "animals")
public abstract class Animal { ...

@Entity
@Table(name = "mammals")
@PrimaryKeyJoinColumn(name = "mammal_id")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class Mammal extends Animal { ...

@Entity
@Table(name = "mammals")
@DiscriminatorValue(value = "dog")
public class Dog extends Mammal { ...

@Entity
@Table(name = "mammals")
@DiscriminatorValue(value = "cat")
public class Cat extends Mammal { ...

回答1:


Did you try hibernateTemplate.find("from Mammal") ?

Your find query needs to be in terms of JPA entities not table names.

BTW tables are typically named in the singular - it generally makes complex queries read a bit better - e.g. WHERE chicken.id = egg.id



来源:https://stackoverflow.com/questions/8494367/mixing-joined-and-single-table-inheritance-and-querying-for-all-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!