简体   繁体   English

在spring / hibernate项目中替换import.sql文件

[英]replacement for import.sql file in spring/hibernate project

In my spring projects, I have hibernate configured to create the tables in the database based on my entity classes. 在我的春季项目中,我已将hibernate配置为根据实体类在数据库中创建表。 Also, when I need insert some initial values on this tables, I put a file named import.sql on my classpath with the sql commands to insert data in the database. 另外,当我需要在此表上插入一些初始值时,我使用sql命令在类路径上放置了一个名为import.sql的文件,以在数据库中插入数据。 I wonder if there is a way to accomplish this import feature without need to place a import.sql file inside my project, using only java classes. 我想知道是否有一种无需使用我的java类就可以在我的项目中放置import.sql文件的情况下完成此导入功能的方法。 Anyone knows if this is possible? 有人知道这是否可能吗?

You can consider two alternatives, 您可以考虑两种选择,

First one is not java only, but since you're using that stack, you can consider Spring DBUnit 第一个不仅是java,而且由于您正在使用该堆栈,因此可以考虑使用Spring DBUnit。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/context.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class,
        DbUnitTestExecutionListener.class})
@DbUnitConfiguration(dataSetLoader = ColumnSensingFlatXMLDataSetLoader.class)
public class TemplateIT {

    @Before
    public void after() throws Exception {
        IDatabaseConnection connection;
        IDatabaseTester databaseTester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL + "&sessionVariables=FOREIGN_KEY_CHECKS=0", USER, JDBC_PASSWORD);
        connection = databaseTester.getConnection();
        QueryDataSet partialDataSet = new QueryDataSet(connection);
        DatabaseOperation.DELETE_ALL.execute(connection,
                partialDataSet);
    }


    @DatabaseSetup("../dbunit/data.xml")
    @ExpectedDatabase("../dbunit/expected.xml")
    @Test
    public void testDBUnit() throws Exception {
        ...
    }


}

The data are handled through XML, but as the library is focused around test it offers many utilities that outmatch dealing with SQL directly. 数据是通过XML处理的,但是由于该库专注于测试,因此它提供了许多直接与SQL处理不相上下的实用程序。 Easier dealing with partial data, simplified assertion thanks to @ExpectedDatabase etc. 借助@ExpectedDatabase等,可以更轻松地处理部分数据,简化断言。

Secondly, one "java only" setup that I use often is to prepare data through hibernate entities. 其次,我经常使用的一种“仅Java”设置是通过休眠实体准备数据。 I'm assuming that you're using generic DAOs, even if not, its easy to set it up. 我假设您使用的是通用DAO,即使没有使用,也很容易设置。 Than in @Before methods of your test simply set up the DB to you're liking, a snippet 比起@Before测试方法,只需按照自己的喜好设置数据库即可

@Before
public void before() throws DAOException {
   Company company = new Company();
    companyDAO.makePersistent(company);
}

I favour this approach as in my view increases portability (often I use in memory DBs for testing), readability and maintaince. 我赞成这种方法,因为我认为它提高了可移植性(通常在内存DB中用于测试),可读性和可维护性。 The downside is a lot of code in the tests, and a preparing data can get tedious for complex objects. 缺点是测试中有很多代码,准备数据对于复杂对象可能会很乏味。

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

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