Java 连接失败的JPQL createNativeQuery

Java 连接失败的JPQL createNativeQuery,java,mysql,jpa,jpql,toplink,Java,Mysql,Jpa,Jpql,Toplink,我在db中加入了两个具有外键关系的实体,但在代码中没有加入(将原因另存为一个问题): 但查询在运行时失败,原因是: Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the r

我在db中加入了两个具有外键关系的实体,但在代码中没有加入(将原因另存为一个问题):

但查询在运行时失败,原因是:

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 
You have an error in your SQL syntax; check the manual that corresponds  to your MySQL 
server version for the right syntax to use near ':communityId AND 
lower(u.email) = :email' at line 1
Error Code: 1064
Call: SELECT u.* FROM user u JOIN user_community_organization uco ON  
u.user_id = uco.user_id WHERE uco.community_id = :communityId AND lower(u.email) = :email
我尝试了不同的变化,将参数移入和移出ON位,移除较低的参数。什么都不管用

在添加联接之前:

em.createQuery("select object(o) from User as o where lower(o.email) = :email");
        q.setParameter("email", email.toLowerCase());
这个问题很有效

我做错了什么?
在GlassFish3.1、toplink和mySql上运行。

您的第一个示例使用本机查询,但您的第二个示例使用JPQL,因此不清楚您要做什么。我认为您的本机查询失败是因为JPA中不支持命名参数(仅支持位置参数,但Hibernate等支持它)。那么,试试这个

em.createNativeQuery("SELECT u.* FROM user u JOIN user_community_organization uco ON "
                + "u.user_id = uco.user_id "
                + "WHERE uco.community_id = ?1 "
                + "AND lower(u.email) = ?2", User.class)
                .setParameter(1, communityId)
                .setParameter(2, email.toLowerCase());
至于JPQL版本,您并没有发布实体代码,所以我将对关系进行猜测,但它看起来像这样

em.createQuery("select u from User u where lower(u.email) = :email and u.communityOrganisation.id = :communityId");
        q.setParameter("email", email.toLowerCase());
        q.setParameter("communityId", communityId);

你确定你在给我们看真正的代码吗?SQL日志显示参数尚未被替换。为什么要使用本机SQL查询而不是JPQL查询?@JB Nizet这是我下一个问题的主题-这涉及到一个三向链接,而我一直无法使用JPQL使其正常工作,所以在此期间我使用的是老式的方式。doh,我怎么会错过这一点?谢谢
em.createQuery("select u from User u where lower(u.email) = :email and u.communityOrganisation.id = :communityId");
        q.setParameter("email", email.toLowerCase());
        q.setParameter("communityId", communityId);