Note that there are some explanatory texts on larger screens.

plurals
  1. POmixing joined and single table inheritance and querying for all objects
    primarykey
    data
    text
    <p>I have a webapp that is working with the following configuration (I changed the entity names):</p> <pre><code>@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 { ... </code></pre> <p>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.</p> <p>The problem is that is not possible to retrieve a mammal (cat or dog) in a polymorphic way:</p> <pre><code>Mammal mammal = (Mammal) hibernateTemplate.get(Mammal.class, id); </code></pre> <p>or a list of mammals (dogs and cats) with a single Hibernate query:</p> <pre><code>List&lt;Mammal&gt; list = (List&lt;Mammal&gt;) hibernateTemplate.find("from Mammal"); </code></pre> <p>I get: "Unknown entity: Mammal", because Mammal is not an entity, it is just a mapped super class.</p> <p>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).</p> <pre><code>@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 { ... </code></pre> <p>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.</p> <p>I hope I was clear enough and thanks in advance for any help.</p> <p>ANOTHER TRY:</p> <p>"Foreign key circularity dependency involving the following tables: "</p> <pre><code>@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 { ... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload