3

Queries in the form of an entity class A in the form of

SELECT a from A a WHERE TYPE(a) = A

fail with

could not resolve property: class of: de.richtercloud.type.operator.nonsense.A [SELECT a from de.richtercloud.type.operator.nonsense.A a WHERE TYPE(a) = A]

which I don't understand because restricting A to A should exclude all superclass instances which aren't As as well as subclass instances.

Example:

package de.richtercloud.type.operator.nonsense;

import java.io.File;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 * Illustrates the problem/misunderstanding that storing an {@link A} and
 * querying it with a {@code WHERE TYPE([identifier]) = A} fails with
 * {@code org.hibernate.QueryException: could not resolve property:
 * class of: de.richtercloud.type.operator.nonsense.A}.
 * @author richter
 */
public class NewMain {
    private final static File DATABASE_DIR = new File("/tmp/type-operator-nonsense");
    private final static String DERBY_CONNECTION_URL = String.format("jdbc:derby:%s", DATABASE_DIR.getAbsolutePath());

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException {
        //setup database
        EntityManagerFactory entityManagerFactory = null;
        try {
            Map<Object, Object> entityManagerFactoryMap = new HashMap<>();
            entityManagerFactoryMap.put("javax.persistence.jdbc.url",
                    String.format("%s;create=%s", DERBY_CONNECTION_URL, !DATABASE_DIR.exists()));
            entityManagerFactory = Persistence.createEntityManagerFactory("type-operator-nonsense",
                    entityManagerFactoryMap);

            //show issue
            EntityManager entityManager = entityManagerFactory.createEntityManager();
            A a = new A(1L, "b");
            entityManager.getTransaction().begin();
            entityManager.persist(a);
            entityManager.flush();
            Query query = entityManager.createQuery("SELECT a from A a WHERE TYPE(a) = A");
            List<?> queryResult = query.getResultList();
            entityManager.getTransaction().commit();
            System.out.println(queryResult.size());
        }finally {
            if(entityManagerFactory != null) {
                entityManagerFactory.close();
            }
        }
    }
}

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="type-operator-nonsense" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>de.richtercloud.type.operator.nonsense.A</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby:/tmp/type-operator-nonsense"/>
      <property name="javax.persistence.jdbc.user" value=""/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>

with org.hibernate:hibernate-entitymanager:5.1.0.Final and org.apache.derby:derby:10.11.1.1

A is a POJO in this example which doesn't involve inheritance, that shouldn't matter for the restriction I expect (although it wouldn't make sense to apply it if there's no inheritance):

package de.richtercloud.type.operator.nonsense;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class A implements Serializable {
    private static final long serialVersionUID = 1L;
    private String b;
    @Id
    private Long id;

    public A() {
    }

    public A(Long id, String b) {
        this.id = id;
        this.b = b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getB() {
        return b;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
  • I just explored with this problem, and found few wanted to share with you. **Can you give a try with .class instead of Type() "SELECT a from A a WHERE a.class = A" Or Create a repository for the entity class A and access with your actual query with Type -SELECT a from A a WHERE TYPE(a) = A** – Shaan Mar 28 '16 at 19:23
  • `SELECT a from A a WHERE a.class = A` fails with the exact same error which indicates that this might be hibernate bug or nonsense feedback. What do you mean by "repository" - a `jar` dependency - speaking maven terms? If yes, why should that make any difference? – Kalle Richter Mar 28 '16 at 20:36
  • I mean JPA repsitory and .class notation for hibernate. Please refer the link might be useful to you http://stackoverflow.com/questions/4884249/java-jpa-query-with-specified-inherited-type – Shaan Mar 28 '16 at 21:28
  • When I look at your example on GitHub - shouldn't @Inheritance be on A? Because B has no sub classes and therefor the annotation doesn't make sense. Have a look at my project example below. – Simon Martinelli Mar 31 '16 at 18:43

2 Answers2

2

This behaviour occurs due to a hibernate bug https://hibernate.atlassian.net/browse/HHH-10653. The example above works fine with OpenJPA.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
0

TYPE() can only be used in inheritance mapping.

Is A a class in a inheritance mapping and A either the route class with @Inheritance or A extends a class that has @Inheritance declared?

Example.

@Inheritance
@Entity
public class Project

@Entity
public class DesignProject extends Project

@Entity
public class QualityProject extends Project

@Entity
public class SoftwareProject extends Project

Now you can use TYPE()

SELECT p
FROM Project p
WHERE TYPE(p) = DesignProject OR TYPE(p) = QualityProject
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • 1
    Can you add a reference or elaborate. Why isn't this mentioned in the [JPA 2.1 specification](http://download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf?AuthParam=1459196694_4386929d6f1aa92ffdf995cd502e4d75)? And you mean "root class", right? I'm having this issues without entity inheritance being involved, see updated question for details. – Kalle Richter Mar 28 '16 at 20:55
  • I can trigger the problem by making the topmost class in an inheritance hierarchy a `MappedSuperclass`. I don't get what you mean by `@Inheritance` declared because there's a default value `InheritanceType.SINGLE_TABLE`, i.e. your statement only makes sense if you'd refer to a concrete strategy (and explain which trouble it causes). I verified the issue with all three strategies. – Kalle Richter Mar 29 '16 at 01:59
  • I mean you must use it with entities in an inheritance tree. @MappedSupperclass is not inheritance in that case. – Simon Martinelli Mar 29 '16 at 07:49
  • SELECT a from A a WHERE TYPE(a) = A in your example just doesn't make sense because A has no inheritance. So A will always be A – Simon Martinelli Mar 29 '16 at 07:54
  • See my answer below. – Kalle Richter Mar 31 '16 at 00:03