Frage

I am using google cloud SQL with JDO. When I try to use the JDO PersistenceManager to store new objects with a new key it works fine, however I get an error when I try to update entities already inserted in the db:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '503062001-43661003' for key 'PRIMARY'

The key is indeed duplicated, but I want to update that object.

Is it possible to do this with the PersistentManager.makePersistentAll() method or in an another way that avoids to write the UPDATE query manually?

More details: The object I am trying to persist is defined like this:

PersistenceCapable(table = "xxx")
public class XXX {

   @PrimaryKey
   @Index(name = "xxx_id")
   private Long userId;

   @PrimaryKey
   @Index(name = "xxx_idx")
   @Column(length = 128)
   private String otherId;
   ...
}
War es hilfreich?

Lösung

If you want to update the object then you retrieve the object first, and update it (either in the same txn, or detach it and update it whilst detached). All of that would be described in the JDO spec

For example https://db.apache.org/jdo/pm.html and page down to "Update Objects"

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top