1

I would like to design my JPA entities without setters (using annotated fields), because i love "immutable" objects.

All entities will get hashCode/equal implementation without using the generated id (i read here).

My problem is how to update such an entity in data base? Is this possible or is the immutability either on both sided (object and entity) or on no side?

Community
  • 1
  • 1
dermoritz
  • 12,519
  • 25
  • 97
  • 185
  • how would you update the fields of such an entity in Java ? – Neil Stockton Apr 28 '15 at 12:48
  • never i just would create a new object, i only need a way to tell entitymanager that it should update an entity with the data of an object, my hope is there is some thing like update(id,object) – dermoritz Apr 28 '15 at 13:00
  • No there isn't. The API is public http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html persist and merge are the methods for persisting/updating – Neil Stockton Apr 28 '15 at 13:02
  • i think merge could do what i want - couldn't it? How does merge decide if it should persist a new entity or update an existent entity - by @Id or by equals? – dermoritz Apr 28 '15 at 13:41
  • 1
    @dermoritz did you solve it? I also love immutable objects and coding in DDD so I have same problem now :D – minizibi Feb 23 '18 at 12:09

0 Answers0