如何以编程方式将会话映射资源添加到Hibernate配置以创建会话工厂? [英] How to add mapping resource to hibernate configuration programatically for session factory creation?

查看:134
本文介绍了如何以编程方式将会话映射资源添加到Hibernate配置以创建会话工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用hibernate / Java / GWT动态创建模式,我正在动态创建 cfg.xml hbm.xml 文件并将它作为blob存储在数据库中。

所以我想为模式构建 sessionfactory
为此,我使用cfg.xml文件创建配置对象,但由于我的hbm.xml文件位于其他表中,因此它们不是文件系统上的文件,所以我怎么能将其作为资源添加到配置对象中。
我不想在文件系统上为它们创建文件

我尝试了 addInputStream() addFile()方法,但它们正在抛出 MappingNotFoundException

作为常规方法,我知道如何创建sessionfactory例如通过将< mapping resource =abc.hbm.xml> 标签添加到cfg.xml等等。但是在这里,如何将它们添加到配置中,因为我没有hbm.xml文件?



我的cfg.xml文件存储在表中

 <?xml version =1.0encoding =UTF-8standalone =no?> 
< hibernate-configuration>
< session-factory>
< property name =hibernate.connection.driver_class> com.mysql.jdbc.Driver< / property>
< property name =hibernate.connection.password> passwd< / property>
< property name =hibernate.connection.driver_class> com.mysql.jdbc.Driver< / property>
< property name =hibernate.connection.url> jdbc:mysql:// localhost:3306 / testSchema< / property>
< property name =hibernate.connection.username>根< / property>
< property name =hibernate.dialect> org.hibernate.dialect.MySQLInnoDBDialect< / property>
< property name =javax.persistence.validation.mode> none< / property>
< property name =hibernate.temp.use_jdbc_metadata_defaults> false< / property>
< property name =hibernate.default_entity_mode> dynamic-map< / property>
< / session-factory>
< / hibernate-configuration>

我的hbm.xml文件存储在表格中



 <?xml version =1.0encoding =UTF-8standalone =no?> 
< hibernate-mapping>
< class entity-name =testTable1>
< id column =idname =idtype =Long>
< generator class =identity/>
< / id>
< property column =idlength =20name =idtype =Long/>
< property column =booleanColumnlength =1name =booleanColumntype =Byte/>
< property column =doubleColumnlength =20name =doubleColumntype =Long/>
< property column =dateColumnname =dateColumntype =Double/>
< / class>
< / hibernate-mapping>


解决方案

得到了答案,我必须使用文件字节从DB中读取,然后将它们添加到配置对象中---

创建文档,

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(bytesOfFile);

然后,

  hibernateConfiguration.addDocument(DOC); 

这对我很有用。
谢谢。


I am trying to create schema dynamically using hibernate / Java/ GWT, I am creating cfg.xml and hbm.xml files dynamically and storing it in database as blob.

So I want to build sessionfactory for the schema. For the same I am creating configuration object using cfg.xml file, but since my hbm.xml files are in other table, they are not files on the file system, so how can I add it to configuration object as resource. I do not want to create files for them on file system.

I tried addInputStream(), addFile() methods but they are throwing MappingNotFoundException.

As regular method I know how to create sessionfactory like by adding the <mapping resource="abc.hbm.xml"> tag into the cfg.xml and so on. But here how can I add them to configuration because I do not have hbm.xml files?

my cfg.xml file stored in table:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.password">passwd</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testSchema</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
      <property name="javax.persistence.validation.mode">none</property>
      <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
      <property name="hibernate.default_entity_mode">dynamic-map</property>
   </session-factory>
</hibernate-configuration>

my hbm.xml file stored in table:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<hibernate-mapping>
  <class entity-name="testTable1">
    <id column="id" name="id" type="Long">
      <generator class="identity"/>
    </id>
    <property column="id" length="20" name="id" type="Long"/>
    <property column="booleanColumn" length="1" name="booleanColumn" type="Byte"/>
    <property column="doubleColumn" length="20" name="doubleColumn" type="Long"/>
    <property column="dateColumn" name="dateColumn" type="Double"/>
  </class>
</hibernate-mapping>

解决方案

Got the answer, I have to create Document using the file bytes read from DB and then add them in to the configuration object---

create the document,

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(bytesOfFile);

and then,

hibernateConfiguration.addDocument(doc);

This is worked for me. thanks.

这篇关于如何以编程方式将会话映射资源添加到Hibernate配置以创建会话工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆