public class ORMHandler
extends java.lang.Object
Configuration
.
Each ORM is loaded via the mapClass(java.lang.String, java.lang.String, java.util.Set<java.lang.String>, java.util.Set<java.lang.String>)
method. Depending upon the
database dialect, this method may inject specialized nodes into the ORM XML
documents before handing them off to Hibernate. This is how we manage the
use of computed columns for databases which do not allow expressions within
indexes.Modifier and Type | Class and Description |
---|---|
static class |
ORMHandler.TableInfo
Holder for the table info
|
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
ATTR_ACCESS
XML access attribute
|
private static java.lang.String |
ATTR_COLUMN
XML column attribute
|
private static java.lang.String |
ATTR_INSERT
XML insert attribute
|
private static java.lang.String |
ATTR_NAME
XML name attribute
|
private static java.lang.String |
ATTR_UPDATE
XML update attribute
|
private static javax.xml.namespace.QName |
CNAME
QName of the 'name' attribute
|
private Database |
database
P2J database
|
private P2JDialect |
dialect
The dialect used by this handler.
|
private static java.lang.String |
ELEM_MANY_TO_ONE
XML many-to-one element
|
private static java.lang.String |
ELEM_ONE_TO_ONE
XML one-to-one element
|
private static java.lang.String |
ELEM_PROPERTY
XML property element
|
private org.xml.sax.EntityResolver |
entityResolver
Entity resolver used when parsing XML
|
(package private) static java.lang.String |
FK_SUFFIX
Foreign-key column name suffix
|
private java.util.Set<org.hibernate.internal.util.xml.XmlDocument> |
hibernateMappings
Stored Hibernate mappings.
|
private static java.util.logging.Logger |
LOG
Logger
|
private static java.lang.String |
NOP_ACCESSOR
NopPropertyAccessor fully qualified class name
|
private boolean |
removeForeignKeys
This flag determines if the foreign keys should be removed or not
|
private org.xml.sax.EntityResolver |
streamEntityResolver
Instance of entity resolver.
|
private static javax.xml.namespace.QName |
TNAME
QName of the 'table' attribute
|
private static javax.xml.namespace.QName |
UNAME
QName of the 'name' usage
|
private org.hibernate.internal.util.xml.XMLHelper |
xmlHelper
Instance of XML helper.
|
Constructor and Description |
---|
ORMHandler(P2JDialect dialect,
Database database)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
static void |
applyHibernateMappings(java.util.Set<org.hibernate.internal.util.xml.XmlDocument> mappings,
org.hibernate.cfg.Configuration configuration)
Apply specified Hibernate mappings to the given Hibernate configuration.
|
(package private) static ORMHandler.TableInfo |
getBackingTableInfo(java.lang.Class<?> dmoClass,
org.xml.sax.EntityResolver resolver)
Retrieve the database table info for a given DMO implementation class, by reading it
directly from the .hbm.xml document.
|
java.util.Set<org.hibernate.internal.util.xml.XmlDocument> |
getHibernateMappings()
Get stored Hibernate mappings.
|
private java.lang.String |
getORMLoc(java.lang.Class<?> dmoClass)
This method gets the ORM file location for the given DMO class.
|
private void |
injectComputedColumnsAndProcessFK(java.util.Set<java.lang.String> indexedColumns,
java.util.Set<java.lang.String> caseSensitiveCols,
java.io.InputStream in,
java.lang.String ormLoc)
For each DMO character property which corresponds with a column
participating in a database index, inject a special property node into
the ORM XML document for the given class.
|
private org.w3c.dom.Document |
loadHBMFile(java.io.InputStream in)
This method loads and parses the specified hbm.xml.
|
(package private) boolean |
mapClass(java.lang.String schema,
java.lang.Class<?> dmoClass)
Configure the specified DMO class, updating the object to relational
mapping to inject computed column property nodes if the database dialect
requires it.
|
(package private) void |
mapClass(java.lang.String schema,
java.io.InputStream in,
java.lang.String ormLoc,
java.util.Set<java.lang.String> indexedCols,
java.util.Set<java.lang.String> caseSensitiveCols)
Configure the DMO class by reading its definition from the specified
stream, updating the object to relational mapping to inject computed
column property nodes if the database dialect requires it.
|
void |
mapClass(java.lang.String schema,
java.lang.String ormLoc,
java.util.Set<java.lang.String> indexedCols,
java.util.Set<java.lang.String> caseSensitiveCols)
Configure the DMO class by reading its definition from the specified ORM file, updating the
object to relational mapping to inject computed column property nodes if the database
dialect requires it.
|
private org.w3c.dom.Document |
removeForeignKeys(java.io.InputStream in,
java.lang.String ormLoc)
Remove the foreign keys from the associated hbm.xml ORM file associated
with the given DMO, and return the document.
|
private boolean |
removeForeignKeys(org.w3c.dom.Node parent)
This method walks the DOM tree and removes the following nodes, when
a node with a
column attribute containing a value which
ends with the __fk suffix is encountered:
many-to-one nodes
one-to-one nodes
if there is a column attribute with its value ending
with the __fk suffix, remove the parent node if
this node is not a many-to-one or one-to-one
node; this ensures that all collections - set, map, etc -
are removed. |
private void |
storeMappingDoc(org.w3c.dom.Document doc)
Read the Hibernate mappings from the given document and store them.
|
private void |
storeMappingStream(java.io.InputStream inputStream)
Read the Hibernate mappings from the given input stream and store them.
|
private static org.w3c.dom.Node |
visitChild(java.util.Set<java.lang.String> indexedColumns,
java.util.Set<java.lang.String> caseSensitiveCols,
org.w3c.dom.Node parent,
org.w3c.dom.Node child,
P2JDialect dialect)
Visit an XML node and possibly inject a computed column property element directly after it.
|
(package private) static void |
walkChildren(java.util.Set<java.lang.String> indexedColumns,
java.util.Set<java.lang.String> caseSensitiveCols,
org.w3c.dom.Node node,
P2JDialect dialect)
Walk the children of an XML element node, testing each to determine whether it represents a
property which corresponds with a text column which participates in a database index.
|
static final java.lang.String FK_SUFFIX
private static final java.util.logging.Logger LOG
private static final java.lang.String ELEM_PROPERTY
private static final java.lang.String ELEM_MANY_TO_ONE
private static final java.lang.String ELEM_ONE_TO_ONE
private static final java.lang.String ATTR_NAME
private static final java.lang.String ATTR_COLUMN
private static final java.lang.String ATTR_ACCESS
private static final java.lang.String ATTR_INSERT
private static final java.lang.String ATTR_UPDATE
private static final java.lang.String NOP_ACCESSOR
private static final javax.xml.namespace.QName CNAME
private static final javax.xml.namespace.QName TNAME
private static final javax.xml.namespace.QName UNAME
private final Database database
private final org.xml.sax.EntityResolver entityResolver
private boolean removeForeignKeys
private final org.hibernate.internal.util.xml.XMLHelper xmlHelper
private final java.util.Set<org.hibernate.internal.util.xml.XmlDocument> hibernateMappings
private org.xml.sax.EntityResolver streamEntityResolver
private final P2JDialect dialect
public ORMHandler(P2JDialect dialect, Database database)
dialect
- P2J database dialect.database
- Database whose ORM mappings require loading.public static void applyHibernateMappings(java.util.Set<org.hibernate.internal.util.xml.XmlDocument> mappings, org.hibernate.cfg.Configuration configuration)
mappings
- Hibernate mappings to apply (a set of loaded XML documents).configuration
- Hibernate configuration to which the mappings are applied.static ORMHandler.TableInfo getBackingTableInfo(java.lang.Class<?> dmoClass, org.xml.sax.EntityResolver resolver) throws PersistenceException
dmoClass
- DMO implementation class to which the table is mapped via ORM.resolver
- Entity resolver.PersistenceException
private static org.w3c.dom.Node visitChild(java.util.Set<java.lang.String> indexedColumns, java.util.Set<java.lang.String> caseSensitiveCols, org.w3c.dom.Node parent, org.w3c.dom.Node child, P2JDialect dialect)
indexedColumns
- Set of those character column names which correspond with columns participating
in database indexes.caseSensitiveCols
- The set of case-sensitive character columns that participate in the index.parent
- Visited node's parent node.child
- XML node being visited.null
if no node was injected.public java.util.Set<org.hibernate.internal.util.xml.XmlDocument> getHibernateMappings()
public void mapClass(java.lang.String schema, java.lang.String ormLoc, java.util.Set<java.lang.String> indexedCols, java.util.Set<java.lang.String> caseSensitiveCols) throws PersistenceException, java.io.FileNotFoundException
schema
- Database schema.ormLoc
- The ORM file location.indexedCols
- A set containing the names of the indexed character columns.caseSensitiveCols
- A set containing the names of the indexed, case-sensitive character columns.PersistenceException
- if there is an error injecting computed column property nodes into the ORM XML.java.io.FileNotFoundException
- if the ORM file was not found.static void walkChildren(java.util.Set<java.lang.String> indexedColumns, java.util.Set<java.lang.String> caseSensitiveCols, org.w3c.dom.Node node, P2JDialect dialect)
This method calls itself recursively.
indexedColumns
- Set of those character column names which correspond with
columns participating in database indexes.caseSensitiveCols
- The set of case-sensitive character columns that participate in the index.node
- XML parent node whose children are to be visited.boolean mapClass(java.lang.String schema, java.lang.Class<?> dmoClass) throws PersistenceException
schema
- Database schema.dmoClass
- DMO implementation class.true
if the corresponding table is read-onlyPersistenceException
- if there is an error injecting computed column property nodes
into the ORM XML.void mapClass(java.lang.String schema, java.io.InputStream in, java.lang.String ormLoc, java.util.Set<java.lang.String> indexedCols, java.util.Set<java.lang.String> caseSensitiveCols) throws PersistenceException
schema
- Database schema.in
- The ORM input stream.ormLoc
- The ORM file location.indexedCols
- A set containing the names of the indexed character columns.caseSensitiveCols
- The set of case-sensitive character columns that participate in the index.PersistenceException
- if there is an error injecting computed column property nodes into the ORM XML.private void injectComputedColumnsAndProcessFK(java.util.Set<java.lang.String> indexedColumns, java.util.Set<java.lang.String> caseSensitiveCols, java.io.InputStream in, java.lang.String ormLoc) throws PersistenceException
NopPropertyAccessor
is used to intercept
calls to get and set the property's value in the DMO. The element is
injected directly after the real property node. The injected element
uses the same name as the real property, only with a leading prefix
prescribed by the corresponding database dialect.
Also, if the removeForeignKeys
flag is set to true
,
all foreign key mappings are removed from the associated hbm.xml file.
indexedColumns
- Set of those character column names which correspond with
columns participating in database indexes.caseSensitiveCols
- The set of case-sensitive character columns that participate in the index.in
- The ORM input stream.ormLoc
- The ORM file location.PersistenceException
- if there is an error injecting computed column property nodes
into the ORM XML.private java.lang.String getORMLoc(java.lang.Class<?> dmoClass)
dmoClass
- The DMO class for which to load and parse the hbm.xml file.private org.w3c.dom.Document loadHBMFile(java.io.InputStream in) throws java.lang.Exception
in
- The ORM stream.java.lang.Exception
- If could not load or parse the hbm.xml file.private org.w3c.dom.Document removeForeignKeys(java.io.InputStream in, java.lang.String ormLoc) throws PersistenceException
in
- The ORM input stream.ormLoc
- The ORM file location.PersistenceException
- if there is an error removing the foreign key nodes from the
ORM XML.private boolean removeForeignKeys(org.w3c.dom.Node parent)
column
attribute containing a value which
ends with the __fk
suffix is encountered:
many-to-one
nodes
one-to-one
nodes
column
attribute with its value ending
with the __fk
suffix, remove the parent node if
this node is not a many-to-one
or one-to-one
node; this ensures that all collections - set, map, etc -
are removed.
parent
- The parent node from which to start removing the foreign-key nodes.true
if the parent
node should be removed.private void storeMappingStream(java.io.InputStream inputStream)
inputStream
- Input stream containing Hibernate mappings.private void storeMappingDoc(org.w3c.dom.Document doc)
doc
- Document containing Hibernate mappings.