net.sourceforge.domian.repository
Class HibernateRepository<T extends Entity>

java.lang.Object
  extended by net.sourceforge.domian.repository.AbstractRepository<T>
      extended by net.sourceforge.domian.repository.AbstractDomianCoreRepository<T>
          extended by net.sourceforge.domian.repository.HibernateRepository<T>
All Implemented Interfaces:
BinaryFormatRepository<T>, PersistentRepository<T>, Repository<T>

public class HibernateRepository<T extends Entity>
extends AbstractDomianCoreRepository<T>
implements BinaryFormatRepository<T>

This repository implementation is based on the Hibernate object-relational mapping (ORM) tool, and its implementation of the Java Persistence API 1.0 (JPA). The Hibernate Query Language (HQL) is used as a fallback solution for both JPA EntityManager and JPA Query Language (JPQL) where suitable.

Highly experimental code as it has yet to be in any form of serious use. The only tested RDBMS is H2.

Conversion of specification object graph to JPQL/HQL is not yet completed!

Partitioning of this repository is not yet completed!

This repository has a table naming convention: all table names must end _TABLE. In your JPA ORM configuration file, you must explicitely configure all table names ending with _TABLE. JPA does not seem to allow custom naming strategies...

The persistence definition supported by this repository is PersistenceDefinition.DELEGATED.

Since:
0.4.2
Author:
Eirik Torske
See Also:
Java Persistence API (JPA)

Nested Class Summary
protected  class HibernateRepository.HibernateRepositoryIterator<V extends T>
          Custom iterator class for HibernateRepository.
protected  class HibernateRepository.Update
           
 
Field Summary
protected  java.util.Map<java.lang.String,java.lang.String> configurationMap
           
protected  java.lang.ThreadLocal<javax.persistence.EntityManager> entityManager
           
protected  javax.persistence.EntityManagerFactory entityManagerFactory
           
protected  net.sourceforge.domian.repository.Rdbms rdbms
           
protected  java.lang.String rdbmsServerName
           
protected  java.io.File repositoryDirectory
           
protected  java.lang.String repositoryDirectoryString
           
protected  java.lang.String repositoryId
           
protected  java.lang.String repositoryRootDirectoryString
           
protected  java.util.List<java.lang.String> tableNameList
           
 
Fields inherited from class net.sourceforge.domian.repository.AbstractDomianCoreRepository
DEFAULT_DOMIAN_ROOT_DIR_NAME, DEFAULT_DOMIAN_ROOT_PATH, log, synchronizer
 
Fields inherited from class net.sourceforge.domian.repository.AbstractRepository
supportsRecursiveIndexing, usesNativePartitioningSupport
 
Constructor Summary
HibernateRepository(net.sourceforge.domian.repository.Rdbms rdbms, java.lang.String repositoryId)
           
HibernateRepository(net.sourceforge.domian.repository.Rdbms rdbms, java.lang.String rdbmsServerName, java.lang.String repositoryRootDirectoryString, java.lang.String repositoryId)
           
HibernateRepository(net.sourceforge.domian.repository.Rdbms rdbms, java.lang.String rdbmsServerName, java.lang.String repositoryRootDirectoryString, java.lang.String repositoryId, java.util.Map<java.lang.String,java.lang.String> configurationMap)
           
 
Method Summary
 void close()
          Closes the repository and releases all resources.
protected  java.lang.Boolean contains(T entity)
           
<V extends T>
java.util.Collection<V>
findAllEntitiesSpecifiedBy(Specification<V> specification)
          Finds and returns all entities approved by the given specification.
<V extends T>
V
findSingleEntitySpecifiedBy(Specification<V> specification)
          Finds and returns one single entity approved by the given specification.
protected  javax.persistence.EntityManager getEntityManager()
           
 java.lang.String getFormat()
           
static java.lang.String getJpaPersistenceUnitName()
           
 EntityPersistenceMetaData getMetaDataFor(T entity)
           
 PersistenceDefinition getPersistenceDefinition()
           
 java.io.File getRepositoryDirectory()
          The unique file directory in which the repository data files resides.
 java.lang.String getRepositoryId()
          The repository-ID the name of the repository.
<V extends T>
java.util.Iterator<V>
iterateAllEntitiesSpecifiedBy(Specification<V> specification)
          Finds and returns all entities approved by the given specification.
 void load()
          The semantics of this operation depends on the Repository's PersistenceDefinition.
protected  void onMakePartition()
          Template method for partitioning callbacks.
 void persist()
          The semantics of this operation depends on the Repository's PersistenceDefinition.
<V extends T>
void
put(V entity)
          Puts the given entity into this repository.
<V extends T>
java.lang.Boolean
remove(V entity)
          Removes the given entity from this repository.
 java.lang.Long removeAllEntities()
          Extra method

Purges all entities in repository without no further ado.

<V extends T>
java.lang.Long
removeAllEntitiesSpecifiedBy(Specification<V> specification)
          Removes all entities approved by the given specification.
 void removeAllRecordsInTable(java.lang.String tableName)
          Extra method

Purges all records in givan table without no further ado.

<V extends T>
void
update(V entity)
          Updates an entity.
<V extends T>
void
update(V entity, Specification<?> deltaSpecification)
          Updates an entity.
 
Methods inherited from class net.sourceforge.domian.repository.AbstractDomianCoreRepository
createUniqueSpecificationFor, getSynchronizer, makePartition, setSynchronizer
 
Methods inherited from class net.sourceforge.domian.repository.AbstractRepository
count, countAll, countAllEntitiesSpecifiedBy, find, findAll, findSingle, isIndexingEntitiesRecursively, isPartitioningNatively, iterate, iterateAll, putAll, remove, removeAll
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sourceforge.domian.repository.Repository
count, countAll, countAllEntitiesSpecifiedBy, find, findAll, findSingle, isIndexingEntitiesRecursively, isPartitioningNatively, iterate, iterateAll, makePartition, putAll, remove, removeAll
 

Field Detail

rdbms

protected net.sourceforge.domian.repository.Rdbms rdbms

rdbmsServerName

protected java.lang.String rdbmsServerName

repositoryRootDirectoryString

protected java.lang.String repositoryRootDirectoryString

repositoryId

protected java.lang.String repositoryId

configurationMap

protected java.util.Map<java.lang.String,java.lang.String> configurationMap

repositoryDirectoryString

protected java.lang.String repositoryDirectoryString

repositoryDirectory

protected java.io.File repositoryDirectory

entityManagerFactory

protected javax.persistence.EntityManagerFactory entityManagerFactory

tableNameList

protected java.util.List<java.lang.String> tableNameList

entityManager

protected final java.lang.ThreadLocal<javax.persistence.EntityManager> entityManager
Constructor Detail

HibernateRepository

public HibernateRepository(net.sourceforge.domian.repository.Rdbms rdbms,
                           java.lang.String repositoryId)

HibernateRepository

public HibernateRepository(net.sourceforge.domian.repository.Rdbms rdbms,
                           java.lang.String rdbmsServerName,
                           java.lang.String repositoryRootDirectoryString,
                           java.lang.String repositoryId)

HibernateRepository

public HibernateRepository(net.sourceforge.domian.repository.Rdbms rdbms,
                           java.lang.String rdbmsServerName,
                           java.lang.String repositoryRootDirectoryString,
                           java.lang.String repositoryId,
                           java.util.Map<java.lang.String,java.lang.String> configurationMap)
Method Detail

getJpaPersistenceUnitName

public static java.lang.String getJpaPersistenceUnitName()

getEntityManager

protected javax.persistence.EntityManager getEntityManager()

onMakePartition

protected void onMakePartition()
Description copied from class: AbstractRepository
Template method for partitioning callbacks.

Overrides:
onMakePartition in class AbstractDomianCoreRepository<T extends Entity>

contains

protected java.lang.Boolean contains(T entity)
Overrides:
contains in class AbstractRepository<T extends Entity>

iterateAllEntitiesSpecifiedBy

public <V extends T> java.util.Iterator<V> iterateAllEntitiesSpecifiedBy(Specification<V> specification)
Description copied from interface: Repository
Finds and returns all entities approved by the given specification. An iterator is returned for lazy retrieval of the entities.

Specified by:
iterateAllEntitiesSpecifiedBy in interface Repository<T extends Entity>
Parameters:
specification - the entity specification
Returns:
an iterator with all entities approved by the specification argument

findAllEntitiesSpecifiedBy

public <V extends T> java.util.Collection<V> findAllEntitiesSpecifiedBy(Specification<V> specification)
Description copied from interface: Repository
Finds and returns all entities approved by the given specification.

Specified by:
findAllEntitiesSpecifiedBy in interface Repository<T extends Entity>
Parameters:
specification - the entity specification
Returns:
a collection of all entities approved by the specification argument, or an empty set if no entities are found and approved

findSingleEntitySpecifiedBy

public <V extends T> V findSingleEntitySpecifiedBy(Specification<V> specification)
Description copied from interface: Repository
Finds and returns one single entity approved by the given specification.

Specified by:
findSingleEntitySpecifiedBy in interface Repository<T extends Entity>
Overrides:
findSingleEntitySpecifiedBy in class AbstractRepository<T extends Entity>
Parameters:
specification - the entity specification
Returns:
the sole entity specified by the specification argument, or null if no entity is found

put

public <V extends T> void put(V entity)
Description copied from interface: Repository
Puts the given entity into this repository.

Specified by:
put in interface Repository<T extends Entity>
Parameters:
entity - the entity to be stored

update

public <V extends T> void update(V entity)
Description copied from interface: Repository
Updates an entity. When an entity is (re)-retrieved, all state changes will be visible, no matter what kinds of repository implementations being involved. Do notice that for all memory-based repositories (and possibly some delegated ones), entity updates are of course immediately visible.

(Only applicable for persistent repositories doing explicit data writes, like PersistenceDefinition.FILE, and possibly PersistenceDefinition.DELEGATED... For all other persistence definitions, this method is redundant/not applicable. Anyway, by applying this method in a consistent manner, an eventual repository migration will not need any code changes.

Specified by:
update in interface Repository<T extends Entity>

update

public <V extends T> void update(V entity,
                                 Specification<?> deltaSpecification)
Description copied from interface: Repository
Updates an entity. When an entity is (re)-retrieved, all state changes will be visible, no matter what kinds of repository implementations being involved. Do notice that for all memory-based repositories (and possibly some delegated ones), entity updates are of course immediately visible.

(Only applicable for persistent repositories doing direct data writes, like PersistenceDefinition.FILE, and possibly PersistenceDefinition.DELEGATED... For all other persistence definitions, this method is redundant/not applicable. Anyway, by applying this method in a consistent manner, an eventual repository migration will not need any code changes.

Specified by:
update in interface Repository<T extends Entity>
Overrides:
update in class AbstractRepository<T extends Entity>
Type Parameters:
V - the entity type
Parameters:
entity - the entity
deltaSpecification - for optimistic locking schemes: a specification specifying the differences/added state between this update and the previously retrieved entity version The specification should be a conjuction of field bound equality specifications only, or null if optimistic locking with automatic retries is completely out of scope

removeAllRecordsInTable

public void removeAllRecordsInTable(java.lang.String tableName)
Extra method

Purges all records in givan table without no further ado.


removeAllEntities

public java.lang.Long removeAllEntities()
Extra method

Purges all entities in repository without no further ado.


removeAllEntitiesSpecifiedBy

public <V extends T> java.lang.Long removeAllEntitiesSpecifiedBy(Specification<V> specification)
Description copied from interface: Repository
Removes all entities approved by the given specification.

Specified by:
removeAllEntitiesSpecifiedBy in interface Repository<T extends Entity>
Parameters:
specification - the entity specification
Returns:
the number of removed entities

remove

public <V extends T> java.lang.Boolean remove(V entity)
Description copied from interface: Repository
Removes the given entity from this repository.

Specified by:
remove in interface Repository<T extends Entity>
Parameters:
entity - the entity to be removed
Returns:
true if the entity is found and removed, otherwise false

getRepositoryDirectory

public java.io.File getRepositoryDirectory()
Description copied from interface: PersistentRepository
The unique file directory in which the repository data files resides.

Specified by:
getRepositoryDirectory in interface PersistentRepository<T extends Entity>
Returns:
the repository directory, if applicable

getRepositoryId

public java.lang.String getRepositoryId()
Description copied from interface: PersistentRepository
The repository-ID the name of the repository. It should be unique within a running system.

For file-based repositories, the repository-ID should constitute the last directory element of the repository directory.

Specified by:
getRepositoryId in interface PersistentRepository<T extends Entity>
Returns:
the repository id

getPersistenceDefinition

public PersistenceDefinition getPersistenceDefinition()
Specified by:
getPersistenceDefinition in interface PersistentRepository<T extends Entity>

getFormat

public java.lang.String getFormat()
Specified by:
getFormat in interface PersistentRepository<T extends Entity>

load

public void load()
Description copied from interface: PersistentRepository
The semantics of this operation depends on the Repository's PersistenceDefinition.

Specified by:
load in interface PersistentRepository<T extends Entity>

persist

public void persist()
Description copied from interface: PersistentRepository
The semantics of this operation depends on the Repository's PersistenceDefinition.

Specified by:
persist in interface PersistentRepository<T extends Entity>

getMetaDataFor

public EntityPersistenceMetaData getMetaDataFor(T entity)
Specified by:
getMetaDataFor in interface PersistentRepository<T extends Entity>
Returns:
persistence meta data for given entity

close

public void close()
Description copied from interface: PersistentRepository
Closes the repository and releases all resources.

This method is redundant for certain repository implementations, but by using this method in a consistent manner, an eventual repository migration will not need any code changes.

Specified by:
close in interface PersistentRepository<T extends Entity>


Copyright © 2006-2010. All Rights Reserved.