/**
* GRANITE DATA SERVICES
* Copyright (C) 2006-2015 GRANITE DATA SERVICES S.A.S.
*
* This file is part of the Granite Data Services Platform.
*
* Granite Data Services is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Granite Data Services is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA, or see <http://www.gnu.org/licenses/>.
*/
package org.granite.test.tide.hibernate4.data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.granite.tide.hibernate4.HibernateDataChangeMergeListener;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.internal.SessionFactoryImpl;
public class TestHibernate4JPAChangeSetMerge extends AbstractTestChangeSetMerge {
private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;
private EntityTransaction tx;
@Override
protected void initPersistence() {
Map<String, String> props = new HashMap<String, String>();
props.put("hibernate.dialect", org.hibernate.dialect.H2Dialect.class.getName());
props.put("hibernate.hbm2ddl.auto", "create-drop");
props.put("hibernate.show_sql", "true");
props.put("hibernate.connection.driver_class", org.h2.Driver.class.getName());
props.put("hibernate.connection.url", "jdbc:h2:mem:test-changesetmerge");
props.put("hibernate.connection.username", "sa");
props.put("hibernate.connection.password", "");
entityManagerFactory = Persistence.createEntityManagerFactory("hibernate4-changesetmerge-pu", props);
try {
SessionFactoryImpl sessionFactory = (SessionFactoryImpl)entityManagerFactory.getClass().getMethod("getSessionFactory").invoke(entityManagerFactory);
EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
registry.setListeners(EventType.MERGE, new HibernateDataChangeMergeListener());
}
catch (Exception e) {
throw new RuntimeException("Could not init persistence", e);
}
}
@Override
protected void open() {
entityManager = entityManagerFactory.createEntityManager();
tx = entityManager.getTransaction();
tx.begin();
}
@Override
protected <T> T find(Class<T> entityClass, Serializable id) {
return entityManager.find(entityClass, id);
}
@Override
protected <T> T save(T entity) {
entityManager.persist(entity);
return entity;
}
@Override
protected <T> T merge(T entity) {
return entityManager.merge(entity);
}
@Override
protected void flush() {
entityManager.flush();
tx.commit();
}
@Override
protected void close() {
entityManager.clear();
entityManager.close();
}
}