How to add mapping resource to hibernate configuration programatically for session factory creation?

寵の児 提交于 2019-12-11 05:28:16

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/17298763/how-to-add-mapping-resource-to-hibernate-configuration-programatically-for-sessi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!