添加映射的过程

博客围绕Java的Configuration类展开,通过add方法添加映射,该任务由Binder类的bindRoot方法处理。结合映射配置文件分析bindRoot方法,指出读取配置信息采用责任下放模式使程序结构清晰,但Binder类方法过长。因后续内容多,暂时跟踪到此。

在Configuration类中,我们通过add方法添加一个映射,而add方法又把这个任务交给了Binder类的bindrooR方法。
 

ExpandedBlockStart.gifContractedBlock.gifprotected void add(org.dom4j.Document doc) throws Exception dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
try dot.gif{
InBlock.gif   Binder.bindRoot( doc, createMappings() );
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
catch (MappingException me) dot.gif{
InBlock.gif   log.error(
"Could not compile the mapping document", me);
InBlock.gif   
throw me;
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif }

None.gif



首先,我们来看一段映射配置:

None.gif<?xml version="1.0"?>
None.gif
<!DOCTYPE hibernate-mapping
None.gif    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
None.gif    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
None.gif
None.gif
<hibernate-mapping>
None.gif
None.gif    
<class name="net.sf.hibernate.examples.quickstart.Cat" table="CAT">
None.gif
None.gif        
<!-- A 32 hex character is our surrogate key. It's automatically
None.gif            generated by Hibernate with the UUID pattern. 
-->
None.gif        
<id name="id" type="string" unsaved-value="null" >
None.gif            
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
None.gif            
<generator class="uuid.hex"/>
None.gif        
</id>
None.gif
None.gif        
<!-- A cat has to have a name, but it shouldn' be too long. -->
None.gif        
<property name="name">
None.gif            
<column name="NAME" sql-type="varchar(16)" not-null="true"/>
None.gif        
</property>
None.gif
None.gif        
<property name="sex"/>
None.gif
None.gif        
<property name="weight"/>
None.gif
None.gif    
</class>
None.gif
None.gif
</hibernate-mapping>
None.gif

 

我们结合这个映射配置文件来看看bindRoot方法(由于方法过长,这里只截取其中处理class的一部分):

ExpandedBlockStart.gifContractedBlock.gif /**//**
InBlock.gif  * 传入一个描述映射关系的Document用以填充Mapping
ExpandedBlockEnd.gif  
*/

ExpandedBlockStart.gifContractedBlock.gif 
public static void bindRoot(Document doc, Mappings model) throws MappingException dot.gif{
InBlock.gif
InBlock.gif  
// 取得根结点,即 hibernate-mapping 结点
InBlock.gif
  Element hmNode = doc.getRootElement();
InBlock.gif
InBlock.gif  
// 取结点的属性值
InBlock.gif
  Attribute schemaNode = hmNode.attribute("schema");
InBlock.gif  model.setSchemaName( (schemaNode
==null? null : schemaNode.getValue() );
InBlock.gif
InBlock.gif  
// dot.gifdot.gif
InBlock.gif
InBlock.gif  
// 取得所有的class子结点。
InBlock.gif  
// 由此可以看出,一个配置文件可以配置多个class的映射。
InBlock.gif
  Iterator nodes = hmNode.elementIterator("class");
ExpandedSubBlockStart.gifContractedSubBlock.gif  
while ( nodes.hasNext() ) dot.gif{
InBlock.gif   Element n 
= (Element) nodes.next();
InBlock.gif
InBlock.gif   
// 至此为止,又将权利下放到了bindRootClass方法。
InBlock.gif   
// 这是重构长方法的一种办法。
InBlock.gif
   RootClass rootclass = new RootClass();
InBlock.gif   Binder.bindRootClass(n, rootclass, model);
InBlock.gif   model.addClass(rootclass);
ExpandedSubBlockEnd.gif  }

InBlock.gif  
// dot.gifdot.gif
InBlock.gif
  
ExpandedBlockEnd.gif }

None.gif

 

所有读取配置信息的工作都是使用上面这种模式:一步一步地把责任往下放,这种做法好处就在于使程序结构清晰,可惜在Binder类中做的还是不够——方法还是太长。

因为后面的代码牵涉到的内容还太多,所以暂时跟踪到此,待mapping包分析完成再来看它。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值