2

I have the following set-up

@Entity
@Table(name = "FOO")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "FOO_TYPE", discriminatorType = DiscriminatorType.STRING,     length = 20)
@DiscriminatorValue("FOO")
public class Foo implements Serializable {

  private static final long serialVersionUID = 1L;
  @Id
  @Column(name = "ID")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @Column(name = "foo_column")
  private String fooColumn;

  ... getters and setters ...

}


@Entity
@Table(name = "BAR")
@PrimaryKeyJoinColumn(name = "BAR_ID", referencedColumnName = "ID")
@DiscriminatorValue("Bar")
public class Bar extends Foo {

  @Column(name = "bar_column")
  private String barColumn;

  ... getters and setters ...

}

When I execute the following query in Hibernate 3.6 as JPA provider, everything works as expected(well... as I expect at least), and the result list is populated with all kinds of objects which extend the Foo class:

List<Foo> fooList = em.createQuery("SELECT foo FROM Foo foo WHERE 1=1 AND foo.fooColumn = 'foo' and foo.barColumn = 'bar'");

Notice that I am filtering by barField on the Foo object - and Hibernate is able to sort this out by filtering and populating the appropriate object.

However, when executing the same query using EclipseLink, I recieve the following error:

...
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [SELECT foo FROM Foo foo WHERE 1=1 AND foo.fooColumn = 'foo' and foo.barColumn = 'bar']
[66, 79] The state field path 'foo.barColumn' cannot be resolved to a valid type.
...

Note: I have read the documentation of EclipseLink and I added a @DiscriminatorColumn and @DiscriminatorValue to the tables specifically.

Then I changed the query to the following - using the TREAT function:

List<Foo> fooList = em.createQuery("SELECT foo FROM Foo foo WHERE 1=1 AND foo.fooColumn = 'foo' and treat(foo as Bar).barColumn = 'bar'");

which now works (under JPA 2.1).

Is it possible to achieve this filtering on child entity without resorting to JPA 2.1 functionality?

Nikola Kolev
  • 1,239
  • 3
  • 17
  • 25
  • Yes, you would just query on the child entity instead of the parent. – Chris Aug 19 '14 at 15:46
  • Can you do a test with the TREAT function? Something like: `SELECT b.name, b.ISBN FROM Order o JOIN TREAT(o.product AS Book) b` – uaiHebert Aug 19 '14 at 16:36

0 Answers0