javamysqlnamed-query

update query using @namedquery does not work properly


This is my DUsers class:

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.Date;
import java.util.Objects;

@Entity
@Table(name = "d_users")
@NamedQueries({
        @NamedQuery(name = "bonsai.dropwizard.dao.d.DUsers.findAll",
                query = "select e from DUsers e"),
        @NamedQuery(name = "bonsai.dropwizard.dao.d.DUsers.findById",
                query = "select e from DUsers e "
                        + "where e.oAuthId = :id "),
        @NamedQuery(name = "bonsai.dropwizard.dao.d.DUsers.findByOAuthId",
                query = "select e from DUsers e "
                        + "where e.oAuthId = :oAuthId "),
        @NamedQuery(name = "bonsai.dropwizard.dao.d.DUsers.findByEmail",
                query = "select e from DUsers e "
                        + "where e.email = :email "),
        @NamedQuery(name="bonsai.dropwizard.dao.d.DUsers.confirm",
                query = "update DUsers set status = 'HELLO' where oAuthId = :id")

})
public class DUsers implements IDdbPojo{
    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    private String id;
    private String oAuthId;
    private String oAuthType;


    private String firstName;
    private String secondName;
    private String city;
    private String phone;
    private String email;
    private String profileLink;
    private String profilePic;
    private String status;
    private String notificationToken;
    private boolean confirmed;

    private String password;
    private String notes;
    private java.util.Date created_timestamp;
    private java.util.Date updated_timestamp;
    .. getters and setters on-going

As you can see, I have defined a few @NamedQueries and they all work properly except the last one that needs to update my database. In order to run this query, I defined two functions:

private void confirmMailDAO(String id) {
        namedQuery("bonsai.dropwizard.dao.d.DUsers.confirm").setParameter("id", id);

}

public void confirmMailInternal(String id) {
    Session session = sessionFactory.openSession();
    try{
        ManagedSessionContext.bind(session);
        Transaction transaction = session.beginTransaction();
        try{
            confirmMailDAO(id);
            transaction.commit();
        }catch (Exception e) {
            transaction.rollback();
            throw new RuntimeException(e);
        }
    } finally {
        session.close();
        ManagedSessionContext.unbind(sessionFactory);
    }

}

After this I defined a path followed by a POST request that should update my database but sadly it doesn't.

@POST
    @Path("/confirm/{id}")
    public void confirmMail(@NotNull @PathParam("id") String id){
        DUsers user = AppConfig.getInstance().getdUsersDAO().findByIdInternal(id);
        if (user == null) {
            throw new NotAuthorizedException("Error");
        }
        AppConfig.getInstance().getdUsersDAO().confirmMailInternal(id);
    }

Does anyone know where am I getting wrong?


Solution

  • You have set param in named query but forgot to execute it. Pass session to your method and execute like:

     private void confirmMailDAO(Session session, String id) {
            Query query = session.getNamedQuery("bonsai.dropwizard.dao.d.DUsers.confirm").setParameter("id", id);
            query.executeUpdate();
    }