Home > Back-end >  how to update single value for multiple ids in Hql.?
how to update single value for multiple ids in Hql.?

Time:09-17

String sql = "UPDATE table SET enabled= true"; sql = " WHERE m_id=" mId " AND id IN (:Id)"; where Ids is string consist of multiple values with comma separated. I am trying in HQl like this

HQL Query

String stringQuery = "UPDATE table SET `enable`=:ENABLED
       WHERE m_id=:M_ID AND id IN (:Id)";
       TypedQuery<String> query = session.createSQLQuery(stringQuery);
       query.setParameter("ENABLED", true);
       query.setParameter("M_ID", mId);
       query.setParameter("ID", Ids);
       query.executeUpdate();

Entity

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Entity
    @Table(name = "table")
    public class Table implements Serializable {
        private static final long serialVersionUID   = 1L;
    
        @Id
        @Column(name = "id")
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
        @Column(name = "m_id")
        private Integer MId;
     @Column(name = "enabled")
        private Boolean enabled;
    }

but i am facing javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement

CodePudding user response:

Inside @Table(name = "table") table is MYSQL reversed keyword

Change:

@Table(name = "table")

To:

@Table(name = "tbl_master")

For more info about reversed keyword see here https://dev.mysql.com/doc/refman/8.0/en/keywords.html

CodePudding user response:

Please use below query

 String stringQuery = "update table set enable=:ENABLED
            where m_id=:M_ID and id in (:Id);
            TypedQuery<String> query = session.createSQLQuery(stringQuery);

query.setParameter("ENABLED", true);
             query.setParameter("M_ID", mId);
             query.setParameter("ID", Ids);
              query.executeUpdate();
  • Related