I do want to store a more complex object structure serialized as JSON to a single text field to the database. I tried to use an AttributeConverter as following:
- NameElement.java: A simple data container class.
- NameListConverter.java: A converter to serialize a List<NameElement> to JSON and back.
- Country.java: My Entity class, containing a List<NameElement>.
- CountryServiceBean.java: an EJB with all entity manager code needed for the Country entity.
- CountryServiceTest.java: An arquilian unit test for the code above.
The unit test class contains 3 different tests, doing mostly the same:
- Create an Country object
- Add 3 names to the list: A, B and C
- Persist this object to DB
- Reload from DB, check if the name list is as expected (A, B, C)
- Reorder the name list to A, C, B
- Write back to DB
- Reload from DB and check again if the name list is as expected (A, C, B).
The funny thing is: Two tests work but one test fail, because the DB still contains the order A, B, C. The difference between the tests is:
- resortDetached() makes the EntityManger#merge() with an changed but detached object. This works.
- resortInTransaction() calls a method within the EJB to load and modify the list in one transaction. This time, the modification is not written back to DB and the test fails!
- resortInTransaction_ButChangeName() same as resortInTransaction(), but this time, not only the name list, but also a 'simple' attribute of the entitiy object is changes. Because of this change, Hibernate detects a change, wants to save the result and 'by accident' also updates the name list. So this test works.
So as a conclusion, I found out, that in the test case resortInTransaction() Hibernate is blind for the changed data! It does not even request the converter to serialize the data. The changes just got lost. If you change the Country - entity to @DynamicUpdate(true), also the third test fails, because now he only update the changed county name. I also had similar problems with JSE environment, but did not spent too much time here. I hope, one concrete example of the problem is enough. Sorry If I report something wrong or unexpected, but this is my first issue, reported here. |