| Method from org.hibernate.cfg.AnnotationConfiguration Detail: |
protected void add(Document doc) throws MappingException {
boolean ejb3Xml = "entity-mappings".equals( doc.getRootElement().getName() );
if ( inSecondPass ) {
//if in second pass bypass the queueing, getExtendedQueue reuse this method
if ( !ejb3Xml ) super.add( doc );
}
else {
if ( !ejb3Xml ) {
final Element hmNode = doc.getRootElement();
Attribute packNode = hmNode.attribute( "package" );
String defaultPackage = packNode != null
? packNode.getValue()
: "";
Set< String > entityNames = new HashSet< String >();
findClassNames( defaultPackage, hmNode, entityNames );
for (String entity : entityNames) {
hbmEntities.put( entity, doc );
}
hbmDocuments.add( doc );
}
else {
List< String > classnames = ( (EJB3ReflectionManager) reflectionManager ).getXMLContext().addDocument( doc );
for (String classname : classnames) {
try {
annotatedClasses.add( reflectionManager.classForName( classname, this.getClass() ) );
}
catch (ClassNotFoundException e) {
throw new AnnotationException( "Unable to load class defined in XML: " + classname, e );
}
}
}
}
}
|
public AnnotationConfiguration addAnnotatedClass(Class persistentClass) throws MappingException {
XClass persistentXClass = reflectionManager.toXClass( persistentClass );
try {
annotatedClasses.add( persistentXClass );
return this;
}
catch (MappingException me) {
log.error( "Could not compile the mapping annotations", me );
throw me;
}
}
Read a mapping from the class annotation metadata (JSR 175). |
public AnnotationConfiguration addCacheableFile(File xmlFile) throws MappingException {
super.addCacheableFile( xmlFile );
return this;
}
|
public AnnotationConfiguration addCacheableFile(String xmlFile) throws MappingException {
super.addCacheableFile( xmlFile );
return this;
}
|
public AnnotationConfiguration addClass(Class persistentClass) throws MappingException {
super.addClass( persistentClass );
return this;
}
|
public AnnotationConfiguration addDirectory(File dir) throws MappingException {
super.addDirectory( dir );
return this;
}
|
public AnnotationConfiguration addDocument(Document doc) throws MappingException {
super.addDocument( doc );
return this;
}
|
public AnnotationConfiguration addFile(String xmlFile) throws MappingException {
super.addFile( xmlFile );
return this;
}
|
public AnnotationConfiguration addFile(File xmlFile) throws MappingException {
super.addFile( xmlFile );
return this;
}
|
public AnnotationConfiguration addInputStream(InputStream xmlInputStream) throws MappingException {
try {
List errors = new ArrayList();
SAXReader saxReader = xmlHelper.createSAXReader( "XML InputStream", errors, getEntityResolver() );
try {
saxReader.setFeature( "http://apache.org/xml/features/validation/schema", true );
//saxReader.setFeature( "http://apache.org/xml/features/validation/dynamic", true );
//set the default schema locators
saxReader.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation",
"http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
);
}
catch (SAXException e) {
saxReader.setValidation( false );
}
org.dom4j.Document doc = saxReader
.read( new InputSource( xmlInputStream ) );
if ( errors.size() != 0 ) {
throw new MappingException( "invalid mapping", (Throwable) errors.get( 0 ) );
}
add( doc );
return this;
}
catch (DocumentException e) {
throw new MappingException( "Could not parse mapping document in input stream", e );
}
finally {
try {
xmlInputStream.close();
}
catch (IOException ioe) {
log.warn( "Could not close input stream", ioe );
}
}
}
|
public AnnotationConfiguration addJar(File jar) throws MappingException {
super.addJar( jar );
return this;
}
|
public AnnotationConfiguration addPackage(String packageName) throws MappingException {
log.info( "Mapping package {}", packageName );
try {
AnnotationBinder.bindPackage( packageName, createExtendedMappings() );
return this;
}
catch (MappingException me) {
log.error( "Could not compile the mapping annotations", me );
throw me;
}
}
Read package level metadata |
public AnnotationConfiguration addProperties(Properties extraProperties) {
super.addProperties( extraProperties );
return this;
}
|
public AnnotationConfiguration addResource(String resourceName) throws MappingException {
super.addResource( resourceName );
return this;
}
|
public AnnotationConfiguration addResource(String resourceName,
ClassLoader classLoader) throws MappingException {
super.addResource( resourceName, classLoader );
return this;
}
|
public AnnotationConfiguration addURL(URL url) throws MappingException {
super.addURL( url );
return this;
}
|
public AnnotationConfiguration addXML(String xml) throws MappingException {
super.addXML( xml );
return this;
}
|
public SessionFactory buildSessionFactory() throws HibernateException {
//add validator events if the jar is available
boolean enableValidatorListeners = !"false".equalsIgnoreCase( getProperty( "hibernate.validator.autoregister_listeners" ) );
Class validateEventListenerClass = null;
try {
validateEventListenerClass = ReflectHelper.classForName(
"org.hibernate.validator.event.ValidateEventListener",
AnnotationConfiguration.class );
}
catch (ClassNotFoundException e) {
//validator is not present
log.debug( "Validator not present in classpath, ignoring event listener registration" );
}
if ( enableValidatorListeners && validateEventListenerClass != null ) {
//TODO so much duplication
Object validateEventListener;
try {
validateEventListener = validateEventListenerClass.newInstance();
}
catch (Exception e) {
throw new AnnotationException( "Unable to load Validator event listener", e );
}
{
boolean present = false;
PreInsertEventListener[] listeners = getEventListeners().getPreInsertEventListeners();
if ( listeners != null ) {
for (Object eventListener : listeners) {
//not isAssignableFrom since the user could subclass
present = present || validateEventListenerClass == eventListener.getClass();
}
if ( !present ) {
int length = listeners.length + 1;
PreInsertEventListener[] newListeners = new PreInsertEventListener[length];
System.arraycopy( listeners, 0, newListeners, 0, length - 1 );
newListeners[length - 1] = (PreInsertEventListener) validateEventListener;
getEventListeners().setPreInsertEventListeners( newListeners );
}
}
else {
getEventListeners().setPreInsertEventListeners(
new PreInsertEventListener[] { (PreInsertEventListener) validateEventListener }
);
}
}
//update event listener
{
boolean present = false;
PreUpdateEventListener[] listeners = getEventListeners().getPreUpdateEventListeners();
if ( listeners != null ) {
for (Object eventListener : listeners) {
//not isAssignableFrom since the user could subclass
present = present || validateEventListenerClass == eventListener.getClass();
}
if ( !present ) {
int length = listeners.length + 1;
PreUpdateEventListener[] newListeners = new PreUpdateEventListener[length];
System.arraycopy( listeners, 0, newListeners, 0, length - 1 );
newListeners[length - 1] = (PreUpdateEventListener) validateEventListener;
getEventListeners().setPreUpdateEventListeners( newListeners );
}
}
else {
getEventListeners().setPreUpdateEventListeners(
new PreUpdateEventListener[] { (PreUpdateEventListener) validateEventListener }
);
}
}
}
enableHibernateSearch();
return super.buildSessionFactory();
}
|
public AnnotationConfiguration configure() throws HibernateException {
super.configure();
return this;
}
|
public AnnotationConfiguration configure(String resource) throws HibernateException {
super.configure( resource );
return this;
}
|
public AnnotationConfiguration configure(URL url) throws HibernateException {
super.configure( url );
return this;
}
|
public AnnotationConfiguration configure(File configFile) throws HibernateException {
super.configure( configFile );
return this;
}
|
public AnnotationConfiguration configure(Document document) throws HibernateException {
super.configure( document );
return this;
}
|
public ExtendedMappings createExtendedMappings() {
return new ExtendedMappings(
classes,
collections,
tables,
namedQueries,
namedSqlQueries,
sqlResultSetMappings,
defaultNamedQueryNames,
defaultNamedNativeQueryNames,
defaultSqlResulSetMappingNames,
defaultNamedGenerators,
imports,
secondPasses,
propertyReferences,
namingStrategy,
typeDefs,
filterDefinitions,
namedGenerators,
joins,
classTypes,
extendsQueue,
tableNameBinding, columnNameBindingPerTable, auxiliaryDatabaseObjects,
generatorTables,
tableUniqueConstraints,
mappedByResolver,
propertyRefResolver,
anyMetaDefs,
reflectionManager
);
}
|
protected AnnotationConfiguration doConfigure(Document doc) throws HibernateException {
super.doConfigure( doc );
return this;
}
|
protected AnnotationConfiguration doConfigure(InputStream stream,
String resourceName) throws HibernateException {
super.doConfigure( stream, resourceName );
return this;
}
|
public ReflectionManager getReflectionManager() {
return reflectionManager;
}
|
public AnnotationConfiguration mergeProperties(Properties properties) {
super.mergeProperties( properties );
return this;
}
|
protected List<XClass> orderAndFillHierarchy(List<XClass> original) {
//TODO remove embeddable
List< XClass > copy = new ArrayList< XClass >( original );
//for each class, copy all the relevant hierarchy
for (XClass clazz : original) {
XClass superClass = clazz.getSuperclass();
while ( superClass != null && !reflectionManager.equals( superClass, Object.class ) && !copy.contains( superClass ) ) {
if ( superClass.isAnnotationPresent( Entity.class )
|| superClass.isAnnotationPresent( MappedSuperclass.class ) ) {
copy.add( superClass );
}
superClass = superClass.getSuperclass();
}
}
List< XClass > workingCopy = new ArrayList< XClass >( copy );
List< XClass > newList = new ArrayList< XClass >( copy.size() );
while ( workingCopy.size() > 0 ) {
XClass clazz = workingCopy.get( 0 );
orderHierarchy( workingCopy, newList, copy, clazz );
}
return newList;
}
|
protected void parseMappingElement(Element subelement,
String name) {
Attribute rsrc = subelement.attribute( "resource" );
Attribute file = subelement.attribute( "file" );
Attribute jar = subelement.attribute( "jar" );
Attribute pckg = subelement.attribute( "package" );
Attribute clazz = subelement.attribute( "class" );
if ( rsrc != null ) {
log.debug( "{} < - {}", name, rsrc );
addResource( rsrc.getValue() );
}
else if ( jar != null ) {
log.debug( "{} < - {}", name, jar );
addJar( new File( jar.getValue() ) );
}
else if ( file != null ) {
log.debug( "{} < - {}", name, file );
addFile( file.getValue() );
}
else if ( pckg != null ) {
log.debug( "{} < - {}", name, pckg );
addPackage( pckg.getValue() );
}
else if ( clazz != null ) {
log.debug( "{} < - {}", name, clazz );
Class loadedClass;
try {
loadedClass = ReflectHelper.classForName( clazz.getValue() );
}
catch (ClassNotFoundException cnf) {
throw new MappingException(
"Unable to load class declared as < mapping class=\"" + clazz.getValue() + "\"/ > in the configuration:",
cnf
);
}
catch (NoClassDefFoundError ncdf) {
throw new MappingException(
"Unable to load class declared as < mapping class=\"" + clazz.getValue() + "\"/ > in the configuration:",
ncdf
);
}
addAnnotatedClass( loadedClass );
}
else {
throw new MappingException( "< mapping > element in configuration specifies no attributes" );
}
}
|
protected void reset() {
super.reset();
namedGenerators = new HashMap();
joins = new HashMap< String, Map< String, Join > >();
classTypes = new HashMap< String, AnnotatedClassType >();
generatorTables = new HashMap< String, Properties >();
defaultNamedQueryNames = new HashSet< String >();
defaultNamedNativeQueryNames = new HashSet< String >();
defaultSqlResulSetMappingNames = new HashSet< String >();
defaultNamedGenerators = new HashSet< String >();
tableUniqueConstraints = new HashMap< Table, List< String[] > >();
mappedByResolver = new HashMap< String, String >();
propertyRefResolver = new HashMap< String, String >();
annotatedClasses = new ArrayList< XClass >();
caches = new ArrayList< CacheHolder >();
hbmEntities = new HashMap< String, Document >();
annotatedClassEntities = new HashMap< String, XClass >();
hbmDocuments = new ArrayList< Document >();
namingStrategy = EJB3NamingStrategy.INSTANCE;
setEntityResolver( new EJB3DTDEntityResolver() );
anyMetaDefs = new HashMap< String, AnyMetaDef >();
reflectionManager = new EJB3ReflectionManager();
}
|
protected void secondPassCompile() throws MappingException {
log.debug( "Execute first pass mapping processing" );
//build annotatedClassEntities
{
List< XClass > tempAnnotatedClasses = new ArrayList< XClass >( annotatedClasses.size() );
for (XClass clazz : annotatedClasses) {
if ( clazz.isAnnotationPresent( Entity.class ) ) {
annotatedClassEntities.put( clazz.getName(), clazz );
tempAnnotatedClasses.add( clazz );
}
else if ( clazz.isAnnotationPresent( MappedSuperclass.class ) ) {
tempAnnotatedClasses.add( clazz );
}
//only keep MappedSuperclasses and Entity in this list
}
annotatedClasses = tempAnnotatedClasses;
}
//process default values first
if ( !isDefaultProcessed ) {
AnnotationBinder.bindDefaults( createExtendedMappings() );
isDefaultProcessed = true;
}
//process entities
if ( precedence == null ) precedence = getProperties().getProperty( ARTEFACT );
if ( precedence == null ) precedence = DEFAULT_PRECEDENCE;
StringTokenizer precedences = new StringTokenizer( precedence, ",; ", false );
if ( !precedences.hasMoreElements() ) {
throw new MappingException( ARTEFACT + " cannot be empty: " + precedence );
}
while ( precedences.hasMoreElements() ) {
String artifact = (String) precedences.nextElement();
removeConflictedArtifact( artifact );
processArtifactsOfType( artifact );
}
int cacheNbr = caches.size();
for (int index = 0; index < cacheNbr; index++) {
CacheHolder cacheHolder = caches.get( index );
if ( cacheHolder.isClass ) {
super.setCacheConcurrencyStrategy(
cacheHolder.role, cacheHolder.usage, cacheHolder.region, cacheHolder.cacheLazy
);
}
else {
super.setCollectionCacheConcurrencyStrategy( cacheHolder.role, cacheHolder.usage, cacheHolder.region );
}
}
caches.clear();
try {
inSecondPass = true;
processFkSecondPassInOrder();
Iterator iter = secondPasses.iterator();
while ( iter.hasNext() ) {
SecondPass sp = (SecondPass) iter.next();
//do the second pass of fk before the others and remove them
if ( sp instanceof CreateKeySecondPass ) {
sp.doSecondPass( classes );
iter.remove();
}
}
iter = secondPasses.iterator();
while ( iter.hasNext() ) {
SecondPass sp = (SecondPass) iter.next();
//do the SecondaryTable second pass before any association becasue associations can be built on joins
if ( sp instanceof SecondaryTableSecondPass ) {
sp.doSecondPass( classes );
iter.remove();
}
}
super.secondPassCompile();
inSecondPass = false;
}
catch (RecoverableException e) {
//the exception was not recoverable after all
throw (RuntimeException) e.getCause();
}
Iterator tables = tableUniqueConstraints.entrySet().iterator();
Table table;
Map.Entry entry;
String keyName;
int uniqueIndexPerTable;
while ( tables.hasNext() ) {
entry = (Map.Entry) tables.next();
table = (Table) entry.getKey();
List< String[] > uniqueConstraints = (List< String[] >) entry.getValue();
uniqueIndexPerTable = 0;
for (String[] columnNames : uniqueConstraints) {
keyName = "key" + uniqueIndexPerTable++;
buildUniqueKeyFromColumnNames( columnNames, table, keyName );
}
}
boolean applyOnDdl = getProperties().getProperty(
"hibernate.validator.apply_to_ddl", //org.hibernate.validator.Environment.APPLY_TO_DDL
"true" )
.equalsIgnoreCase( "true" );
//TODO search for the method only once and cache it?
Constructor validatorCtr = null;
Method applyMethod = null;
try {
Class classValidator = ReflectHelper.classForName( "org.hibernate.validator.ClassValidator", this.getClass() );
Class messageInterpolator = ReflectHelper.classForName( "org.hibernate.validator.MessageInterpolator", this.getClass() );
validatorCtr = classValidator.getDeclaredConstructor(
Class.class, ResourceBundle.class, messageInterpolator, Map.class, ReflectionManager.class
);
applyMethod = classValidator.getMethod( "apply", PersistentClass.class );
}
catch (ClassNotFoundException e) {
if ( !isValidatorNotPresentLogged ) {
log.info( "Hibernate Validator not found: ignoring" );
}
isValidatorNotPresentLogged = true;
}
catch (NoSuchMethodException e) {
throw new AnnotationException( e );
}
if ( applyMethod != null && applyOnDdl ) {
for (PersistentClass persistentClazz : (Collection< PersistentClass >) classes.values()) {
//integrate the validate framework
String className = persistentClazz.getClassName();
if ( StringHelper.isNotEmpty( className ) ) {
try {
Object validator = validatorCtr.newInstance(
ReflectHelper.classForName( className ), null, null, null, reflectionManager
);
applyMethod.invoke( validator, persistentClazz );
}
catch (Exception e) {
log.warn( "Unable to apply constraints on DDL for " + className, e );
}
}
}
}
}
|
public AnnotationConfiguration setCacheConcurrencyStrategy(String clazz,
String concurrencyStrategy) throws MappingException {
super.setCacheConcurrencyStrategy( clazz, concurrencyStrategy );
return this;
}
|
public void setCacheConcurrencyStrategy(String clazz,
String concurrencyStrategy,
String region,
boolean cacheLazyProperty) throws MappingException {
caches.add( new CacheHolder( clazz, concurrencyStrategy, region, true, cacheLazyProperty ) );
}
|
public AnnotationConfiguration setCollectionCacheConcurrencyStrategy(String collectionRole,
String concurrencyStrategy) throws MappingException {
super.setCollectionCacheConcurrencyStrategy( collectionRole, concurrencyStrategy );
return this;
}
|
public void setCollectionCacheConcurrencyStrategy(String collectionRole,
String concurrencyStrategy,
String region) throws MappingException {
caches.add( new CacheHolder( collectionRole, concurrencyStrategy, region, false, false ) );
}
|
public AnnotationConfiguration setInterceptor(Interceptor interceptor) {
super.setInterceptor( interceptor );
return this;
}
|
public AnnotationConfiguration setNamingStrategy(NamingStrategy namingStrategy) {
super.setNamingStrategy( namingStrategy );
return this;
}
|
public void setPrecedence(String precedence) {
this.precedence = precedence;
}
|
public AnnotationConfiguration setProperties(Properties properties) {
super.setProperties( properties );
return this;
}
|
public AnnotationConfiguration setProperty(String propertyName,
String value) {
super.setProperty( propertyName, value );
return this;
}
|