Java TypedQuery<;T>;。getSingleResult返回类型为Class而不是类型为T的对象

Java TypedQuery<;T>;。getSingleResult返回类型为Class而不是类型为T的对象,java,openejb,apache-tomee,Java,Openejb,Apache Tomee,我花了两天的时间来解决这个问题,但我还没有接近解决它。我希望那里的一位专家能提供帮助 问题出在这里。我正在使用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) {

我花了两天的时间来解决这个问题,但我还没有接近解决它。我希望那里的一位专家能提供帮助

问题出在这里。我正在使用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
    }
}
下面是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>

org.apache.openjpa.persistence.PersistenceProviderImpl
支付账户
非JTA支付账户
豆子账户
我肯定这很容易,但我想不出这个。怎么回事


感谢您的帮助……

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

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

我认为您必须强制转换显式
Account Account=(Account)query.getSingleResult()
我尝试过任何一种方法,无论是否使用显式转换,代码都会出现相同的错误(“java.lang.Class不能强制转换为beans.Account”)OMG…就是这样!!现在我看到了解决方案,这个问题对我来说非常有意义。使用“Account”表示类(完全错误),但使用“rec”表示该类的实例。非常感谢你!
SELECT rec FROM Account rec WHERE rec.key = :key AND rec.type = :type
        ^-- use the alias here, and not the class name