Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no viable alternative at input 'SELECT*'

I'm trying to create a postgresql query and Use entityManger to create the query, but got errors during the creation.

Caused by: org.hibernate.query.sqm.ParsingException: line 1:7 no viable alternative at input 'SELECT*' at org.hibernate.query.hql.internal.StandardHqlTranslator$1.syntaxError(StandardHqlTranslator.java:46)

This query works fine from sql console

SELECT * FROM user_details where user_name = ? LIMIT 1

@Component
@RequiredArgsConstructor
public class UserDaoImp implements UserDao {

    private final EntityManagerFactory entityManagerFactory;

    private static final String FIND_USER_BY_NAME =
            "SELECT * FROM user_details where user_name = ? LIMIT 1";

    @Override
    public User findUserByName(String userName) {
        TypedQuery<User> query = getEntityManager().createQuery(FIND_USER_BY_NAME, User.class);
        query.setParameter("user_name", userName);

        return query.getSingleResult();
    }

Not sure where i missed

like image 561
houhou Avatar asked Oct 26 '25 06:10

houhou


1 Answers

JPQL does not support the SELECT * syntax or raw table names. It operates on entity classes and their fields, not on raw database tables. Here is how u can fix it:-

private static final String FIND_USER_BY_NAME = "SELECT u FROM User u WHERE u.userName = :userName";

like image 93
Nandini Sahu Avatar answered Oct 28 '25 19:10

Nandini Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!