简体   繁体   中英

Are JPA annotations required when using Spring test DBUnit @DatabaseSetup annotation?

I am playing with Spring test dbunit library, I have a question about datasets. I have a POJO representing an entity I want to persist into in-memory database in my tests. This POJO is not annotated by any JPA annotations like @Entity , @Table or @Column , is it possible to use such class in XML dataset file with @DatabaseSetup annotation?

Code example: XML dataset file:

<?xml version="1.0" encoding="UTF-8"?>
    <dataset>
        <Person id="1" dateOfBirth="2000-07-25T10:55:58" age="10.5" sex="MALE"/>
    </dataset>

and Person POJO class:

public class Person {

private Long id;
private LocalDateTime dateOfBirth;
private BigDecimal age;
private Sex sex;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public LocalDateTime getDateOfBirth() {
    return dateOfBirth;
}

public void setDateOfBirth(LocalDateTime dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

public BigDecimal getAge() {
    return age;
}

public void setAge(BigDecimal age) {
    this.age = age;
}

public Sex getSex() {
    return sex;
}

public void setSex(Sex sex) {
    this.sex = sex;
}

Right now I am getting following error:

org.dbunit.dataset.NoSuchTableException: Person

I have another POJO class with JPA annotations and it is processed without any errors by @DatabaseSetup annotation. I am trying to use both POJOs in single test. Are JPA annotations required by Spring test DBUnit library in order to setup my database before tests?

No, you don't need any JPA annotations. The problem is that the DB you're using is missing a table named Person . DBUnit will insert the data, but it won't create the tables for you (There's a similar question about this: Is there any way for DBUnit to automatically create tables? )

To manage the schema I recommend you to use a tool that manages 'db migrations' such as flyway or liquibase . Both these tools integrate with spring, so they can create the schema when spring loads up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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