Update a single value from a list of dependent objects

For an object with a list of components:

class Entity{
    Long id;
    String name;
    List<Component> components = new ArrayList<Component>();
}
class Component{ Object value; }

Configuration:

<hibernate-mapping>
    <class name="Entity" table="entity">
        <id name="id" access="field" column="id"/>
        <property name="name" access="field" unique="true"/>

        <list name="components" access="field" table="COMPONENTS" lazy="true">
            <key column="id"/>
            <list-index column="idx"/>
            <composite-element class="Component">
                <property name="value" access="field"/>
            </composite-element>
        </list>
    </class>
</hibernate-mapping>

Is it possible to update one component from the list using the HQL instruction, for example

update Entity e set e.components[:index].value = :value where e.name = :name

what does not work?

Alternatively, you can configure lazy loading of the list of components so that the first access:

entity.components.get(0).value = "..";

not loading the whole list?

Edit: The configuration lazy="extra"works for selection (only loads the component for updating), but does not update the changed component.

+3
source share
1 answer

You cannot update one item in a collection through HQL.

From 13.4. DML style operations section :

  • from-where .
  • , , HQL.

, . , - ; , , .

lazy-load ( , , Nth, ), , , .

( , KLE ):

select c
  from Entity e join e.components c
 where index(c) = :index
+1

Source: https://habr.com/ru/post/1725381/


All Articles