Darlyn
Darlyn

Reputation: 4940

Unknown column error using eclipselink jpa

I am using JPA eclipse link with spring to find a data in the database based on a string. I have implemented a method

@Transactional
    public String authorization(LL l){
        Query query = m.createNativeQuery("SELECT a FROM user a WHERE a.name = ?");
        query.setParameter(1, l.getName());
        UserEntity tmp = (UserEntity) query.getSingleResult();
}

My database is simple

id | name | password 

However, by executing this I get error

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'a' in 'field list'

I have read the documentation and this should be the right way ho to execute such query. Why is this throwing said error? Did I make mistake or did I overlook something?

All help is appreciated.

Upvotes: 0

Views: 244

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26492

You seem to expect to have an entity as a result, so instead of a native query:

m.createNativeQuery

use the

m.createQuery

Update

In case you need to use native query you should add the expected result class:

Query query = m.createNativeQuery("SELECT a.* FROM user a WHERE a.name = ?", UserEntity.class);

Upvotes: 1

Related Questions