简体   繁体   English

休眠查询无法在Java Spring中工作

[英]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;
    }

由于您使用的是非本地查询语言,因此可能需要类似

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

@Pasha, the following code is a SQL query not a HQL query. @Pasha,以下代码是一个SQL查询而不是HQL查询。

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

If you must run a SQL query, use the following instead: 如果必须运行SQL查询,请改用以下命令:

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: 要将注册查询转换为HQL查询,请假设Registration类具有userId字段:

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 有关完整的示例,请参见以下指南: http : //krams915.blogspot.com/2011/03/spring-hibernate-one-to-many.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM