hibernate涂鸦(1)——cfg配置
?
org.hibernate.cfg.SettingsFactory: 工厂的工厂,根据properties创建一系列setting和factory,会在Configuration的无参构造函数里被new出来,并设置给Configuration的settingsFactory
? -- protected SettingsFactory() 空构造函数。
?
org.hibernate.cfg.EJB3DTDEntityResolver extends DTDEntityResolver:? 根据xml文件的publicId和systemId创建并返回SAX的InputSource,其中systemId会包含具体的xsd或者dtd(dtd由父类处理)的文件名字,classpath路径固定为org/hibernate/ejb/xxx.xsd或者org/hibernate/xxx.dtd
? -- entityResolver 静态自身对象,默认创建,但它没有用private封锁构造函数,所以不是单例模式。
?
org.hibernate.util.xml.OriginImpl: 描述xml配置文件的来源
? -- String type
? -- String name
?
org.hibernate.util.xml.XmlDocumentImpl: 描述xml doc本身
? -- documentTree: dom4j的文档结构
? -- origin: 来源对象
?
org.hibernate.util.xml.MappingReader: 饥渴单例,帮助类,单个public方法,通过resolver和inputSource来生成xmlDocument。
? -- public XmlDocument readMappingDocument(EntityResolver, InputSource, Origin): 根据resolver来做xml校验,并生成document
?
?
org.hibernate.event.EventListeners: 一个所有listener的超级容器类,包含了所有listener类型的数组,会在Configuration的reset方法里被new出来,并设置给Configuration的eventListeners
? -- eventInterfaceFromType: 一个将listener类型的Class对象按event类型分类存放的Map。
? -- public Class getListenerClassFor(String): 根据传入的event type值从上面的map里得到Class对象
?
org.hibernate.secure.JACCConfiguration: EJB JACC配置的抽象,JACC对象应该是受JavaEE容器管理的;有一个以contextId为参数的构造函数,会在Configuration的doConfig方法中解析到配置了"security"子节点时被new出来。
? -- policyConfiguration: 在构造函数中通过PolicyConfigurationFactory(参见JACC API)得到
? -- public JACCConfiguration(String contextId) throws HibernateException: 唯一的一个构造函数,会将在取得policyConfiguration时捕获的异常转化成HibernateException抛出
? -- public void addPermission(String role, String entityName, String action): 用传入的实体名字和动作构成EJBMethodPermission(参见JACC API)对象后授权给role参数指定的角色。
?
?
org.hibernate.cfg.Configuration.MappingsImpl: 内部类,默认构造函数,会在Configuration的addPackage方法中被new出来传给AnnotationBinder.bindPackage方法
? -- String schemaName: 默认schema配置 @QUESTION赋值时间
? -- String catalogName: 默认catalog配置 @QUESTION赋值时间
? -- Boolean useNewGeneratorMappings: 是否用新版的Id生成机制
? -- public ReflectionManager getReflectionManager(): 取外部类Configuration的reflectionManager
? -- public TypeResolver getTypeResolver(): 取外部类Configuration的typeResolver
? -- public Properties getConfigurationProperties(): 取外部类Configuration的properties
? -- public boolean useNewGeneratorMappings(): 返回useNewGeneratorMappings的值,如果useNewGeneratorMappings为空,则用getConfigurationProperties()里的"hibernate.id.new_generator_mappings"属性值来初始化useNewGeneratorMappings
? -- public void addGenerator(IdGenerator): 如果defaultNamedGenerators集合里还没有包括传入的idGenetator的name,则把name作为key,对象本身作为value放入namedGenerators,旧的key-value会被覆盖掉并记录log。
? -- public void addSecondPass(SecondPass): ②addSecondPass(传入的SecondPass, false)
? -- public void addSecondPass(SecondPass, boolean onTopOfTheQueue): 将传入的SecondPass对象放入secondPasses列表中,如果onTopOfTheQueue是true则放入第0个,else放入最后一个。
? -- public void addFilterDefinition(FilterDefinition): 把传入的filterDefinition的name作为key,对象本身作为value放入filterDefinitions
? -- public void addDefaultQuery(String, NamedQueryDefinition): 把传入的name作为key,对象本身作为value放入namedQueries,然后在defaultNamedQueryNames集合里加入name,修改之前如果namedQueries或者namedSqlQueries之一已经有这个name了,则停止修改并抛出DuplicateMappingException,该类为MappingException的子类。
? -- public void addDefaultSQLQuery(String, NamedSQLQueryDefinition): 把传入的name作为key,对象本身作为value放入namedSqlQueries,然后在defaultNamedNativeQueryNames集合里加入name,修改之前如果namedQueries或者namedSqlQueries之一已经有这个name了,则停止修改并抛出DuplicateMappingException,该类为MappingException的子类。
? -- public void addQuery(String, NamedQueryDefinition) throws DuplicateMappingException: 如果defaultNamedQueryNames集合里还没有包括传入的name参数,则把name作为key,对象本身作为value放入namedQueries,修改之前如果namedQueries或者namedSqlQueries之一已经有这个name了,则停止修改并抛出DuplicateMappingException,该类为MappingException的子类。
? -- public void addSQLQuery(String, NamedSQLQueryDefinition) throws DuplicateMappingException: 如果defaultNamedNativeQueryNames集合里还没有包括传入的name参数,则把name作为key,对象本身作为value放入namedSqlQueries,修改之前如果namedQueries或者namedSqlQueries之一已经有这个name了,则停止修改并抛出DuplicateMappingException,该类为MappingException的子类。
? -- public void addTypeDef(String, String, Properties): 拿传入的typeClass和properties构造一个TypeDef对象,然后把传入的typeName作为key,TypeDef对象作为value放入typeDefs
? -- public void addAnyMetaDef(AnyMetaDef) throws AnnotationException: 如果anyMetaDefs的key集合里已经包括了传入annotation的name参数值,则抛出AnnotationException,否则把把传入的annotation的name参数值作为key,annotation本身作为value放入anyMetaDefs。
?
org.hibernate.cfg.Configuration.MetadataSourceQueue: 内部类,默认构造函数,会在Configuration的reset方法中被new出来,并设置给metadataSourceQueue
? -- annotatedClasses: List<XClass>,transient,存放所有实体的XClass,包括H-cfg配置文件里定义的"class"以及JAP-orm声明的"entity"/"mapped-superclass"/"embeddable"节点下的"class"和"listener"。
? -- hbmMetadataByEntityNameXRef: Map<String, XmlDocument>,从映射实体的标识符找hbm doc。
? -- hbmMetadataToEntityNamesMap: LinkedHashMap<XmlDocument, Set<String>>,从hbm doc文档找映射实体标识符集合。
? -- public void add(XClass): 将传入的XClass放入annotatedClasses
? -- public void add(XmlDocument): 传入这个方法的doc都应该是hbm,递归的查找hbm中声明的所有"class"/"subclass"/"joined-subclass"/"union-subclass"子节点上定义的被映射实体的唯一标识符,唯一标识符通常为这些子节点的"entity-name"属性值,如果"entity-name"属性值不存在,则使用"name"属性值,如果"name"属性值不是class的全限定名,则在"name"属性值之前加上"package"属性值(定义在root节点"hibernate-mapping"上);对每一个标识符:将这些标识符作为key,hbm doc作为value放入到hbmMetadataByEntityNameXRef中;将hbm doc作为key,所有标识符组成的Set作为value放入hbmMetadataToEntityNamesMap中。
?
org.hibernate.cfg.Configuration.CacheHolder: 内部类,对Cache配置的描述,全属性参数构造函数,会在Configuration的reset方法中建立出一个该对象的空List,并设置给caches
? -- String role: 对谁使用缓存,一般为实体类的名字
??-- String usage: 缓存对象的并发策略(read-only|read-write|nonstrict-read-write|transactional四选一)
??-- String region: 缓存在哪里
??-- boolean isClass: 是否是实体类级别的缓存
??-- boolean cacheLazy: 是否lazy
org.hibernate.cfg.Configuration: 配置核心类,很多配置方法都返回this,使得这些方法可以在单个语句中被连续调用。
? -- settingsFactory: 在reset中被new出来
? -- XMLHelper: transient,在reset中被new出来,提供SAXReader,DOMReader
? -- entityResolver: 有public的accessor方法,在reset中使用new EJB3DTDEntityResolver赋值
? -- typeResolver: 有public的get方法,在变量声明时直接使用new TypeResolver赋值
? -- properties: 属性库
? -- reflectionManager: transient,有public的get方法,在reset中使用new JavaReflectionManager赋值
? -- secondPasses: List<SecondPass>: 需要第二次处理的所有对象列表
? -- inSecondPass: 默认false,只有在secondPassCompile()方法中才会被置成true,在方法退出之前被置回false。@QUESTION
? -- metadataSourceQueue: 元数据队列,在reset中使用new MetadataSourceQueue赋值
? -- caches: List<CacheHolder>
? -- eventListeners: 有public的get方法,在reset中使用new EventListeners赋值
?
? -- defaultNamedGenerators: Set<String>,IdGenerator的名字组成的集合
? -- namedGenerators: Map<String, IdGenerator>,名字找IdGenerator对象
? -- defaultNamedQueryNames: Set<String>,NamedQueryDefinition的名字组成的集合
? -- namedQueries: Map<String, NamedQueryDefinition>,名字找NamedQueryDefinition对象
? -- defaultNamedNativeQueryNames: Set<String>,NamedSQLQueryDefinition的名字组成的集合
? -- namedSqlQueries: Map<String, NamedSQLQueryDefinition>,名字找NamedSQLQueryDefinition对象
? -- filterDefinitions: Map<String, FilterDefinition>,名字找FilterDefinition对象
? -- typeDefs: Map<String, TypeDef>,名字找TypeDef对象
? -- anyMetaDefs: Map<String, AnyMetaDef>,名字找@AnyMetaDef对象
? -- public Configuration(): 公开构造函数,new一个SettingsFactory后作为参数调用包内构造函数。
? -- protected Configuration(SettingsFactory): 赋值settingsFactory,调用reset方法。
? -- protected void reset(): 只在包内构造函数中调用一次,初始化。
? -- public Configuration configure() throws HibernateException: 默认使用"/hibernate.cfg.xml"调用configure(String)方法。
? -- public Configuration configure(File) throws HibernateException: 根据文件类建立InputStream,作为参数和文件toString调用①doConfigure方法。File找不到错误会被包装成HibernateException抛出。
? -- public Configuration configure(org.w3c.dom.Document) throws HibernateExceptio: 把w3c的dom结构转化为dom4j的dom结构,作为参数调用②doConfigure方法。
? -- public Configuration configure(String) throws HibernateException: 根据文件路径建立InputStream,作为参数和String本身调用①doConfigure方法,空的InputStream会导致一个HibernateException的抛出。
? -- public Configuration configure(URL) throws HibernateException: ①doConfigure(url.openStream(), url.toString()) ,url的io错误会被包装成HibernateException抛出
? -- protected Configuration doConfigure(InputStream, String) throws HibernateException: 以entityResolver为参数调用XMLHelper建立SAXReader后读取InputStream生成dom4j的dom结构,作为参数调用②doConfigure方法,解析InputStream失败会抛出MappingException,该类为HibernateException的子类。
? -- protected Configuration doConfigure(Document) throws HibernateException: 最终使用的配置方法:
?
? -- public Configuration addResource(String) throws MappingException: ①add(由传入的mapping全文件名得到的InputStream, "resource", mapping全文件名),读取InputStream的ClassLoader优先选择当前线程的CL,其次使用加载Environment类的CL;空的InputStream会导致一个MappingNotFoundException的抛出,该类为MappingException的子类。
? -- public Configuration addResource(String, ClassLoader) throws MappingException: ①add(由传入的mapping文件名得到的InputStream, "resource", mapping文件名),和上面方法的区别是只能通过指定的ClassLoader来读取InputStream,同样空的InputStream会导致一个MappingNotFoundException的抛出,该类为MappingException的子类。
? -- public Configuration addClass(Class) throws MappingException: 直接把Class的全名称的点替换为除号后再加上.hbm.xml,和该Class的CL作为参数调用② addResource方法。
? -- public Configuration addFile(String) throws MappingException: 通过文件名建立File对象,然后调用②addFile(File对象)
? -- public Configuration addFile(File) throws MappingException: ②add(由传入的File得到的InputSource, "file", File的AbsolutePath) ,File找不到错误导致一个MappingNotFoundException的抛出,该类为MappingException的子类。
? -- public Configuration addJar(File) throws MappingException: 对jar包中每一个以".hbm.xml"结尾的文件调用addInputStream(文件的InputStream),IO错误会被包装成InvalidMappingException抛出,该类为MappingException的子类。
? -- public Configuration addInputStream(InputStream) throws MappingException: ①add(传入的InputStream, "input stream", null)
? -- public Configuration addPackage(String) throws MappingException: AnnotationBinder.bindPackage(传入的package名字, new出一个内部类MappingImp对象),对catch到的MappingException记录日志后再抛出。
? -- public Configuration addAnnotatedClass(Class): 通过reflectionManager.toXClass(Class)将传入的Class对象转化成JavaXClass对象,然后调用metadataSourceQueue.add(XClass)。
? -- private XmlDocument add(InputStream, String, String): ②add(由传入的InputStream得到的InputSource, originType, originName)
? -- private XmlDocument add(InputSource, String, String): ③add(inputSource, 根据originType和originName建立的OriginImpl对象)
? -- private XmlDocument add(InputSource, Origin): 将InputSource参数和entityResolver属性传给MappingReader来生成XmlDocumentImpl的实例,然后作为参数调用add(xmlDocument),最后return这个实例。
? -- public void add(XmlDocument): 如果是inSecondPass或者不是JPA的orm.xml,则调用metadataSourceQueue.add(xmlDocument);else,则通过reflectionManager.jpaMetadataProvider.xmlContext.addDocument(Document)将doc文档中的dom4j结构解析成XMLContext对象,并且将返回的一个className的List中的所有Class(包括JPA orm中所有"entity"/"mapped-superclass"/"embeddable"定义的class和listener)都通过reflectionManager.classForName(className, 当前Configuration对象的Class对象)转化成XClass对象,然后调用metadataSourceQueue.add(XClass),在根据className找class的过程中如果出现类找不到错误则包装成AnnotationException抛出,该类为MappingException的子类。
?
? -- public Configuration setCacheConcurrencyStrategy(String, String): ②setCacheConcurrencyStrategy(传入的entityName, 传入的concurrencyStrategy,?传入的entityName)。
? -- public Configuration setCacheConcurrencyStrategy(String, String?, String): ③setCacheConcurrencyStrategy(传入的entityName, 传入的concurrencyStrategy,?传入的region, true)。
? -- public void setCacheConcurrencyStrategy(String, String, String, boolean cacheLazyProperty) throws MappingException: 用传入的4个参数再加上指示isClass的true值构造一个CacheHolder对象放入caches列表中。
?
? -- public Configuration setCollectionCacheConcurrencyStrategy(String, String): ②setCollectionCacheConcurrencyStrategy(传入的collectionName, 传入的concurrencyStrategy,?传入的collectionName)。
? -- public void setCollectionCacheConcurrencyStrategy(String, String, String): 用传入的3个参数再加上指示isClass的false值和指示lazy的false值构造一个CacheHolder对象放入caches列表中。
?
? -- public void setListener(String, String):?③setListeners(传入的type, new String[]{传入的listener名字})
? -- public void setListener(String, Object): ④setListeners(传入的type, new Object[]{传入的listener对象}),其中Object数组是由特定类型的数组cast而来的,具体类型通过eventListeners.getListenerClassFor(传入的type)得到,所以当传入的Object对象不属于这个类型时,会发生ArrayStoreException,该错误并没有得到捕获
? -- public void setListeners(String, String[]): ④setListeners(传入的type, new Object[]{根据传入的listeners的ClassName构造的多个对象}),其中Object数组是由特定类型的数组cast而来的,具体类型通过eventListeners.getListenerClassFor(传入的type)得到,所以当传入的listeners中有一个ClassName不属于这个类型时,会发生ArrayStoreException,该异常以及类找不到异常会被转换成MappingException抛出。
? -- public void setListeners(String, Object[]): 根据传入的type,分别修改eventListeners里存的对应的listener数组:如果传入的listeners是null,则使用空数组修改;else则使用listener赋值。如果传入的type不符合所有预定于的event type,在抛出MappingException。
?
? -- public Configuration setProperties(Properties): 直接赋值整个properties。
? -- public Configuration setProperty(String, String): 对properties里的单个property进行赋值。
? -- public Configuration addProperties(Properties): 增加或者替换额外的properties。
?
?
?
org.hibernate.cfg.AnnotationBinder: 静态帮助类,使用private封锁构造函数。
? -- public static void bindPackage(String, Mappings): 通过mapping参数拿到它外部Configuration类的reflectionManager,调用packageForName(传入的pkg名字参数)得到一个XPackage的对象,然后:
?
?
?
?
?
?