在hibernate中将映射类添加为单独的xml文件

时间:2013-12-19 11:42:51

标签: hibernate hibernate-mapping

如何将我的hibernate映射类作为单独的xml文件添加到cfg文件中。

这是我的hibernate配置文件。

 <hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test

        <property name="connection.username">root</property>
        <property name="connection.password">rot@pspl#12</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">
       org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">false</property>

        <property name="hbm2ddl.auto">validate</property>

       **//how to import mappingclass.xml here **


    </session-factory>
</hibernate-configuration>

这里我的映射类。我需要将这些元素作为mapping.xml添加到cfg文件中。

mappingclass.xml

<mapping class="pepper.logis.item.model.Item" />
    <mapping class="pepper.logis.itemstock.model.ItemStock" />
    <mapping class="pepper.logis.itemreceiptheader.model.ItemReceiptHeader" />
    <mapping class="pepper.logis.itemreceiptdetails.model.ItemReceiptDetails" />
    <mapping class="pepper.logis.itemissueheader.model.ItemIssueHeader" />
    <mapping class="pepper.logis.itemissuedetails.model.ItemIssueDetails" />
    <mapping class="pepper.logis.itemrequisition.model.ItemRequisition" />

由于

1 个答案:

答案 0 :(得分:3)

这可能对你有帮助..

private static SessionFactory buildSessionFactory() {
        try {

            Configuration configuration = new Configuration();

            configuration.configure("hibernate.cfg.xml");

            configuration.addAnnotatedClass(pepper.logis.itemstock.model.ItemStock);//add your mapping class here..

            ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder()
            .applySettings(configuration.getProperties());

            return configuration.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());

        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }