0

Imagine the following scenario where we use inheritance strategy TABLE_PER_CLASS and Template as superclass while Product as subclass.

Template:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name = "Templates")
@NamedQuery(name = "getAllTemplates", query = "SELECT t FROM Template t")
public class Template implements Serializable { ...}

Product:

@Entity
@Table(name = "Product")
public class Product extends Template  implements Serializable { ... }   

In this scenario even thought i have in my DB 2 templates and 1 product. Whenever i call the Template namedQuery i retrieve both Products and Templates alike.

I tried do something like so:

SELECT t FROM Template t WHERE TYPE(t) = Template

However it returns the following error: The class does not have a descriptor, or a descriptor that does not use inheritance or uses a ClassExtractor for inheritance

Is there a way to only get the Templates?

pirho
  • 11,565
  • 12
  • 43
  • 70
João Marques
  • 121
  • 1
  • 1
  • 14

1 Answers1

1

The TYPE operator does not work for sub classes either when using TABLE_PER_CLASS. There seems not to be explicit information about using TYPE with TABLE_PER_CLASS.

However there are lots of posts saying that this strategy is inefficient and not exactly recommended to be used.

JPA 2.1 specifications say about TABLE_PER_CLASS:

Support for the TABLE_PER_CLASS mapping strategy is optional in this release.

This means also that support might be only partial: like TYPE support not implemented.

Also there are some posts that indicate that not only Hibernate suffers from this, see (some pretty old, but still), like: this and this.

So as a conclusion:

  • change to SINGLE_TABLE or JOINED strategy if possible
  • live with it

In Hibernate you could also try to get it working with @DiscriminatorColumn and adding the column to Template entity but I personally think it is not worth the effort.

pirho
  • 11,565
  • 12
  • 43
  • 70