1

Is there a way to update a part of a composite key. Every time I try, JPA creates a new record in the database.

@Embeddable
public class primaryKey implements Serializable{


    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Entity1 key1;
    private Entity2 key2;

I am trying to update the key2.

Here is how I am updating:

EntityWithCompositeKey ent = findBy... //This returns only 1 result
ent.setKey2(Entity2 newKey2);
repository.save(ent);
rena
  • 1,223
  • 3
  • 17
  • 30
  • Possible duplicates [JPA @EmbeddedId: How to update part of a composite primary key?](http://stackoverflow.com/questions/18205248/jpa-embeddedid-how-to-update-part-of-a-composite-primary-key) – Fady Saad May 04 '17 at 04:35
  • If you change part of the set of fields of an object that determine the result of `equals`, then you change the result of `equals`. That is fundamental and proper. By the way, objects don't have "composite keys"; that's a relational concept and not applicable to object models. (And `primaryKey` doesn't conform to the naming conventions.) So of course when you persist an object whose `equals` value has changed, it is a new object in the persistent store. That is fundamental and proper. How could it be different? – Lew Bloch May 04 '17 at 04:41

0 Answers0