Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up code for access to Hibernate Sessions.
- Loading branch information
Showing
15 changed files
with
1,216 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package org.fdl.db; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.hibernate.Session; | ||
import org.ncibi.commons.exception.ConstructorCalledError; | ||
|
||
/** | ||
* This class provides factory methods to the various databases at NCIBI. | ||
* | ||
* @author gtarcea | ||
* | ||
*/ | ||
public final class DB | ||
{ | ||
/** | ||
* Only static methods, the constructor should never be called. | ||
*/ | ||
private DB() | ||
{ | ||
throw new ConstructorCalledError(this.getClass()); | ||
} | ||
|
||
/** | ||
* A placeholder class to identify persistence units that have not been initialized. | ||
* | ||
* @author gtarcea | ||
* | ||
*/ | ||
private static class NullPersistenceSession implements PersistenceSession | ||
{ | ||
@Override | ||
public FluentQuery hqlQuery(final String query) | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public FluentQuery hqlQuery(final Session session, final String query) | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public Session session() | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public FluentQuery sqlQuery(final String query) | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public FluentQuery sqlQuery(final Session session, final String query) | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public FluentQuery sqlQuery(final Session session, final String queryName, List<QueryProperty> qp) | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public FluentQuery hqlQuery(final Session session, final String queryName, List<QueryProperty> qp) | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public <T> void delete(final Session session, T what) | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* Used for locking. | ||
*/ | ||
private static final Object lockObject = new Object(); | ||
|
||
/** | ||
* An uninitialized session. | ||
*/ | ||
private static final NullPersistenceSession NULL_PERSISTENCE_SESSION = new NullPersistenceSession(); | ||
|
||
/** | ||
* A list of all known persistence units. | ||
*/ | ||
private static Map<String, PersistenceSession> persistenceUnitsMap = loadPersistenceUnits(); | ||
|
||
/** | ||
* Loads all the persistence unit names and sets them to NULL_PERSISTENCE_SESSION for later (lazy) | ||
* initialization. | ||
* | ||
* @return The map of persistence units. | ||
*/ | ||
private static Map<String, PersistenceSession> loadPersistenceUnits() | ||
{ | ||
final List<String> persistenceUnitNames = PersistenceUnits.getConfigured(); | ||
final Map<String, PersistenceSession> punitMap = new HashMap<String, PersistenceSession>(); | ||
|
||
for (final String punit : persistenceUnitNames) | ||
{ | ||
punitMap.put(punit, NULL_PERSISTENCE_SESSION); | ||
} | ||
return punitMap; | ||
} | ||
|
||
/** | ||
* Retrieves a given persistence unit. | ||
* | ||
* @param punit | ||
* The name of the persistence unit to look up. | ||
* @return The persistence unit. | ||
*/ | ||
public static PersistenceUnit db(final String punit) | ||
{ | ||
PersistenceSession p = persistenceUnitsMap.get(punit); | ||
|
||
if (p == null) | ||
{ | ||
throw new IllegalArgumentException("Unknown Persistence Unit: " + punit); | ||
} | ||
else if (p == NULL_PERSISTENCE_SESSION) | ||
{ | ||
synchronized (lockObject) | ||
{ | ||
/* | ||
* Make sure another thread hasn't initialized the persistence unit | ||
* while we were waiting to acquire the lock. | ||
*/ | ||
p = persistenceUnitsMap.get(punit); | ||
if (p == NULL_PERSISTENCE_SESSION) | ||
{ | ||
final PersistenceSession np = new PersistenceUnit(punit); | ||
persistenceUnitsMap.put(punit, np); | ||
p = np; | ||
} | ||
} | ||
} | ||
|
||
return (PersistenceUnit) p; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package org.fdl.db; | ||
|
||
import java.util.Map; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.Persistence; | ||
|
||
import org.ncibi.commons.config.ProjectConfiguration; | ||
import org.ncibi.commons.db.DBConfig; | ||
import org.ncibi.commons.exception.ConstructorCalledError; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
/** | ||
* Utility class for creating an EntityManager. | ||
* | ||
* @author gtarcea | ||
* | ||
*/ | ||
public final class EntityManagers | ||
{ | ||
/** | ||
* Not instantiable - utility class. | ||
*/ | ||
private EntityManagers() | ||
{ | ||
throw new ConstructorCalledError(this.getClass()); | ||
} | ||
|
||
/** | ||
* Creates a new EntityManager for the given persistenceUnit name. | ||
* | ||
* @param persistenceUnit | ||
* The persistenceUnit to create an EntityManager for. | ||
* @return A new EntityManager. | ||
*/ | ||
public static EntityManager newEntityManager(final String persistenceUnit) | ||
{ | ||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit); | ||
return emf.createEntityManager(); | ||
} | ||
|
||
/** | ||
* Creates a new EntityManager for the given persistence unit using the | ||
* database url, username and password for the connection parameters. | ||
* | ||
* @param persistenceUnit | ||
* The persistenceUnit to create an EntityManager for. | ||
* @param url | ||
* The database url to use. | ||
* @param username | ||
* The database user to use. | ||
* @param password | ||
* The password for the database user. | ||
* @return A new EntityManager. | ||
*/ | ||
public static EntityManager newEntityManager(final String persistenceUnit, final String url, | ||
final String username, final String password) | ||
{ | ||
final Map<String, String> properties = ImmutableMap.of("hibernate.connection.url", url, | ||
"hibernate.connection.username", username, "hibernate.connection.password", | ||
password); | ||
return newEntityManager(persistenceUnit, properties); | ||
} | ||
|
||
/** | ||
* Creates a new EntityManager for the given persistence unit. Obtains the | ||
* database url, username and password from the DBConfig object. | ||
* | ||
* @param persistenceUnit | ||
* The persistenceUnit to create an EntityManager for. | ||
* @param dbConfig | ||
* The DBConfig object used to obtain the database url, username | ||
* and password. | ||
* @return A new EntityManager. | ||
*/ | ||
public static EntityManager newEntityManager(final String persistenceUnit, | ||
final DBConfig dbConfig) | ||
{ | ||
return newEntityManager(persistenceUnit, dbConfig.getDatabaseUrl(), dbConfig | ||
.getDatabaseUsername(), dbConfig.getDatabasePassword()); | ||
} | ||
|
||
/** | ||
* Creates a new EntityManager for the given persistence unit and configures | ||
* it with the given set of properties. | ||
* | ||
* @param persistenceUnit | ||
* The persistenceUnit to create an EntityManager for. | ||
* @param properties | ||
* The properties used to configure the EntityManager. | ||
* @return A new EntityManager. | ||
*/ | ||
public static EntityManager newEntityManager(final String persistenceUnit, | ||
final Map<String, String> properties) | ||
{ | ||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, | ||
properties); | ||
return emf.createEntityManager(); | ||
} | ||
|
||
/** | ||
* Creates a new EntityManager for the given persistence unit obtaining the | ||
* database url, username and password from the global ProjectConfiguration. | ||
* | ||
* @param persistenceUnit | ||
* The persistenceUnit to create an EntityManager for. | ||
* @return A new EntityManager. | ||
*/ | ||
public static EntityManager newEntityManagerFromProject(final String persistenceUnit) | ||
{ | ||
return newEntityManager(persistenceUnit, new DBConfig(ProjectConfiguration.getProject() | ||
.getConfiguration(), persistenceUnit)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.fdl.db; | ||
|
||
import org.hibernate.transform.ResultTransformer; | ||
|
||
/** | ||
* An interface for creating fluent type queries. A Class can implement this interface to create an | ||
* fluent query such as: | ||
* <nl/> | ||
* <code> | ||
* sqlQuery("select * from Provenance").list() | ||
* </code> | ||
* | ||
* @author gtarcea | ||
*/ | ||
public interface FluentQuery | ||
{ | ||
/** | ||
* Sets the ResultTransformer for a query. | ||
* | ||
* @param rt | ||
* The ResultTransformer to use. | ||
* @return FluentQuery. | ||
*/ | ||
public FluentQuery to(ResultTransformer rt); | ||
|
||
/** | ||
* Binds a class (for SQL Queries) to a query. | ||
* | ||
* @param <T> | ||
* The class type. | ||
* @param cls | ||
* The class to bind. | ||
* @return FluentQuery. | ||
*/ | ||
public <T> FluentQuery bind(Class<T> cls); | ||
|
||
/** | ||
* Sets a parameters value. A parameter in a query starts with a ':', for example <code> select * from Provenance where id = :id"</code> | ||
* | ||
* @param param | ||
* The parameter to set. | ||
* @param value | ||
* The value to set it to. | ||
* @return FluentQuery. | ||
*/ | ||
public FluentQuery forParam(String param, Object value); | ||
|
||
/** | ||
* Runs the query returning a list of results. The query is wrapped in a transaction. | ||
* | ||
* @param <T> | ||
* The type of elements to return. | ||
* @return A list of results of type T. | ||
*/ | ||
public <T> T list(); | ||
|
||
/** | ||
* Runs the query returning a single result. The query is wrapped in a transaction. | ||
* | ||
* @param <T> | ||
* The type of element to return. | ||
* @return A result of type T or null if nothing matches the query. | ||
*/ | ||
public <T> T single(); | ||
|
||
} |
Oops, something went wrong.