Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

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);
}

Wednesday, June 11, 2008

Spring Dynamic Modules and Hibernate

I'm currently working on a Sourceforge project.
On the server side I want to use Spring DM and Hibernate.
Eclipse RCP and Spring DM builds the client.

I spent the last few evenings trying and searching for a working Spring DM/Hibernate example. I had a lot of classloading issues.
Yesterday I got it to work, you can download the plug-in here.

It's a simple application handling a User object and using HSQL as database.

I added all the dependent jars in the /lib directory. You can create separated plug-ins for them if you want.
Here a list of all the jar files, because thats where I had my problems:
  • cglib-nodep-2.1_3.jar
  • commons-collections.jar
  • commons-dbcp.jar
  • commons-pool.jar
  • dom4j-1.6.1.jar
  • hibernate3.jar
  • hsqldb.jar
  • jta.jar
  • spring-core.jar
  • spring-jdbc.jar
  • spring-orm.jar
  • spring-tx.jar // why is the org.springframework.dao package in this jar?

You can access the service in the Test class:

package com.blogspot.swissdev.springservice;

import org.osgi.framework.BundleContext;

/**
*
* @author Flavio Donze
*/
public class Test {

private BundleContext context = Activator.getDefault().getContext();

public void start() {
System.out.println("starting the test...");

UserService service = (UserService) context.getService(context.getServiceReference(UserService.class.getName()));

User user = (User) context.getService(context.getServiceReference(User.class.getName()));
user.setPassword("pass");
user.setUsername("user");
service.store(user);

for (User u : service.findAll())
{
System.out.println("User: "+u.getId() + ", " + u.getUsername() + ", " + u.getPassword());
}
}
}


And here is my Spring configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="META-INF/spring/database.properties"/>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>

<bean id="test" init-method="start" class="com.blogspot.swissdev.springservice.Test"/>

<!-- USER beans, POJO, DAO, Service -->
<bean id="user" class="com.blogspot.swissdev.springservice.UserImpl" scope="prototype">
</bean>

<bean id="userDao" class="com.blogspot.swissdev.springservice.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="userService" class="com.blogspot.swissdev.springservice.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>

</beans>
Since I just wanted to get Spring DM and Hibernate to work, I didn't really test the rest, just in case you encounter some bugs.

To setup your workspace with Spring DM read the first part of my previous post.

For this example I used:
Eclipse 3.4 RC3
Spring Dynamic Modules for OSGi(tm) 1.0.2