Hibernate - apply locks to parent tables in polymorphic queries

4.3k views Asked by At

I have two objects:

public class ParentObject {
 // some basic bean info
}

public class ChildObject extends ParentObject {
 // more bean info
}

Each of these tables corresponds to a differnet table in a database. I am using Hibernate to query the ChildObject, which will in turn populate the parent objects values.

I have defined my mapping file as so:

<hibernate-mapping>
<class name="ParentObject"
       table="PARENT_OBJECT">
   <id name="id"
       column="parent"id">
      <generator class="assigned"/>
   </id>
   <property name="beaninfo"/>
   <!-- more properties -->
   <joined-subclass name="ChildObject" table="CHILD_OBJECT">
       <key column="CHILD_ID"/>
       <!--properties again-->
   </joined-subclass>
</class>
</hibernate-mapping>

I can use hibernate to query the two tables without issue.

I use

session.createQuery("from ChildObject as child ");

This is all basic hibernate stuff. However, the part which I am having issues with is that I need to apply locks to the all the tables in the query.

I can set the lock type for the child object by using the query.setLockType("child", LockMode.?). However, I cannot seem to find a way to place a lock on the parent table.

I am new to Hibernate, and am still working around a few mental roadblocks. The question is: how can I place a lock on the parent table?

I was wondering if there was a way around having to do this without undoing the Polymorphic structure that I have set up.

1

There are 1 answers

3
ChssPly76 On BEST ANSWER

Why do you have to lock both tables? I'm asking because depending on what you're trying to do there may be alternative solutions to achieve what you want.

The way things are, Hibernate normally only locks the root table unless you're using some exotic database / dialect. So, chances are you're already locking your ParentObject table rather than ChildObject.

Update (based on comment):

Since you are using an exotic database :-) which doesn't support FOR UPDATE syntax, Hibernate is locking the "primary" tables as they are specified in query ("primary" in this case being table mapped for the entity listed in FROM clause, not the root of the hierarchy - e.g. ChildObject, not ParentObject). Since you want to lock both tables, I'd suggest you try one of the following:

  1. Call session.lock() on entities after you've obtained them from the query. This should lock the root table of the hierarchy, however I'm not 100% sure on whether it'll work because technically you're trying to "upgrade" the lock that's already being held on a given entity.
  2. Try to cheat by explicitly naming ParentObject table in your query and requesting lock mode for it:

    String hql = "select c from ChildObject c, ParentObject p where c.id = p.id";
    session.createQuery(hql)
     .setLockMode("c", LockMode.READ)
     .setLockMode("p", LockMode.READ).list();