简体   繁体   English

使用休眠将Excel工作表映射到数据库

[英]Mapping Excel sheet to database using hibernate

I have some master data to populate in the database and this is a one time activity done at the time of the very first execution. 我有一些主数据要填充到数据库中,这是第一次执行时的一次性活动。 I have decided to maintain the data in multiple sheets in an excel file. 我决定将数据保留在Excel文件中的多个工作表中。 There are some relationships in the database which i have to care of in the excel sheet. 我必须在Excel工作表中处理数据库中的某些关系。 This sheet will be read using an API(POI) and the data will be persisted using hibernate. 将使用API​​(POI)读取此工作表,并使用休眠方式保留数据。

Please suggest if this is the right approach or there are better ways to handle this? 请提出这是正确的方法还是有更好的方法来解决? Is there any other way to dump the excel into the database using hibernate(need a single excel file and support 5 types of databases)? 还有其他方法可以使用hibernate将excel转储到数据库中(需要一个excel文件并支持5种类型的数据库)吗?

Using excel and POI you can achieve this but as you are asking for other way so here it is: 使用excel和POI可以实现此目的,但是当您要求其他方式时,它是这样的:

If you are sure that this is one time activity then you can use hibernate.hbm2ddl.import_files property which will execute .sql file at the time of DB creation. 如果确定这是一次活动,则可以使用hibernate.hbm2ddl.import_files属性,该属性将在创建数据库时执行.sql文件。

Add hibernate property hibernate.hbm2ddl.import_files in your hibernate configuration. 在休眠配置中添加休眠property hibernate.hbm2ddl.import_files Change hibernate.hbm2ddl.aut o property to create . hibernate.hbm2ddl.aut o属性更改为create value of property hibernate.hbm2ddl.import_files is an SQL file which is @ /classes folder. property hibernate.hbm2ddl.import_files值是一个SQL文件,它是@ / classes文件夹。

Session factory bean will look like 会话工厂bean看起来像

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.hbm2ddl.import_files">initial_data.sql</prop>
        </props>
    </property>
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM