hibernate 启动代码分析

从上一篇源代码阅读看,这hibernate套的还真是有点深,这次主要看一下他的启动过程


// 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的时候, 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值