com.algoTrader.util.HibernateUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.algoTrader.util.HibernateUtil.java

Source

/*******************************************************************************
 * Copyright (c) 2013 Andy Flury.
 * http://code.google.com/p/algo-trader
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 ******************************************************************************/
package com.algoTrader.util;

import org.hibernate.LockOptions;
import org.hibernate.NonUniqueObjectException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class HibernateUtil {

    public static boolean lock(SessionFactory sessionFactory, Object target) {

        Session session = sessionFactory.getCurrentSession();

        try {
            session.buildLockRequest(LockOptions.NONE).lock(target);
            return true;
        } catch (NonUniqueObjectException e) {
            //  different object with the same identifier value was already associated with the session
            return false;
        }
    }

    public static Object merge(SessionFactory sessionFactory, Object target) {

        Session session = sessionFactory.getCurrentSession();

        return session.merge(target);
    }
}