且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式将会话映射资源添加到Hibernate配置以创建会话工厂?

更新时间:2022-10-26 14:19:41

得到了答案,我必须使用文件字节从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.