|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.basedb.core.HibernateUtil
public class HibernateUtil
This class collects most of the functionality we need for using Hibernate.
Field Summary | |
---|---|
private static org.hibernate.cfg.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 org.hibernate.dialect.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 org.hibernate.cfg.Mappings |
mappings
The mappings related to a configuration. |
private static org.hibernate.SessionFactory |
sf
The single SessionFactory. |
Constructor Summary | |
---|---|
HibernateUtil()
|
Method Summary | ||
---|---|---|
private static void |
addColumn(org.hibernate.cfg.Mappings mappings,
org.hibernate.mapping.Table hibernateTable,
VirtualColumn column)
Add a column to a table. |
|
private static void |
addExtendedProperties(Element classTag,
String className)
Append <property> tags to the given class tag. |
|
private static void |
addFilterConditions(Element classTag,
String className)
Add <filter> conditions to the given class tag. |
|
private static void |
addRawDataMappings(org.hibernate.cfg.Configuration cfg)
Generate additional mappings for the raw data types. |
|
private static void |
addSqlFunctions(org.hibernate.cfg.Configuration cfg,
DbEngine dbEngine)
Add more SQL functions to HQL that are not present in a standard Hibernate installation. |
|
private static void |
addStaticMappings(org.hibernate.cfg.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(org.hibernate.Session session)
Clear the session of cached objects. |
|
(package private) static void |
close(org.hibernate.Session session)
Close a session. |
|
(package private) static void |
close(org.hibernate.StatelessSession session)
Close a statless session. |
|
(package private) static void |
commit(org.hibernate.Transaction tx)
Commit a transaction. |
|
private static Element |
createFilterElement(String name,
String condition)
Create a <filter> tag using the given name and condition. |
|
private static Element |
createPropertyElement(ExtendedProperty property)
Creates a <property> tag from an extended property definition. |
|
(package private) static org.hibernate.Query |
createQuery(org.hibernate.Session session,
String hql)
Create a Hibernate query. |
|
(package private) static org.hibernate.Query |
createQuery(org.hibernate.StatelessSession session,
String hql)
Create a Hibernate query using a stateless session. |
|
(package private) static org.hibernate.SQLQuery |
createSqlQuery(org.hibernate.Session session,
String sql)
Create a Hibernate SQL query. |
|
(package private) static org.hibernate.SQLQuery |
createSqlQuery(org.hibernate.StatelessSession session,
String sql)
Create a Hibernate SQL query using a stateless session. |
|
(package private) static void |
createStaticTables(boolean update,
ProgressReporter progress)
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(org.hibernate.Session session,
BasicData data)
Delete a data item from the database. |
|
(package private) static void |
disableFilter(org.hibernate.Session session,
String name)
Disable the filter with the given name. |
|
(package private) static
|
doJdbcWork(org.hibernate.Session session,
JdbcWork<R> work)
Execute some arbitrary JDBC code using the same database connection as the Hibernate session. |
|
(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 org.hibernate.Filter |
enableFilter(org.hibernate.Session session,
String name)
Enable the filter with the given name. |
|
(package private) static void |
evictData(org.hibernate.Session session,
BasicData data)
Remove a data item from the internal cache of Hibernate. |
|
(package private) static int |
executeUpdate(org.hibernate.Query query)
Exceute an update or delete query. |
|
(package private) static boolean |
exists(org.hibernate.Session session,
Class clazz,
int id)
Checks if an item with the specified ID exists in the database. |
|
private static void |
fixUnitSymbolColumnDefinition(Element classTag)
Fixes the definition of the UnitSymbols.symbol column so that it is case-sensitive. |
|
(package private) static void |
flush(org.hibernate.Session session)
Flush all changed objects to the database. |
|
(package private) static org.hibernate.mapping.PersistentClass |
getClassMapping(String entityName)
|
|
(package private) static Iterator<org.hibernate.mapping.PersistentClass> |
getClassMappings()
|
|
(package private) static org.hibernate.metadata.ClassMetadata |
getClassMetadata(String entityName)
|
|
(package private) static Connection |
getConnection(org.hibernate.Session session)
Get the underlying JDBC connection from the Hibernate session. |
|
static DbEngine |
getDbEngine()
Get the database engine. |
|
static org.hibernate.dialect.Dialect |
getDialect()
Get the database dialect. |
|
(package private) static org.hibernate.engine.Mapping |
getMapping()
|
|
(package private) static org.hibernate.Query |
getPredefinedQuery(org.hibernate.Session session,
String name)
Get a predefined HQL query. |
|
(package private) static org.hibernate.Query |
getPredefinedQuery(org.hibernate.Session session,
String name,
String... replacements)
Get a predefined HQL query. |
|
(package private) static org.hibernate.Query |
getPredefinedQuery(org.hibernate.StatelessSession session,
String name)
Get a predefined HQL query using the stateless session. |
|
(package private) static org.hibernate.Query |
getPredefinedSQLQuery(org.hibernate.Session session,
String name,
String... replacements)
Get a predefined SQL query. |
|
(package private) static org.hibernate.Query |
getPredefinedSQLQuery(org.hibernate.StatelessSession session,
String name)
Get a predefined SQL query using the stateless session. |
|
(package private) static org.hibernate.SessionFactory |
getSessionFactory()
Get the configured session factory. |
|
(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(org.hibernate.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,
org.hibernate.Query query)
Load a data item from the database using a query. |
|
(package private) static
|
loadData(org.hibernate.Session session,
Class<T> clazz,
int id)
Load a data item from the database when you know the id. |
|
(package private) static
|
loadData(org.hibernate.Session session,
Class<T> clazz,
String entityName,
int id)
Load a data item from the database when you know the id. |
|
(package private) static
|
loadData(org.hibernate.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(org.hibernate.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,
org.hibernate.Query query,
SessionControl sc)
Scroll through the result of a query using an iterator. |
|
(package private) static
|
loadList(Class<T> clazz,
org.hibernate.Query query,
SessionControl sc)
Load a List of items from the database
using a query. |
|
(package private) static void |
lockData(org.hibernate.Session session,
BasicData data,
org.hibernate.LockOptions lockOptions)
Reconnect a disconnected data object to the session, discarding changes that have been made while the object was disconnected. |
|
private static void |
manipulateDocument(Document dom)
Some XML files require manipulation by us before they are registered. |
|
private static org.hibernate.mapping.Table |
newDynamicMapping(VirtualDb db,
VirtualTable table)
Create a new org.hibernate.mapping.Table object for
the specified table. |
|
(package private) static org.hibernate.Session |
newSession()
Create a new Hibernate session. |
|
(package private) static org.hibernate.Session |
newSession(org.hibernate.Interceptor interceptor)
Create a new Hibernate session, optionally with an interceptor. |
|
(package private) static org.hibernate.StatelessSession |
newStatelessSession(org.hibernate.Session session)
|
|
(package private) static org.hibernate.Transaction |
newTransaction(org.hibernate.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(org.hibernate.Session session,
BasicData data)
Reload the data from the database for a given entity. |
|
(package private) static void |
rollback(org.hibernate.Transaction tx)
Rollback a transaction. |
|
(package private) static void |
saveData(org.hibernate.Session session,
BasicData data)
Save a new data object to the database. |
|
(package private) static void |
saveData(org.hibernate.StatelessSession session,
BasicData data)
Save a new data object to the database. |
|
private static void |
setConfigurationProperties(org.hibernate.cfg.Configuration cfg)
Read database configuration properties from the base.config file and add those to the Hibernate configuration. |
|
private static boolean |
tableExists(String tableName,
org.hibernate.Session session)
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(org.hibernate.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 org.hibernate.SessionFactory sf
private static org.hibernate.cfg.Configuration cfg
private static org.hibernate.cfg.Mappings mappings
private static org.hibernate.dialect.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(org.hibernate.cfg.Configuration cfg) throws BaseException
BaseException
private static void addStaticMappings(org.hibernate.cfg.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 manipulateDocument(Document dom)
private static void addExtendedProperties(Element classTag, String className)
classTag
- An Element representing a <class> tagclassName
- The name of the (ExtendableData
) classprivate static Element createPropertyElement(ExtendedProperty property)
private static void addFilterConditions(Element classTag, String className)
classTag
- An Element representing a <class> tagclassName
- The name of the entity classprivate static Element createFilterElement(String name, String condition)
private static void addRawDataMappings(org.hibernate.cfg.Configuration cfg) throws BaseException
BaseException
private static void addSqlFunctions(org.hibernate.cfg.Configuration cfg, DbEngine dbEngine)
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.
private static void fixUnitSymbolColumnDefinition(Element classTag)
static void createStaticTables(boolean update, ProgressReporter progress) throws BaseException
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, org.hibernate.Session session) throws SQLException
tableName
- The name of the tablesession
- The Hibernate session
SQLException
virtualTableExists(VirtualDb, VirtualTable)
private static org.hibernate.mapping.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(org.hibernate.cfg.Mappings mappings, org.hibernate.mapping.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 org.hibernate.Session newSession() throws BaseException
BaseException
static org.hibernate.Session newSession(org.hibernate.Interceptor interceptor) throws BaseException
BaseException
static org.hibernate.StatelessSession newStatelessSession(org.hibernate.Session session) throws BaseException
BaseException
static org.hibernate.Transaction newTransaction(org.hibernate.Session session) throws BaseException
BaseException
static Connection getConnection(org.hibernate.Session session)
doJdbcWork(Session, JdbcWork)
instead.
Other code may continue using this method until the Hibernate team develops a full replacement. See http://forum.hibernate.org/viewtopic.php?t=974518
doJdbcWork(Session, JdbcWork)
static org.hibernate.SessionFactory getSessionFactory()
static <R> R doJdbcWork(org.hibernate.Session session, JdbcWork<R> work) throws SQLException
getConnection(Session)
wherever possible.
session
- The Hibernate session to usework
- The work implementation
SQLException
- In case there is an
SQL errorstatic void commit(org.hibernate.Transaction tx) throws BaseException
BaseException
static void rollback(org.hibernate.Transaction tx) throws BaseException
BaseException
static void close(org.hibernate.Session session)
static void close(org.hibernate.StatelessSession session)
static void flush(org.hibernate.Session session) throws BaseException
BaseException
clear(Session)
static void clear(org.hibernate.Session session) throws BaseException
BaseException
flush(Session)
static void saveData(org.hibernate.Session session, BasicData data) throws BaseException
BaseException
static void saveData(org.hibernate.StatelessSession session, BasicData data) throws BaseException
BaseException
static void updateData(org.hibernate.Session session, BasicData data) throws BaseException
BaseException
static void lockData(org.hibernate.Session session, BasicData data, org.hibernate.LockOptions lockOptions) throws BaseException
lockOptions
= LockOptions.NONE
if
the logged in user only has read permission to the object.
BaseException
DbControl.reattachItem(BasicItem, boolean)
static <T> T loadData(org.hibernate.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 <T> T loadData(org.hibernate.Session session, Class<T> clazz, String entityName, int id) throws BaseException
session
- The Hibernate 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 void refresh(org.hibernate.Session session, BasicData data)
session
- The Hibernate session which is connected to the
databasedata
- The entity to reloadstatic boolean exists(org.hibernate.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(org.hibernate.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(org.hibernate.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, org.hibernate.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(org.hibernate.Query query) throws BaseException
query
- A Query
object which is executed using the
Query.executeUpdate()
method
BaseException
static void initCollection(org.hibernate.Session session, BasicData data, String collectionName) throws BaseException
BaseException
DbControl.detachItem(BasicItem)
static void deleteData(org.hibernate.Session session, BasicData data) throws BaseException
BaseException
static void evictData(org.hibernate.Session session, BasicData data) throws BaseException
BaseException
updateData(Session, BasicData)
,
lockData(Session, BasicData, LockOptions)
static org.hibernate.Query createQuery(org.hibernate.Session session, String hql) throws BaseException
BaseException
static org.hibernate.Query createQuery(org.hibernate.StatelessSession session, String hql) throws BaseException
BaseException
static org.hibernate.SQLQuery createSqlQuery(org.hibernate.Session session, String sql) throws BaseException
BaseException
static org.hibernate.SQLQuery createSqlQuery(org.hibernate.StatelessSession session, String sql) throws BaseException
BaseException
static org.hibernate.Filter enableFilter(org.hibernate.Session session, String name) throws BaseException
BaseException
static void disableFilter(org.hibernate.Session session, String name) throws BaseException
BaseException
static <T> List<T> loadList(Class<T> clazz, org.hibernate.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, org.hibernate.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 org.hibernate.Query getPredefinedQuery(org.hibernate.Session session, String name) throws BaseException
BaseException
PredefinedQuery
static org.hibernate.Query getPredefinedQuery(org.hibernate.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 org.hibernate.Query getPredefinedQuery(org.hibernate.StatelessSession session, String name) throws BaseException
BaseException
PredefinedQuery
static org.hibernate.Query getPredefinedSQLQuery(org.hibernate.Session session, String name, String... replacements) throws BaseException
BaseException
PredefinedQuery
static org.hibernate.Query getPredefinedSQLQuery(org.hibernate.StatelessSession session, String name) throws BaseException
BaseException
PredefinedQuery
static Iterator<org.hibernate.mapping.PersistentClass> getClassMappings()
static org.hibernate.mapping.PersistentClass getClassMapping(String entityName)
static org.hibernate.metadata.ClassMetadata getClassMetadata(String entityName)
static org.hibernate.engine.Mapping getMapping()
public static org.hibernate.dialect.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.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |