I have a JSON-Mapping in my entity
{code:java}@Entity public class MyEntity { @Id private Long id; @JdbcTypeCode(SqlTypes.JSON) private MyJson jsonProperty; private String info; }{code}
MyJson is a simple POJO:
{code:java}@Embeddable public class MyJson { private String stringProp; private Long longProp; }{code}
If i change only the jsonProperty, it seems the entity is _not marked dirty_ → changes are not written to the DB.
If i change any other field-value to , too (in my test-case the String-field _info_), the entity is marked dirty and all changes are written to DB, even the JSON-changes.
(see _MyEntityTest.java_ in attached Test-project):
{code:java}@QuarkusTest @Transactional(Transactional.TxType.REQUIRES_NEW) class MyEntityTest {
@Inject EntityManager em; static final long PK = 123L; static final String CHANGED = "CHANGED";
@Test void shouldCreateUpdateAndSelectMyEntity() throws Exception { insert(); findAndUpdate(); selectFiltered(); }
void insert() { MyEntity myEntity = new MyEntity(); myEntity.setId(PK); MyJson myJson = new MyJson(); myJson.setLongProp(100L); myJson.setStringProp("Hello"); myEntity.setJsonProperty(myJson); em.persist(myEntity); }
void findAndUpdate() { MyEntity found = em.find(MyEntity.class, PK); found.getJsonProperty().setStringProp(CHANGED); // found.setInfo(CHANGED); // by changing any other property of the entity, it will be marked as dirty and EVERY change will be written to the DB }
void selectFiltered() { List<MyEntity> result = em .createQuery("SELECT e FROM MyEntity e WHERE e.jsonProperty.longProp = :x", MyEntity.class) .setParameter("x", 100L) .getResultList(); assertEquals(1, result.size()); assertEquals(CHANGED, result.get(0).getJsonProperty().getStringProp(), "json property not changed"); assertEquals(CHANGED, result.get(0).getInfo(), "plain property not changed"); } }{code}
see [https://discourse.hibernate.org/t/changes-in-jsonb-field-do-not-mark-entity-as-dirty/7684/6|https://discourse.hibernate.org/t/changes-in-jsonb-field-do-not-mark-entity-as-dirty/7684/6|smart-link] |
|