/ DEVELOPMENT,Hibernate,Java Java/Hibernate: generate universally a unique object ID with ORM/Hibernate persistence layer

Java/Hibernate: generate universally a unique object ID with ORM/Hibernate persistence layer

In most applications, it is necessary to generate universally a unique object ID. Often, this information which is used like primary key in database, is generated via an Oracle sequencer or procedure. And its value is not really predictable. So, here, an simple solution used in an application with a layer ORM (object-relational mapping) like Hibernate:

First, it’s singleton ID generator class:

import java.util.UUID;

public class IdGenerator {

    public static String createId() {
        UUID uuid = java.util.UUID.randomUUID();
        return uuid.toString();

This class returns IDs with values:


More, the previous generator is used in an abstract class whose inherits the POJO class used in the ORM layer:

public abstract class AbstractPersistentObject {
    private String id = IdGenerator.createId();
    private Integer version = null;


So, the Id information is always initialized from the object’s creation.

Finally, the POJO class extends this abstract class:

public class Document extends AbstractPersistentObject {

    private int num;
    private int year;
    private String type = null;

The HBM file corresponding to the Document POJO persistence class “Document.hbm.xml”:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "">
    <class table="Document" name="" discriminator-value="Document">
        <id name="id" column="c_id" type="string"><generator class="assigned" /></id>
        <discriminator column="class" type="java.lang.String"/>
        <version name="version" column="c_version" unsaved-value="null" />
        <property unique="true" not-null="true" column="c_num" type="int" name="num"/>
        <property not-null="true" column="c_year" type="int" name="year"/>
        <property column="c_type" type="java.lang.String" name="type"/>

To conclude with this simple post/example, here, the classes used in ORM layer in order to take into account the fact that the ID information is always initialized:

The class DAO used in ORM layer:

public class DocumentDaoHibernateImpl extends GenericDaoImpl<Document, String> implements DocumentDao {

	public DocumentDaoHibernateImpl(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
		super(sessionFactory, Document.class);
public interface DocumentDao extends GenericDao<Document, String> {

import java.util.List;

public interface GenericDao<T, PK extends Serializable> {
	PK create(T persistentObject);

	T get(PK id);

	List<T> getAll();

	void update(T persistentObject);
	void createOrUpdate(T persistentObject);

	void delete(T persistentObject);

The generic component ‘GenericDaoImpl’:

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.proxy.HibernateProxy;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class GenericDaoImpl<T, PK extends Serializable> extends HibernateDaoSupport implements GenericDao<T, PK> {

	//private SessionFactory sessionFactory;
	private Class<T> type;

	public GenericDaoImpl(SessionFactory sessionFactory, Class<T> type) {
		this.type = type;

//	public GenericDaoImpl(Class<T> type) {
//		this.type = type;
//	}

	public PK create(T o) {
		return (PK) getSession().save(o);

	@Transactional(propagation=Propagation.REQUIRED, readOnly=true)
	public T get(PK id) {
		T value = (T) getSession().get(type, id);
		if (value == null) {
            return null;

        if (value instanceof HibernateProxy) {
	        value = (T) ((HibernateProxy) value).getHibernateLazyInitializer().getImplementation();
        return value;
		//return (T) getSession().get(type, id);

	@Transactional(propagation=Propagation.REQUIRED, readOnly=true)
	public List<T> getAll() {
		Criteria crit = getSession().createCriteria(type);
		return crit.list();
	public void createOrUpdate(T o) {
		//Sometimes, if createOrUpdate be called with proxy then uncomment this part
//		Class clazz = null;
//        if (o instanceof HibernateProxy) {
//        	clazz = ((HibernateProxy) o).getHibernateLazyInitializer().getImplementation().getClass();
//        } else {
//        	clazz = o.getClass();
//        }
//		if (clazz.isInstance(AbstractPersistentObject.class)) {
//			if (((AbstractPersistentObject) o).isCreation()) {
//				getSession().saveOrUpdate(o);
//			} else {
//				getSession().merge(o);
//			}
//		} else {
//			throw new RuntimeException("this method support only AbstractPersistentObject");
//		}
		if (o instanceof AbstractPersistentObject) {
			if (((AbstractPersistentObject) o).isCreation()) {
			} else {
		} else {
			throw new RuntimeException("this method support only AbstractPersistentObject");

	public void update(T o) {

	public void delete(T o) {

//	public Session getSession() {
//		boolean allowCreate = true;
//		return SessionFactoryUtils.getSession(sessionFactory, allowCreate);
//	}
//	public void setSessionFactory(SessionFactory sessionFactory) {
//		this.sessionFactory = sessionFactory;
//	}

Best regards,


Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post

Backup your projectsBackup your projects

Hello again, Here, I expose a simple solution to backup your developments in the remote directory \\YOUR_SERVER_or_EXTERNALDISK\backupWorkspaces\. To do this: – Create file “backup.dat” in your Eclipse workspace ie “R:\java\dev\eclipse_workspaces\default-3.3”: –

Documentum : Audit trail entries dm_audittrail / SessionConfig / application_codeDocumentum : Audit trail entries dm_audittrail / SessionConfig / application_code

Hi, After my previous posts concerning the Documentum audit trail entries (dm_audittrail) and, here, I would like to expose a solution in order to force content server to