从上一篇源代码阅读看,这hibernate套的还真是有点深,这次主要看一下他的启动过程
先初始化Configuration 对象,这个类里保存运行时的配置信息,
解析获得session factory里面的配置信息
这里主要看下对mapping 节点的处理
这里的 metadataXml 实际上是一个mapping文件的对象,包含了一个hbm.xml里面的配置信息。建立相应的pojo类是在build session factory的时候,
// configures settings from hibernate.cfg.xml
Configuration cfg=new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
先初始化Configuration 对象,这个类里保存运行时的配置信息,
protected Map<String, PersistentClass> classes;
protected Map<String, String> imports;
protected Map<String, Collection> collections;
protected Map<String, Table> tables;
protected List<AuxiliaryDatabaseObject> auxiliaryDatabaseObjects;
protected Map<String, NamedQueryDefinition> namedQueries;
protected Map<String, NamedSQLQueryDefinition> namedSqlQueries;
protected Map<String, ResultSetMappingDefinition> sqlResultSetMappings;
protected Map<String, TypeDef> typeDefs;
protected Map<String, FilterDefinition> filterDefinitions;
protected Map<String, FetchProfile> fetchProfiles;
protected Map tableNameBinding;
protected Map columnNameBindingPerTable;
protected List<SecondPass> secondPasses;
protected List<Mappings.PropertyReference> propertyReferences;
protected Map<ExtendsQueueEntry, ?> extendsQueue;
protected Map<String, SQLFunction> sqlFunctions;
解析获得session factory里面的配置信息
private void parseSessionFactory(Element sfNode, String name) {
Iterator elements = sfNode.elementIterator();
while ( elements.hasNext() ) {
Element subelement = (Element) elements.next();
String subelementName = subelement.getName();
if ( "mapping".equals( subelementName ) ) {
parseMappingElement( subelement, name );
}
else if ( "class-cache".equals( subelementName ) ) {
String className = subelement.attributeValue( "class" );
Attribute regionNode = subelement.attribute( "region" );
final String region = ( regionNode == null ) ? className : regionNode.getValue();
boolean includeLazy = !"non-lazy".equals( subelement.attributeValue( "include" ) );
setCacheConcurrencyStrategy( className, subelement.attributeValue( "usage" ), region, includeLazy );
}
else if ( "collection-cache".equals( subelementName ) ) {
String role = subelement.attributeValue( "collection" );
Attribute regionNode = subelement.attribute( "region" );
final String region = ( regionNode == null ) ? role : regionNode.getValue();
setCollectionCacheConcurrencyStrategy( role, subelement.attributeValue( "usage" ), region );
}
else if ( "listener".equals( subelementName ) ) {
parseListener( subelement );
}
else if ( "event".equals( subelementName ) ) {
parseEvent( subelement );
}
}
}
这里主要看下对mapping 节点的处理
private XmlDocument add(InputSource inputSource, Origin origin) {
XmlDocument metadataXml = MappingReader.INSTANCE.readMappingDocument( entityResolver, inputSource, origin );
add( metadataXml );
return metadataXml;
}
这里的 metadataXml 实际上是一个mapping文件的对象,包含了一个hbm.xml里面的配置信息。建立相应的pojo类是在build session factory的时候,