org.eclipse.emf.teneo.hibernate.HbEntityManagerWrapper Maven / Gradle / Ivy
/**
*
*
* Copyright (c) 2005, 2006, 2007, 2008 Springsite BV (The Netherlands) and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Martin Taal
* Benjamin Cabe
*
*
* $Id: HbEntityManagerWrapper.java,v 1.16 2010/11/12 13:35:37 mtaal Exp $
*/
package org.eclipse.emf.teneo.hibernate;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.Query;
import org.eclipse.emf.teneo.annotations.pannotation.InheritanceType;
import org.hibernate.Session;
import org.hibernate.engine.internal.ForeignKeys;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.mapping.JoinedSubclass;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.SingleTableSubclass;
import org.hibernate.mapping.UnionSubclass;
/**
* Wraps a hibernate entity manager.
*
* @author Martin Taal
* @version $Revision: 1.16 $
*/
public class HbEntityManagerWrapper implements SessionWrapper {
/** The hibernate session */
private EntityManager entityManager = null;
/** The datastore which created me */
private final HbEntityDataStore hbEntityDataStore;
/** The current transaction */
private EntityTransaction entityTransaction = null;
private FlushModeType flushMode = null;
/** Constructor */
public HbEntityManagerWrapper(HbEntityDataStore hbEntityDataStore) {
this.hbEntityDataStore = hbEntityDataStore;
}
/** Set the session in the constructor */
public HbEntityManagerWrapper(HbEntityDataStore hbEntityDataStore, EntityManager entityManager) {
this.hbEntityDataStore = hbEntityDataStore;
this.entityManager = entityManager;
}
/**
* Return the session or entityManager, return is an object to support both session as well as
* entitymanager.
*/
public Object getClassicSession() {
if (entityManager == null) {
entityManager = hbEntityDataStore.getEntityManagerFactory().createEntityManager();
}
return entityManager;
}
/**
* Return the session or entityManager, return is an object to support both session as well as
* entitymanager.
*/
public Object getSession() {
if (entityManager == null) {
entityManager = hbEntityDataStore.getEntityManagerFactory().createEntityManager();
}
return entityManager;
}
/** Convenience which casts */
public EntityManager getEntityManager() {
return (EntityManager) getSession();
}
/** Begin a transaction */
public void beginTransaction() {
assert (entityTransaction == null);
entityTransaction = getEntityManager().getTransaction();
entityTransaction.begin();
}
/** Commit a transaction */
public void commitTransaction() {
if (entityTransaction == null) {
throw new IllegalStateException("EntityTransaction is null, call begin before commit!");
}
entityTransaction.commit();
entityTransaction = null;
}
/** Rollback transaction */
public void rollbackTransaction() {
if (entityTransaction == null) {
throw new IllegalStateException("EntityTransaction is null, call begin before commit!");
}
entityTransaction.rollback();
entityTransaction = null;
}
/** Return an object using the entityname and a serializable id */
public Object get(String entityName, Serializable id) {
return ((Session) getEntityManager().getDelegate()).get(entityName, id);
}
/** Query */
public List> executeQuery(String qry) {
final Query query = getEntityManager().createQuery(qry);
return query.getResultList();
}
/** Query */
public List> executeQuery(String qry, boolean cacheable) {
final Query query = getEntityManager().createQuery(qry);
// todo: cacheable in ejb3?
// query.setCacheable(cacheable);
return query.getResultList();
}
/** Query */
public List> executeQuery(String qry, String entityParameter, Object entity) {
final Query query = getEntityManager().createQuery(qry);
query.setParameter(entityParameter, entity);
return query.getResultList();
}
/** Query */
public List> executeQuery(String qry, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy