John
John

Reputation:

hibernate query not working in java spring

I have this code and its working fine

Registration person = (Registration) session.get(Registration.class, 8);
      person.setConfirmed(true);
      session.save(person);

but this is not working , says mapping error

String query = "FROM registration WHERE user_id = 8";
 Query query2 = session.createQuery(query);
 Registration person = (Registration) query2.uniqueResult();
 person.setConfirmed(true);
 session.save(person);

This is my registration class

@Column(name = "user_id")
    public Integer getUserId() {
        return userId;
    }

Upvotes: 0

Views: 2812

Answers (2)

chris
chris

Reputation: 3858

@Pasha, the following code is a SQL query not a HQL query.

String query = "FROM registration WHERE user_id = 8";
Query query2 = session.createQuery(query);

If you must run a SQL query, use the following instead:

String query = "FROM registration WHERE user_id = 8";
Query query2 = session.createSQLQuery(query); 
query2.executeUpdate();

To convert the SQL query to a HQL query, assuming the Registration class has userId field:

String query = "FROM registration WHERE userId = 8";
Query query2 = session.createQuery(query); 
query2.executeUpdate();

For a complete example, see the following guide: http://krams915.blogspot.com/2011/03/spring-hibernate-one-to-many.html

Upvotes: 2

dseibert
dseibert

Reputation: 1329

Because you are using a non-native query language, you probably need something like

String query = "FROM Registration WHERE userId = 8";

Upvotes: 4

Related Questions