【问题标题】:TypedQuery<T>.getSingleResult returns object of type Class instead of type TTypedQuery<T>.getSingleResult 返回 Class 类型的对象而不是 T 类型
【发布时间】:2014-06-02 05:53:20
【问题描述】:

我已经花了两天时间解决这个问题,但我离解决它还差得远。我希望那里的一位专家可以提供帮助。

这就是问题所在。我正在使用 TomEE 1.6.0.2 (OpenEJB 4.6.0.2) 并尝试做一个返回单个结果的简单 TypedQyery,我得到了最奇怪的返回值。这不是我期望的 bean 对象——它是一个 java.lang.Class!

代码片段如下:

public static Account getAccount(String type, String key, EntityManager em) {

    try {
        TypedQuery<Account> query = em.createNamedQuery("Account.getByTypeAndKey", Account.class)
            .setParameter("type", type)
            .setParameter("key", key)
            .setMaxResults(1);

        Account account = query.getSingleResult();  // This statement throws an exception
    }
    catch (Exception e) {
        System.out.println(e.getMessage());         // "java.lang.Class cannot be cast to beans.Account"
    }

    try {
        TypedQuery<Account> query = em.createNamedQuery("Account.getByTypeAndKey", Account.class)
                .setParameter("type", type)
                .setParameter("key", key)
                .setMaxResults(1);

        Object object = query.getSingleResult();    // This works

        System.out.printf("object is '%s'\n", object.toString());
        // "object is 'class beans.Account'"

        Account account = new Account();
        System.out.printf("account is '%s'\n", account.toString());
        // "account is 'Account{id=0, type='null, etc... }'"

        return account;
    }
    catch (Exception e) {
        System.out.println(e.getMessage());         // Never gets here
    }
}

这是 Account 类的一个片段:

@Entity
@Table(name="Account")
@NamedQueries({
    @NamedQuery(name = "Account.getByTypeAndKey", query = "SELECT Account FROM Account rec WHERE rec.key = :key AND rec.type = :type"),
})
public class Account implements Serializable {

    private int id;
    private String type;

    // The usual stuff ...
}

这里是persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="unipagosPersistenceUnit" transaction-type="JTA">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>

        <jta-data-source>PAY_AccountDSJta</jta-data-source>
        <non-jta-data-source>PAY_AccountDSNonJta</non-jta-data-source>

        <class>beans.Account</class>

        <properties>
            <property name="openjpa.DynamicEnhancementAgent" value="true"/>
            <property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>
            <property name="openjpa.Log" value="SQL=TRACE"/>
            <property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72, PrintParameters=true, MaxActive=10, MaxIdle=5, MinIdle=2, MaxWait=60000"/>
        </properties>
    </persistence-unit>

</persistence>

我确信这很容易,但我无法弄清楚这一点。怎么回事?!

TIA 为您提供帮助...

【问题讨论】:

  • 我认为你必须明确地投射Account account = (Account)query.getSingleResult()
  • 我已经尝试过任何一种方式,无论有没有显式转换,代码都会出现相同的错误(“java.lang.Class 不能转换为 beans.Account”)跨度>

标签: java openejb apache-tomee


【解决方案1】:

您的查询是错误的。应该是

SELECT rec FROM Account rec WHERE rec.key = :key AND rec.type = :type
        ^-- use the alias here, and not the class name

【讨论】:

  • 天啊...那是!!现在我看到了解决方案,这个问题对我来说非常有意义。使用“Account”表示类(完全错误),但使用“rec”表示该类的实例。非常感谢!
猜你喜欢
  • 2021-07-19
  • 2011-04-29
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多