How to add mapping resource to hibernate configura

2019-08-08 05:37发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-08-08 06:05

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.

查看更多
登录 后发表回答