繁体   English   中英

为什么我不能使用TYPE-WHERE子句查询与JPA 2.1中的实体类型匹配的实体?

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

实体类A形式的查询,形式为

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

失败

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]

我不明白,因为将A限制为A应该排除所有不是A的超类实例以及子类实例。

例:

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>

org.hibernate:hibernate-entitymanager:5.1.0.Finalorg.apache.derby:derby:10.11.1.1

在此示例中, A是一个POJO,它不涉及继承,这与我期望的限制无关紧要(尽管在没有继承的情况下应用它是没有意义的):

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

完整的示例可以在https://github.com/krichter722/type-operator-nonsense中找到。

TYPE()仅可用于继承映射。

A是继承映射中的类,而A是带有@Inheritance的路由类还是A扩展了声明了@Inheritance的类?

例。

@Inheritance
@Entity
public class Project

@Entity
public class DesignProject extends Project

@Entity
public class QualityProject extends Project

@Entity
public class SoftwareProject extends Project

现在您可以使用TYPE()

SELECT p
FROM Project p
WHERE TYPE(p) = DesignProject OR TYPE(p) = QualityProject

发生此行为是由于休眠错误https://hibernate.atlassian.net/browse/HHH-10653所致。 上面的示例在OpenJPA上运行良好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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