|
2.8.2: 2008-09-18 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sf.basedb.core.HibernateUtil
public class HibernateUtil
This class collects most of the functionality we need for using Hibernate.
Field Summary | |
---|---|
private static Configuration |
cfg
The Hibernate configuration, including all mappings. |
private static DbEngine |
dbEngine
Our extra database specific sql generation that is not possible by the Hibernate dialect. |
private static Dialect |
dialect
The Hibernate database dialect in use. |
private static boolean |
isInitialised
|
private static Logger |
log
Log core events. |
private static Logger |
logSql
Log all batcher SQL statements. |
private static SessionFactory |
sf
The single SessionFactory. |
Constructor Summary | |
---|---|
HibernateUtil()
|
Method Summary | ||
---|---|---|
private static void |
addColumn(Table hibernateTable,
VirtualColumn column)
Add a column to a table. |
|
private static void |
addExtendedPropertiesMappings(Configuration cfg)
Generate additional mappings for all ExtendableData items. |
|
private static void |
addFilterConditions(Configuration cfg)
Generate filters for Ownable , Shareable , etc. classes
that are used by the Query implementations. |
|
private static void |
addRawDataMappings(Configuration cfg)
Generate additional mappings for the raw data types. |
|
private static void |
addSqlFunctions(Configuration cfg)
Add more SQL functions to HQL that are not present in a standard Hibernate installation. |
|
private static void |
addStaticMappings(Configuration cfg)
Read all mapping files, which must be named Class.hbm.xml where class
is the name of each class. |
|
(package private) static void |
clear(Session session)
Clear the session of cached objects. |
|
(package private) static void |
close(Session session)
Close a session. |
|
(package private) static void |
close(StatelessSession session)
Close a statless session. |
|
(package private) static void |
commit(Transaction tx)
Commit a transaction. |
|
private static Property |
createExtendedProperty(Table t,
ExtendedProperty ep,
boolean extendable)
Add an extended property to the specified Hibernate mapping |
|
(package private) static Query |
createQuery(Session session,
String hql)
Create a Hibernate query. |
|
(package private) static Query |
createQuery(StatelessSession session,
String hql)
Create a Hibernate query using a stateless session. |
|
(package private) static SQLQuery |
createSqlQuery(Session session,
String sql)
Create a Hibernate SQL query. |
|
(package private) static SQLQuery |
createSqlQuery(StatelessSession session,
String sql)
Create a Hibernate SQL query using a stateless session. |
|
(package private) static void |
createStaticTables(boolean update)
Create the all tables in the database. |
|
(package private) static void |
createVirtualTable(VirtualDb db,
VirtualTable table)
Create a table in the dynamic database. |
|
static void |
dbIndexes(boolean verbose,
boolean silent,
boolean dropIndexes,
boolean updateIndexes)
Prints a lot of useful stuff about the database to standard output. |
|
(package private) static void |
deleteData(Session session,
BasicData data)
Delete a data item from the database. |
|
(package private) static void |
disableFilter(Session session,
String name)
Disable the filter with the given name. |
|
(package private) static void |
dropVirtualTable(VirtualDb db,
VirtualTable table)
Drop a table in the dynamic database. |
|
static void |
dynamicDbIndexes(boolean verbose,
boolean silent,
boolean dropIndexes)
Prints a lot of useful stuff about the dynamic database to standard output. |
|
(package private) static Filter |
enableFilter(Session session,
String name)
Enable the filter with the given name. |
|
(package private) static void |
evictData(Session session,
BasicData data)
Remove a data item from the internal cache of Hibernate. |
|
(package private) static int |
executeUpdate(Query query)
Exceute an update or delete query. |
|
(package private) static boolean |
exists(Session session,
Class clazz,
int id)
Checks if an item with the specified ID exists in the database. |
|
(package private) static void |
flush(Session session)
Flush all changed objects to the database. |
|
(package private) static PersistentClass |
getClassMapping(String entityName)
|
|
(package private) static Iterator<PersistentClass> |
getClassMappings()
|
|
(package private) static ClassMetadata |
getClassMetadata(String entityName)
|
|
(package private) static Connection |
getConnection(Session session)
Get the underlying JDBC connection from the Hibernate session. |
|
static DbEngine |
getDbEngine()
Get the database engine. |
|
static Dialect |
getDialect()
Get the database dialect. |
|
(package private) static Mapping |
getMapping()
|
|
(package private) static Query |
getPredefinedQuery(Session session,
String name)
Get a predefined HQL query. |
|
(package private) static Query |
getPredefinedQuery(Session session,
String name,
String... replacements)
Get a predefined HQL query. |
|
(package private) static Query |
getPredefinedQuery(StatelessSession session,
String name)
Get a predefined HQL query using the stateless session. |
|
(package private) static Query |
getPredefinedSQLQuery(Session session,
String name)
Get a predefined SQL query. |
|
(package private) static Query |
getPredefinedSQLQuery(StatelessSession session,
String name)
Get a predefined SQL query using the stateless session. |
|
(package private) static Object |
getPropertyValue(BasicData data,
String propertyName)
Get the value of a property on a data object using Hibernate metadata methods. |
|
(package private) static String |
getShortEntityName(String entityName)
Removes the package name part of a class name and any ending 'Data'. |
|
(package private) static void |
init1()
First step of initialising this class. |
|
(package private) static void |
init2()
Second step of initialising this class. |
|
(package private) static void |
initCollection(Session session,
BasicData data,
String collectionName)
Initialise a collection on a data object. |
|
(package private) static boolean |
isEmptyDatabase()
Check if the main database has any tables. |
|
(package private) static
|
loadData(Class<T> clazz,
Query query)
Load a data item from the database using a query. |
|
(package private) static
|
loadData(Session session,
Class<T> clazz,
int id)
Load a data item from the database when you know the id. |
|
(package private) static
|
loadData(StatelessSession session,
Class<T> clazz,
int id)
Load a data item from the database using a statless session when you know the id. |
|
(package private) static
|
loadData(StatelessSession session,
Class<T> clazz,
String entityName,
int id)
Load a data item from the database using a statless session when you know the id. |
|
(package private) static
|
loadIterator(Class<T> clazz,
Query query,
SessionControl sc)
Scroll through the result of a query using an iterator. |
|
(package private) static
|
loadList(Class<T> clazz,
Query query,
SessionControl sc)
Load a List of items from the database
using a query. |
|
(package private) static void |
lockData(Session session,
BasicData data,
LockMode lockMode)
Reconnect a disconnected data object to the session, discarding changes that have been made while the object was disconnected. |
|
private static Table |
newDynamicMapping(VirtualDb db,
VirtualTable table)
Create a new org.hibernate.mapping.Table object for
the specified table. |
|
(package private) static Session |
newSession()
Create a new Hibernate session. |
|
(package private) static StatelessSession |
newStatelessSession(Session session)
|
|
(package private) static Transaction |
newTransaction(Session session)
Create a new transaction for a session. |
|
static String |
quote(String value)
Quote a string with the default quote type for the current database engine. |
|
(package private) static void |
refresh(Session session,
BasicData data)
Reload the data from the database for a given entity. |
|
(package private) static void |
rollback(Transaction tx)
Rollback a transaction. |
|
(package private) static void |
saveData(Session session,
BasicData data)
Save a new data object to the database. |
|
(package private) static void |
saveData(StatelessSession session,
BasicData data)
Save a new data object to the database. |
|
private static void |
setConfigurationProperties(Configuration cfg)
Read database configuration properties from the base.config file and add those to the Hibernate configuration. |
|
private static boolean |
tableExists(String tableName,
Connection c)
Check if table in the dynamic database exists. |
|
(package private) static void |
testTransactions()
|
|
(package private) static void |
unload()
Unload all settings. |
|
(package private) static void |
updateData(Session session,
BasicData data)
Update an existing data object in the database. |
|
static boolean |
useThetaJoin(QueryType queryType)
Check if theta joins or ANSI joins are used by the database. |
|
(package private) static boolean |
virtualTableExists(VirtualDb db,
VirtualTable table)
Check if a table exists in the dynamic database or not. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final Logger log
private static final Logger logSql
private static SessionFactory sf
private static Configuration cfg
private static Dialect dialect
private static DbEngine dbEngine
private static boolean isInitialised
Constructor Detail |
---|
public HibernateUtil()
Method Detail |
---|
static void init1() throws BaseException
Application.start()
method. In this step we load configuration
settings from the 'base.config' and 'hibernate.cfg.xml' files and
create the Dialect and DbEngine objects.
Initialising means that we
load the configuration from the properties and the xml file,
read all mapping files, generate additional mappings for the
ExtendableData
items and raw data, and generate filters
used by Query
implementation.
BaseException
static void init2() throws BaseException
Application.start()
method. In this step we read all mapping files,
generate additional mappings for the ExtendableData
items and raw data,
and generate filters used by Query
implementation.
BaseException
static void unload()
private static void setConfigurationProperties(Configuration cfg) throws BaseException
BaseException
private static void addStaticMappings(Configuration cfg) throws BaseException
Class.hbm.xml
where class
is the name of each class. The must be located in the same directory as the class
files, and we support loading the xml files both from a jar file of from the
directory.
BaseException
private static void addExtendedPropertiesMappings(Configuration cfg)
ExtendableData
items.
private static void addRawDataMappings(Configuration cfg) throws BaseException
BaseException
private static Property createExtendedProperty(Table t, ExtendedProperty ep, boolean extendable)
t
- The table the property should be added toep
- The extended property to add to the mappingextendable
- TRUE if the property belongs to an extendable item, FALSE
otherwise (RawData)private static void addFilterConditions(Configuration cfg) throws BaseException
Ownable
, Shareable
, etc. classes
that are used by the Query
implementations. We are generating the filters
here beacuse we don't want anyone to mess them which could have been the case
if the filter had been defined in the xml mapping files.
BaseException
private static void addSqlFunctions(Configuration cfg)
Configuration.addSqlFunction(String, SQLFunction)
. Note!
The name of the function must be in lower-case letters, otherwise
Hibernate will not be able to find it. In HQL, it is possible to use
both lower-case and uppercase.
Here are the functions registered so far.
static void createStaticTables(boolean update) throws BaseException
SchemaUpdate
tool
provided by Hibernate. The database must exists and we recommend that
it is empty to begin with.
BaseException
static boolean isEmptyDatabase() throws BaseException
BaseException
static void createVirtualTable(VirtualDb db, VirtualTable table) throws BaseException
VirtualTable.getTableName(VirtualDb)
. It is assumed
that the table doesn't already exists. This can be checked by
virtualTableExists(VirtualDb, VirtualTable)
. This method
uses a separate connection to the database because some databases (ie. MySQL)
can't handle transactions properly for this type of SQL statements.
This method uses classes in the org.hibernate.mapping
package
to define a Hibernate representation of the table and uses the
Table.sqlCreateString
method to generate the SQL. This should
ensure that this code works with all database engines supported by Hibernate.
db
- The virtual db itemtable
- Information about the table to create
BaseException
dropVirtualTable(VirtualDb, VirtualTable)
,
virtualTableExists(VirtualDb, VirtualTable)
static void dropVirtualTable(VirtualDb db, VirtualTable table) throws BaseException
VirtualTable.getTableName(VirtualDb)
. If the table
doesn't exists this method does nothing. This method
uses a separate connection to the database because some databases (ie. MySQL)
can't handle transactions properly for this type of SQL statements.
This method uses classes in the org.hibernate.mapping
package
to define a Hibernate representation of the table and uses the
Table.sqlDropString
method to generate the SQL. This should
ensure that this code works with all database engines supported by Hibernate.
db
- The virtual db itemtable
- Information about the table to drop
BaseException
createVirtualTable(VirtualDb, VirtualTable)
,
virtualTableExists(VirtualDb, VirtualTable)
static boolean virtualTableExists(VirtualDb db, VirtualTable table)
VirtualTable.getTableName(VirtualDb)
.
This method uses JDBC DatabaseMetaData
object on a
separate connection to find out if the table exists or not.
db
- The virtual db itemtable
- Information about the tablecreateVirtualTable(VirtualDb, VirtualTable)
,
dropVirtualTable(VirtualDb, VirtualTable)
private static boolean tableExists(String tableName, Connection c) throws SQLException
tableName
- The name of the tablec
- The JDBC connection
SQLException
virtualTableExists(VirtualDb, VirtualTable)
private static Table newDynamicMapping(VirtualDb db, VirtualTable table)
org.hibernate.mapping.Table
object for
the specified table. This method is used by
createVirtualTable(VirtualDb, VirtualTable)
to build the mapping
information Hibernate requires to generate the SQL to create the table.
db
- The virtual db itemtable
- Information about the table to generate the mapping forprivate static void addColumn(Table hibernateTable, VirtualColumn column)
newDynamicMapping(VirtualDb, VirtualTable)
to build the mapping
information Hibernate requires to generate the SQL to create the table.
hibernateTable
- The org.hibernate.mapping.Table
object
that holds the mapping for the tablecolumn
- The column to add to the tablestatic Session newSession() throws BaseException
BaseException
static StatelessSession newStatelessSession(Session session) throws BaseException
BaseException
static Transaction newTransaction(Session session) throws BaseException
BaseException
static Connection getConnection(Session session)
static void commit(Transaction tx) throws BaseException
BaseException
static void rollback(Transaction tx) throws BaseException
BaseException
static void close(Session session)
static void close(StatelessSession session)
static void flush(Session session) throws BaseException
BaseException
clear(Session)
static void clear(Session session) throws BaseException
BaseException
flush(Session)
static void saveData(Session session, BasicData data) throws BaseException
BaseException
static void saveData(StatelessSession session, BasicData data) throws BaseException
BaseException
static void updateData(Session session, BasicData data) throws BaseException
BaseException
static void lockData(Session session, BasicData data, LockMode lockMode) throws BaseException
lockMode
= LockMode.NONE
if
the logged in user only has read permission to the object.
BaseException
DbControl.reattachItem(BasicItem)
static <T> T loadData(Session session, Class<T> clazz, int id) throws BaseException
session
- The Hibernate session which is connected to the
databaseclazz
- An object of this class is returned with the dataid
- The id of the item to load
BaseException
static void refresh(Session session, BasicData data)
session
- The Hibernate session which is connected to the
databasedata
- The entity to reloadstatic boolean exists(Session session, Class clazz, int id)
session
- The Hibernate session which is connected to the
databaseclazz
- The type of the itemid
- The ID of the item
static <T> T loadData(StatelessSession session, Class<T> clazz, int id) throws BaseException
session
- The Hibernate stateless session which is connected
to the databaseclazz
- An object of this class is returned with the dataid
- The id of the item to load
BaseException
static <T> T loadData(StatelessSession session, Class<T> clazz, String entityName, int id) throws BaseException
session
- The Hibernate stateless session which is connected
to the databaseclazz
- An object of this class is returned with the dataentityName
- The entity name of the itemid
- The id of the item to load
BaseException
static <T> T loadData(Class<T> clazz, Query query) throws BaseException
clazz
- An object of this class is returned with the dataquery
- A Query
object which should select one
item of the specified class
BaseException
static int executeUpdate(Query query) throws BaseException
query
- A Query
object which is executed using the
Query.executeUpdate()
method
BaseException
static Object getPropertyValue(BasicData data, String propertyName) throws BaseException
BaseException
Metadata.getPropertyValue(BasicItem, String)
static void initCollection(Session session, BasicData data, String collectionName) throws BaseException
BaseException
DbControl.detachItem(BasicItem)
static void deleteData(Session session, BasicData data) throws BaseException
BaseException
static void evictData(Session session, BasicData data) throws BaseException
BaseException
updateData(Session, BasicData)
,
lockData(Session, BasicData, LockMode)
static Query createQuery(Session session, String hql) throws BaseException
BaseException
static Query createQuery(StatelessSession session, String hql) throws BaseException
BaseException
static SQLQuery createSqlQuery(Session session, String sql) throws BaseException
BaseException
static SQLQuery createSqlQuery(StatelessSession session, String sql) throws BaseException
BaseException
static Filter enableFilter(Session session, String name) throws BaseException
BaseException
static void disableFilter(Session session, String name) throws BaseException
BaseException
static <T> List<T> loadList(Class<T> clazz, Query query, SessionControl sc) throws BaseException
List
of items from the database
using a query.
clazz
- The list should contain objects of this classquery
- The query to executesc
- A optional SessionControl that is automatically protected from
timeouts during the time the query is running
BaseException
static <T> ScrollIterator<T> loadIterator(Class<T> clazz, Query query, SessionControl sc) throws BaseException
clazz
- The iterator returns objects of this classquery
- The query to executesc
- A optional SessionControl that is automatically protected from
timeouts during the time the query is running
BaseException
static Query getPredefinedQuery(Session session, String name) throws BaseException
BaseException
PredefinedQuery
static Query getPredefinedQuery(Session session, String name, String... replacements) throws BaseException
replacements
- In the SQL string we look for {1}, {2}, ... and
replace this with the contents of the string array
BaseException
PredefinedQuery
static Query getPredefinedQuery(StatelessSession session, String name) throws BaseException
BaseException
PredefinedQuery
static Query getPredefinedSQLQuery(Session session, String name) throws BaseException
BaseException
PredefinedQuery
static Query getPredefinedSQLQuery(StatelessSession session, String name) throws BaseException
BaseException
PredefinedQuery
static Iterator<PersistentClass> getClassMappings()
static PersistentClass getClassMapping(String entityName)
static ClassMetadata getClassMetadata(String entityName)
static Mapping getMapping()
public static Dialect getDialect()
public static DbEngine getDbEngine()
public static boolean useThetaJoin(QueryType queryType)
queryType
- Query type to be checked.
public static String quote(String value)
value --> `value`
, and with any ANSI-compatible
database: value --> "value"
. This method uses the
Dialect.openQuote
and Dialect.closeQuote
methods
of the currently installed Hibernate dialect.
value
- The value to quote
getDialect()
static String getShortEntityName(String entityName)
static void testTransactions() throws BaseException
BaseException
public static void dynamicDbIndexes(boolean verbose, boolean silent, boolean dropIndexes)
verbose
- If true, lots of information will be printedsilent
- If true, no information will be printeddropIndexes
- If true, all indexes will be droppedpublic static void dbIndexes(boolean verbose, boolean silent, boolean dropIndexes, boolean updateIndexes)
verbose
- If true, lots of information will be printedsilent
- If true, no information will be printeddropIndexes
- If true, all indexes will be droppedupdateIndexes
- If true, indexes that doesn't already exist will be created
|
2.8.2: 2008-09-18 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |