Bulk update of embedded entities in linked list

#1

Hi,

I'm trying to do a bulk update of a column in an embedded entity, but I end up with a query execution error because of the "." between the column name of the parent entity and column name of the child entity.

The column in the parent entity represents a linked list of an embeddable class.

Example:

                int updateCount = em.createQuery(
                    "UPDATE EntityA a " +
                    "SET a.EntityBList.Column1 = :value " +
                    "where a.uid = :uid")
                    .setParameter("value", 100.0)
                    .setParameter("uid", 12345)
                    .executeUpdate();
 

The dot before "Column1" in the SET part of the query is causing the "unexpected query token" exception.

How can I do update statements like that?

Kind Regards,

Roland

#2

JPA Update queries are limited to updating the objects in the UPDATE clause directly only (i.e. instances of EntityA in your example) and not referenced objects (e.g. instances of EntityB).

When UPDATE queries cannot be used you may update objects without UPDATE queries.

ObjectDB Support
#3

Hi,

Thank you for the clarification.

I already did some tests trying to update the entity itself as described in the documentation.

The nature of my application is to do some data maintenance in the database of an existing business application, I don't have the source code for. So far I used object arrays and reflection to read the data, and UPDATE queries to update the data and everything works fine.

But it seems the entity update inside the transaction doesn't work with the objects I'm using. I also tried to change a simple string field of the object. After committing the changes and selecting the objects again, no data has changed.

Example:

...
        for (Object entity : resultSet)
        {
            em.getTransaction().begin();

            @SuppressWarnings("unchecked")
            Iterable<Object> linkedList = (Iterable<Object>) ReflectionHelper.getField(entity, "LinkedListField");
            for (Object x : linkedList)
            {
                ReflectionHelper.setField(x, "Amount", 0.0d);
            }

            ReflectionHelper.setField(entity, "LinkedListField", linkedList);

            em.getTransaction().commit();
        }
...

I can see the changed values while debugging, but after a fresh selection of the previously changed object, the old values are still there.

Kind Regards,

Roland

#4

Try JDO's makeDirty as shown on that documentation page.

ObjectDB Support
#5

It works! Thank you so much!

(I should have read the documentation more carefully)

Reply