奇怪的实体管理器错误

时间:2012-11-24 23:30:38

标签: java openjpa

我已经提出了这个问题:

Usuario user1=(Usuario) request.getSession().getAttribute("user");
String query1 = "SELECT f FROM Favorito f WHERE f.usuarioBean = 'user1'";

我在这里调用Manager方法

List<Favorito> favoritos = managerFavorito.getFavoritoByUser(query1);

Manager方法如下所示:

public List<Favorito> getFavoritoByUser(String query1) {
        EntityManager em = getEntityManager();
        List<Favorito> results = null;
        try {
            Query query = em.createNativeQuery(query1);
            results = (List<Favorito>) query.getResultList();
        } finally {
            em.close();
        }
        return results;
    }

错误:

 org.apache.openjpa.persistence.ArgumentException: Metadata not found for type "class java.lang.String". Class not improved.

完整错误:

[25/11/12 0:17:37:538 CET] 0000002b servlet       E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0068E: Se ha creado una excepción no detectada en uno de los métodos de servicio del servlet servletControl en la aplicación MotorSalesGR83.15. Excepción creada: <openjpa-2.0.2-SNAPSHOT-r422266:1032678 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: No se han encontrado metadatos para el tipo "class java.lang.String". La clase no se ha mejorado.
    at org.apache.openjpa.meta.MetaDataRepository.getMetaDataInternal(MetaDataRepository.java:377)
    at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:358)
    at org.apache.openjpa.jdbc.meta.MappingRepository.getMapping(MappingRepository.java:355)
    at org.apache.openjpa.jdbc.meta.strats.RelationStrategies.toDataStoreValue(RelationStrategies.java:88)
    at org.apache.openjpa.jdbc.meta.strats.RelationFieldStrategy.toDataStoreValue(RelationFieldStrategy.java:836)
    at org.apache.openjpa.jdbc.meta.FieldMapping.toDataStoreValue(FieldMapping.java:938)
    at org.apache.openjpa.jdbc.kernel.exps.PCPath.toDataStoreValue(PCPath.java:844)
    at org.apache.openjpa.jdbc.kernel.exps.Lit.calculateValue(Lit.java:109)
    at org.apache.openjpa.jdbc.kernel.exps.CompareEqualExpression.appendTo(CompareEqualExpression.java:98)
    at org.apache.openjpa.jdbc.kernel.exps.SelectConstructor.buildWhere(SelectConstructor.java:298)
    at org.apache.openjpa.jdbc.kernel.exps.SelectConstructor.evaluate(SelectConstructor.java:93)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreQuery.createWhereSelects(JDBCStoreQuery.java:349)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreQuery.executeQuery(JDBCStoreQuery.java:187)
    at org.apache.openjpa.kernel.ExpressionStoreQuery$DataStoreExecutor.executeQuery(ExpressionStoreQuery.java:776)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1003)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:861)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:792)
    at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:542)
    at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:288)
    at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:302)
    at entities.controller.FavoritoManager.getVehiculoFavorito(FavoritoManager.java:172)
    at tiw.servletControl.doPost(servletControl.java:78)
    at tiw.servletControl.doGet(servletControl.java:38)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:718)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1657)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:179)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3933)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:931)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1583)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1604)

出了什么问题?我开始认为我不能使用简单的Where来搜索Favorito中的行。

1 个答案:

答案 0 :(得分:1)

您的代码中发生了一些奇怪的事情。

首先,您将user1对象拉出会话,但从未使用它。此外,在您的查询中,您声称f.usuarioBean等于字符串&#39; user1&#39;,我原以为永远不会这样:

String query1 = "SELECT f FROM Favorito f WHERE f.usuarioBean = 'user1'";

我认为你的意思是做这样的事情:

String query1 = "SELECT f FROM Favorito f WHERE f.usuarioBean = '" + user1.getId() + "'";

更好的方法(实际上,唯一正确的方法)是使用查询参数:

String query1 = "SELECT f FROM Favorito f WHERE f.usuarioBean = ?";
// and then later:
query.setParameter(1, user1);

其次,您正在调用createNativeQuery,它将SQL查询字符串作为参数。但是,您传递的字符串不是SQL,它是JPQL(您无法在SQL中选择表别名,可以吗?)。您应该调用createQuery,或者更好的是,过载createQuery(String, Class),它会创建TypedQuery,以提高类型安全性。