Syed
Syed

Reputation: 2607

Update query not working propertly using namedquery in jpa

I'm trying to update a simple table using jpa. I have written a jpa namedquery for it

@NamedQuery(name="updatedetails",query="update myentity set enddesc=?, startdesc=? Where id=?")

My code is as follows

em.createNamedQuery("updatedetails",myentity.class).serParameter(1, "abcd").serParameter(2,"gjvg").serParameter(3, "gghjj").executeUpdate();

myentity is my class name

It throws me the following error

Encountered "=" at character but expected ["."] While parsing the query

Is there anything wrong with the query

Upvotes: 0

Views: 7564

Answers (3)

BRAIEK AYEMN
BRAIEK AYEMN

Reputation: 87

write this code in model class

 @NamedQuery(name = "new_ticket_bat.UpdateflagAutres", query = "UPDATE new_ticket_bat t set t.status_AUTRE='0' WHERE t.id= :id")
<br>

write this code in you service class

 static void updateflagAutres(String id) {
       DataBaseTools dbTools = new DataBaseTools("databaseOv");
        try {
        Query q=dbTools.em.createNamedQuery("new_ticket_bat.UpdateflagAutres");
            q.setParameter("id", id);
            q.executeUpdate();
            dbTools.em.getTransaction().commit();
            dbTools.em.close();
         } catch (Exception ex) {
            ex.printStackTrace();
            Tools.traiterException(Tools.getStackTrace(ex));
        } 
    }

Upvotes: 0

Akhil KC
Akhil KC

Reputation: 76

em.createNamedQuery("updatedetails",myentity.class).serParameter(1, "abcd").serParameter(2,"gjvg").serParameter(3, "gghjj").executeUpdate();

Instead of serParameter use setParameter, should work.

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522311

I believe that JPA named queries should use actual names are parameter placeholders, rather than ?, the latter which is used in prepared statements. So something like this should work:

@NamedQuery(name="updatedetails",query="update myentity set enddesc = :enddesc, startdesc = :startdesc Where id = :id")

List<myentity> results = em.createNamedQuery("updatedetails", myentity.class)
                           .setParameter("enddesc", "abcd")
                           .setParameter("startdesc", "gjvg")
                           .setParameter("id", "gghjj")
                           .getResultList();

As side note, you should probably make your class names begin with uppercase letters, i.e. call it MyEntity, rather than what you currently have.

Upvotes: 6

Related Questions