-3

From what I know, UPDATE and DELETE can be done by NamedQueries.

However,

void updateName (int ID, String name) {        

   EntityManager entityManager = 
      Persistence.createEntityManagerFactory("uPU").createEntityManager();
   Query query = entityManager.createNamedQuery("Users.updateName");
   query.setParameter("name", name);
   query.setParameter("id", ID);
}

isn't doing the update.

The named query is as follows:

@NamedQuery(name = "Users.updateName", query = "UPDATE Users u SET u.name = :name "
        + "WHERE u.id = :id "),

A similar thing going on with DELETE.

Nothing wrong with SELECT on Namedqueries.

Is there something more to altering the contents of an SQL table with NamedQueries?

//================================

ADD:

i'm using a namedquery.

From what I know, opening the transaction and commiting it is not necessary on namedQueries.

query.executeUpdate();

gives a runtime error.

//================================

ADD2:

The code is giving no compile or runtime errors except to that addition suggested by DataNucleus below. see my previous edition above regarding that suggestion.

Roam
  • 4,831
  • 9
  • 43
  • 72

1 Answers1

1

Thought about actually executing the query? All you've done there is create it.

query.executeUpdate()

http://www.datanucleus.org/products/accessplatform/jpa/jpql.html#JPQL_UPDATE_Queries

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
  • see my edit after the line comment. special emphasis on named query. – Roam Mar 13 '14 at 08:13
  • 4
    If you have an exception don't you think people would like to see it (and its stacktrace)? If the exception is a TransactionRequiredException, then that is what the JPA spec says will be thrown if no tx is present ("named" or otherwise) - look at the spec. Obviously that depends on your JPA implementation too, since some (e.g DataNucleus JPA) allow nontransactional operations. – DataNucleus Mar 13 '14 at 08:28