Tuesday, December 2, 2008

Generic Hibernate DAO

I intended to create a post about Hibernate subselects, but I decided to post the whole Generic Hibernate DAO.

A short briefing of the surrounding system.
I have different child classes of "BasicObject" they all got an according "ObjectRef" which is a small object (id,name,locale) used as reference on the larger BasicObject (like in a tree or document content). The reference object and the BasicObject have the same id (UUID).
The whole software is a server/client application using Eclipse EMF to model, generate and handle the business objects, Eclipse Riena for remoting and Hibernate/Teneo for persistence. Ohh and for sure Eclipse RCP as client. Isn't the Eclipse movement great :-).

To the subselects, I used them in the search(...) method.
The search() restricts the select on BasicObject fields and returns a list of ObjectRefs. This saves us a lot of bandwidth since BasicObjects can have n fields of any size and the ObjectRef only has three.
The first query (DetachedCriteria) which is restricted based on the BasicObject fields (i.e. name LIKE 'test' AND description LIKE 'Start%') is used to restrict the actual query on ObjectRef using the result (ids) of the DetachedCriteria query.

As for the rest of the class, it's a Generic Hibernate Data Access Object even though the name ends in "Service". I just combined the Service and DAO, since in our case, it doesn't make sense to separate them.
Maybe the create() method is a bit confusing, that's because we use the Eclipse Modeling Framework to create the business objects.

BasicObjectService.java


/*******************************************************************************
* $RCSfile: $ Copyright (c) 2007 henzler informatik gmbh, CH-4106 Therwil
*******************************************************************************/
package com.softmodeler.service.impl;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.softmodeler.model.BasicObject;
import com.softmodeler.model.ObjectRef;
import com.softmodeler.service.IBasicObjectService;
import com.softmodeler.service.ModelUtil;

/**
* The BasicObjectService implements the default handling for all BasicObject sub services
*
* @author created by Author: fdo, last update by $Author: $
* @version $Revision: $, $Date: $
*/
public abstract class BasicObjectService implements
IBasicObjectService {
/** The Hibernate Session */
protected Session session;
/** The Entity Manager */
protected EntityManager entityManager;

/**
* The constructor
*
* @param session
* @param entityManager
*/
public BasicObjectService(Session session, EntityManager entityManager) {
this.session = session;
this.entityManager = entityManager;
}

/**
* Returns the class of the BasicObject
*
* @return
*/
@SuppressWarnings("unchecked")
protected Class getObjectClass() {
return (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

}

/**
* Returns the class of the ObjectRef
*
* @return
*/
@SuppressWarnings("unchecked")
protected Class getRefClass() {
return (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
}

/**
* Begins a Transaction
*
* @return
*/
protected EntityTransaction beginTransaction() {
EntityTransaction transaction = entityManager.getTransaction();
if (!transaction.isActive()) {
transaction.begin();
}
return transaction;
}

@SuppressWarnings("unchecked")
@Override
public K create() {
// create the objects using the according EPackage
EPackage modelPackage = ModelUtil.getModelPackage();
EPackage refPackage = ModelUtil.getRefPackage();
EClass objectEClass = (EClass) modelPackage.getEClassifier(getObjectClass().getSimpleName());
EClass refEClass = (EClass) refPackage.getEClassifier(getRefClass().getSimpleName());
K object = (K) modelPackage.getEFactoryInstance().create(objectEClass);
V ref = (V) refPackage.getEFactoryInstance().create(refEClass);

// set the ids
object.setId(EcoreUtil.generateUUID());
ref.setId(object.getId());

// store objects in the database
EntityTransaction transaction = beginTransaction();
session.save(object);
session.save(ref);
transaction.commit();
return object;
}

@SuppressWarnings("unchecked")
@Override
public void delete(K object) {
EntityTransaction transaction = beginTransaction();

// load first, to prevent delete problems
Criteria criteria = session.createCriteria(getObjectClass());
criteria.add(Restrictions.eq(ID, object.getId()));
K loadedObject = (K) criteria.uniqueResult();

Criteria criteriaRef = session.createCriteria(getRefClass());
criteriaRef.add(Restrictions.eq(ID, object.getId()));
V loadedRef = (V) criteria.uniqueResult();

session.delete(loadedObject);
session.delete(loadedRef);
transaction.commit();
}

@SuppressWarnings("unchecked")
@Override
public List findAllRefs() {
Criteria critica = session.createCriteria(getRefClass());
return critica.list();
}

@SuppressWarnings("unchecked")
@Override
public K findById(String uuid) {
Criteria criteria = session.createCriteria(getObjectClass());
criteria.add(Restrictions.eq(ID, uuid));
return (K) criteria.uniqueResult();
}

@SuppressWarnings("unchecked")
@Override
public K findByRef(V ref) {
Criteria criteria = session.createCriteria(getObjectClass());
criteria.add(Restrictions.eq(ID, ref.getId()));
return (K) criteria.uniqueResult();
}

@SuppressWarnings("unchecked")
@Override
public V getRef(K object) {
Criteria criteria = session.createCriteria(getRefClass());
criteria.add(Restrictions.eq(ID, object.getId()));
return (V) criteria.uniqueResult();
}

@Override
public void store(K object) {
EntityTransaction transaction = beginTransaction();
entityManager.merge(object);
transaction.commit();
}

@Override
public void storeRef(ObjectRef ref) {
EntityTransaction transaction = beginTransaction();
entityManager.merge(ref);
transaction.commit();
}


@SuppressWarnings("unchecked")
@Override
public List search(Map fieldValues) {
DetachedCriteria objCritica = DetachedCriteria.forEntityName(getObjectClass().getSimpleName(), "obj");
for (String field : fieldValues.keySet()) {
objCritica.add(Restrictions.like(field, fieldValues.get(field)));
}
objCritica.setProjection(Property.forName(ID));
Criteria criteria = session.createCriteria(getRefClass());
criteria.add(Property.forName(ID).in(objCritica));
return criteria.list();
}
}



Here the according interface:
IBasicObjectService.java


/*******************************************************************************
* $RCSfile: $ Copyright (c) 2007 henzler informatik gmbh, CH-4106 Therwil
*******************************************************************************/
package com.softmodeler.service;

import java.util.List;
import java.util.Map;

import com.softmodeler.model.BasicObject;
import com.softmodeler.model.ObjectRef;

/**
* @author created by Author: fdo, last update by $Author: $
* @version $Revision: $, $Date: $
*/
public interface IBasicObjectService {
/** Id field */
public static final String ID = "id";

/**
* Creates, stores and returns an object
*
* @return
*/
K create();

/**
* Creates, stores and returns an object reference
*
* @return
*/
V getRef(K object);

/**
* Deletes an object
*
* @param object
*/
void delete(K object);

/**
* Stores an object
*
* @param object
*/
void store(K object);

/**
* Stores a reference object
*
* @param ref
*/
void storeRef(V ref);

/**
* Finds an object, by it's uuid
*
* @param AutoJobRef
* @return
*/
K findById(String uuid);

/**
* Finds an object, using it's reference object
*
* @param ref
* @return
*/
K findByRef(V ref);

/**
* Returns a list containing all ref objects
*
* @return
*/
List findAllRefs();

/**
* Searches ObjectRefs using the passed map to create the query
*
* @param fieldValues field name => value
* @return
*/
List search(Map fieldValues);
}

Monday, December 1, 2008

Eclipse Nebula

I was searching for a better SWT DateTime widget, since the standard widget isn't really "done" yet.
Durring my search I stumbled over the Eclipse Nebula project, which provides a few real nice widgets that extend the SWT widget collection.

Currently the widgets are in Alpha and Beta status. Right now I'm using the DateChooserCombo in a CellEditor, I found and reported a Bug and hope the widgets will be stabilzed soon.

http://www.eclipse.org/nebula/