Hibernate saveOrUpdate () doesn't seem to create and save cascade

I am new to Hibernate and am trying to store "UserState" with a "WorkspaceState" list. UserStates are injected using the username that is set, WorkspaceStates are set using the UUID schema. My problem is that if I have a UserState with one WorkspaceState in it, the UserState will be saved, but the WorkspaceState will not.

Here is the Hibernate mapping

<hibernate-mapping>
    <class name="UserState" table="USERSTATE">
        <id name="owner" type="java.lang.String">
            <column name="OWNER" />
            <generator class="assigned" />
        </id>
        <list name="workspaces" inverse="false" cascade="all" table="WORKSPACESTATE" lazy="true">
            <key>
                <column name="UID" />
            </key>
            <list-index></list-index>
            <one-to-many class="WorkspaceState" />
        </list>
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="WorkspaceState" table="WORKSPACESTATE">
        <id name="uid" type="java.lang.String">
            <column name="UID" />
            <generator class="uuid" />
        </id>
        <property name="owner" type="java.lang.String">
            <column name="OWNER" />
        </property>
    </class>
</hibernate-mapping>

      

I have a UserState object with one WorkspaceState in it. When I do session.saveOrUpdate (userst) I can see that hibernate has already removed the WorkspaceState from my custom object. Then the commit saves it to the database without the workspacestate function.

In truth, the WorkspaceStates themselves have lists, but I suspect that whatever I am doing wrong continues on.

thank

Change - how it was done. HibernateUtil is as shown in the standard hibernate docs:

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;

try {
    transaction = session.beginTransaction();
    String username = (String) session.merge(state);
    transaction.commit();
} catch (HibernateException e) {
    transaction.rollback();
    e.printStackTrace();
    return false;
} finally {
    session.close();
}

      

+3


source to share


1 answer


you need to change the cascade link to save-update and remove the inverse attribute from the job list display



+1


source







All Articles