Org.hibernate.TransientObjectException during Criteria.list ()

I saw posts all over the internet that talk about how to fix TransientObjectExceptions during save / update / delete, but I have this problem when calling a list in my criteria.

I have two objects A and B. A has a field named b that is of type B. In my mapping, b is displayed as many-to-one. All this works in a larger save structure (the structure is similar to Core Data), so I don’t use cascades in my sleeping comparisons, because cascades are processed at a higher level.

This is an interesting code surrounding my criteria:

A a = new A();
B b = new B();
a.setB(b);

session.save("B", b); // Actually handled by the higher level
session.save("A", a); // framework, this is just for clarity

// transaction committed and session closed
...
// new session opened

Criteria criteria = session.createCriteria(A.class);
criteria.add(Restrictions.eq("b", b));
List<?> objects = criteria.list();

A, Ab b ( , b , , b ).

, , crit.list():

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: B
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:244)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:449)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:141)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1769)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1740)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1612)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2294)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
at org.hibernate.loader.Loader.list(Loader.java:2167)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1706)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)

:

<class entity-name="A" lazy="false">
    <tuplizer entity-mode="dynamic-map" class="MyTuplizer" />
    <id type="long" column="id">
        <generator class="native" />
    </id>
    <many-to-one name="b" entity-name="B" column="b_id" lazy="false" />
    </class>

    <class entity-name="B" lazy="false">
    <tuplizer entity-mode="dynamic-map" class="MyTuplizer" />
    <id type="long" column="id">
    <generator class="native" />
    </id>
    </class>

- , TransientObjectException ? , , , , .

+3
3

, b , , . , . , :

session.update(b);

Hibernate:

update() SQL UDPATE. Hibernate , , . ( , , , a lock() .) , .

saveOrUpdate() , update(), save() lock(): , . , saveOrUpdate(), , , .

, merge() , , . , , , NonUniqueObjectException.

+1

cascade = all . ,

<class entity-name="A" lazy="false">
<tuplizer entity-mode="dynamic-map" class="MyTuplizer" />
<id type="long" column="id">
<generator class="native" />
</id>
<many-to-one name="b" entity-name="B" column="b_id" lazy="false" cascade="all" />
</class>

<class entity-name="B" lazy="false">
<tuplizer entity-mode="dynamic-map" class="MyTuplizer" />
<id type="long" column="id">
<generator class="native" />
</id>
</class>
0

Since you have insert data like persist (), save () in sleep mode. But the above error is that you simply use the merge () method and others that can execute update information.

0
source

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


All Articles