然后就是准备模板了,首先声明
Html代码
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="${entity.className}" table="${entity.tableName}">
- <#if entity.properties?exists>
- <#list entity.properties as property>
- <#if property.primary>
- <id name="${property.name}" type="${property.type}">
- <#else>
- <property name="${property.name}" type="${property.type}">
- </#if>
- <#if property.type=="java.lang.String">
- <column name="${property.field?upper_case}" <#if property.length?exists>length="${property.length}"</#if>></column>
- <#elseif property.type=="java.util.Date">
- <column name="${property.field?upper_case}" length=7></column>
- <#elseif property.type=="java.lang.Long" || property.type=="java.lang.Integer"
- || property.type=="java.lang.Short">
- <column name="${property.field?upper_case}" <#if property.length?exists>precision="${property.length}"</#if> scale="0"></column>
- </#if>
- <#if property.primary==true>
- <#if property.sequence?exists>
- <generator class="sequence">
- <param name="sequence">${property.sequence}</param>
- </generator>
- </#if>
- </id>
- <#else>
- </property>
- </#if>
- </#list>
- </#if>
- </class>
- </hibernate-mapping>
4 注册
首先是对生成的hbm.xml的注册,比如,我获取倒Hibernate的一个config以后
然后就是要通知sessionFactory,新增了持久类,目前很多方法都是重启sessionfactory,就是关闭当前 sessionFactory ,然后根据config build一个新的sessionFactory出来,但是,这种情况感觉总不那么完美,虽然这个过程持续不到多长时间,但用户每增一个表就close然后build一个,单说用户体验,人家正在提交数据了,你把这个给close了....
但目前hibernate包的 sessionFactory 确实没提供这种对持久类的add支持,XX伟人说过:没有条件 创造条件也要上,于是乎,拿起你的键盘,启动 editplus,向hibernate3的源码砍去。。
Java代码
- 一是,改动Configuration,三句话
- public Mapping getMapping()
- {
- return this.mapping;
- }
然后是SessionFactoryImpl 我们要让他知道,这个世界上还存在很多未知的来客,需要你去主动了解。。。。
增加代码
- public void addPersistentClass(PersistentClass model,Mapping mapping)
- {
- if ( !model.isInherited() ) {
- IdentifierGenerator generator = model.getIdentifier().createIdentifierGenerator(
- settings.getDialect(),
- settings.getDefaultCatalogName(),
- settings.getDefaultSchemaName(),
- (RootClass) model
- );
- if (!identifierGenerators.containsKey(model.getEntityName()))
- identifierGenerators.put( model.getEntityName(), generator );
- }
- model.prepareTemporaryTables( mapping, settings.getDialect() );
- String cacheRegion = model.getRootClass().getCacheRegionName();
- CacheConcurrencyStrategy cache = CacheFactory.createCache(
- model.getCacheConcurrencyStrategy(),
- cacheRegion,
- model.isMutable(),
- settings,
- properties
- );
- if (cache!=null)
- allCacheRegions.put( cache.getRegionName(), cache.getCache() );
- EntityPersister cp = PersisterFactory.createClassPersister(model, cache, this, mapping);
- if ( cache != null && cache.getCache() instanceof OptimisticCache ) {
- ( ( OptimisticCache ) cache.getCache() ).setSource( cp );
- }
- entityPersisters.put( model.getEntityName(), cp );
- }
最后遗留的是hbm.xml在cfg.xml的注册,以保证系统重启后可以顺利加载新增的持久化配置
- /**
- * 把hbm.xml的路径加入到cfg.xml的mapping结点
- *
- * @param cfg.xml的路径
- * @param hbm.xml的路径
- */
- public static void updateHbmCfg(URL url,String hbm)
- {
- try
- {
- SAXReader reader = new SAXReader();
- Document doc = reader.read(url);
- Element element = (Element)doc.getRootElement()
- .selectSingleNode("session-factory");
- Element hbmnode = element.addElement("mapping");
- hbmnode.addAttribute("resource", hbm);
- String filepath = url.getFile();
- if (filepath.charAt(0)=='/')
- filepath = filepath.substring(1);
- FileOutputStream outputstream = new FileOutputStream(filepath);
- XMLWriter writer = new XMLWriter(outputstream);
- writer.write(doc);
- outputstream.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }