开发者

Query needed to check if an email already exist in a DB(Java Persistence API)

I try to figure out what is the problem with this method that uses JPQL to validate if an email al开发者_如何学Cready exist in the DB, for some reason it does not work. Someone can have a look? Or give another alternative query more simple?

@Override
public boolean emailAlreadyExists(String value) {
    Query checkEmailExists = em.createQuery("SELECT COUNT(b.email) FROM "
            + Buyer.class.getName() + " b WHERE email = :emailparam");
    checkEmailExists.setParameter("emailparam", value);
    long matchCounter = 0;
    matchCounter = (Long) checkEmailExists.getSingleResult();
    if (matchCounter > 0) {
        return true;
    }
    return false;
}

This is part of the console output:

Caused by: Exception [EclipseLink-8024] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.JPQLException Exception Description: Syntax error parsing the query [SELECT COUNT(b.email) FROM entities.Buyer b WHERE b.email = :emailparam], line 1, column 35: syntax error at [.]. Internal Exception: MismatchedTokenException(83!=78)

Im sure it has to do something with the syntax. But where i am making the mistake?


Typically JPA uses short class names (without package name). And I personally have never used aliases in JPA queries, so I am not sure they are supported there. And you do not need them in your query. And I am not sure the space between = and : is permitted.

So, try this: SELECT COUNT(email) FROM Buyer WHERE email=:emailparam

I hope this will work. In this case try to play with the query and see which one of the changes really does the work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜