【发布时间】:2016-03-28 18:18:03
【问题描述】:
以实体类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 应该排除所有不是As 的超类实例以及子类实例。
例子:
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.Final 和 org.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;
}
}
【问题讨论】:
-
我只是探索了这个问题,发现很少有人愿意与您分享。 您能否尝试使用 .class 而不是 Type() "SELECT a from A a WHERE a.class= A" 或者为实体类 A 创建一个存储库并使用您的实际查询访问 Type -SELECT a from A a WHERE TYPE(a) = A
-
SELECT a from A a WHERE a.class = A失败并出现完全相同的错误,这表明这可能是休眠错误或无意义的反馈。 “存储库”是什么意思 -jar依赖项 - 说 maven 术语?如果是,那为什么会有什么不同? -
我的意思是 JPA repsitory 和休眠的 .class 表示法。请参考链接可能对您有用stackoverflow.com/questions/4884249/…
-
当我在 GitHub 上查看您的示例时 - @Inheritance 不应该在 A 上吗?因为 B 没有子类,因此注释没有意义。看看下面我的项目示例。