0

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;
    }

2 Answers 2

4

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

String query = "FROM Registration WHERE userId = 8";
Sign up to request clarification or add additional context in comments.

Comments

2

@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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.