private Lock createLock(Lockable l) {
   Lock lock = new Lock();
   String className = l.getClass().getCanonicalName();
   try {
     className = Hibernate.getClass(l).getCanonicalName();
   } catch (HibernateException h) {
     log.error("Hibernate getClass() ", h);
   }
   // log.debug( "Example log: "+l.getLockname()+" "+l.getClass().getCanonicalName()+"->"+className
   // );
   lock.setObjectId(l.getDbID());
   lock.setObjectType(className);
   lock.setName(l.getLockname());
   return lock;
 }
  /**
   * Aquire lock for given item or check that you still have it. This should be done before database
   * objects are updated, not just before they are made persistent. It should not be done (Cannot be
   * done) with objects that don't have a dbID yet.
   *
   * @param u
   * @param sessionId
   * @param item
   * @return
   */
  public boolean aquireLock(User u, String sessionId, Lockable item) {
    boolean result = false;
    StatelessSession s = getSession();
    Transaction tx = s.beginTransaction();
    if ((item == null) || (item.getDbID() == null)) throw new Error("Unlockable item");
    try {
      Lock lock = createLock(item);
      lock.setUserLogin(u.getLogin());
      lock.setHttpSessionId(sessionId);
      log.debug("Aquire Lock\n" + lock.toString());
      List<Lock> l =
          s.createQuery("from Lock where objectId = :id and objectType = :type")
              .setLong("id", lock.getObjectId())
              .setString("type", lock.getObjectType())
              .list();
      if (l.size() == 0) {
        s.insert(lock);
        tx.commit();
        result = true;
      } else {
        Lock lock2 = l.get(0);
        if (lock2.getUserLogin().equals(lock.getUserLogin())
            && lock2.getHttpSessionId().equals(lock.getHttpSessionId())) result = true;
        else result = false;
        tx.commit();
      }
    } catch (HibernateException he) {
      log.debug("lock aquire failed");
      StringWriter sw = new StringWriter();
      he.printStackTrace(new PrintWriter(sw));

      log.error(sw.toString());
      tx.rollback();
    }
    return result;
  }