简体   繁体   中英

Why can't I query an entity with a TYPE-WHERE clause matching the entity type in JPA 2.1?

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 A s 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;
    }
}

The complete example can be found at https://github.com/krichter722/type-operator-nonsense .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM