Add mapping classes as a separate xml file in hibernate

无人久伴 提交于 2019-12-07 08:04:10

问题


How can i add my hibernate mapping classes into cfg file as a separate xml file.

Here is my hibernate configuration file .

 <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>

here My mapping classes.I need to add these elements in to cfg file as a mapping.xml..

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" />

Thanks


回答1:


This may be helping you..

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);
        }
    }


来源:https://stackoverflow.com/questions/20680859/add-mapping-classes-as-a-separate-xml-file-in-hibernate

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