【问题标题】:EntityManagerException while running in Travis CI, Spring Boot and PostgreSQL在 Travis CI、Spring Boot 和 PostgreSQL 中运行时出现 EntityManagerException
【发布时间】:2017-04-09 12:02:18
【问题描述】:

我使用 Spring Boot、PostgreSQL、Maven 和 JUnit 制作了一个简单的 Web 应用程序。在我的 IDE (mvn clear-verify) 中运行它时,一切正常,但在 Travis CI 中运行时,我遇到了这个异常:

在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class] 中定义名称为“entityManagerFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory

还有很多其他的。我的测试在 IDE 中也能完美运行。有人能告诉我为什么吗?我的代码在这里:

实体:

@Entity
@Table(name = "contacts")
public class Contact extends BaseEntity{

    @Column(name = "name", nullable = false)
    private String name;

    public Contact() {
    }
    public Contact(String name) {
        this(null,name);
    }
    public Contact(Integer id, String name) {
        super(id);
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Contact{" +
                "id='" + id + '\'' +
                "name='" + name + '\'' +
                '}';
    }
}

存储库:

@Repository
public class ContactRepositoryImpl implements ContactRepository{

    private static final Logger LOG = LoggerFactory.getLogger(ContactRepositoryImpl.class);

    @Autowired
    private ProxyContactRepository proxyContactRepository;

    private Pattern regexPattern;

    @Override
    public List<Contact> getAllSorted(String nameFilter) {
        List<Contact> listOfAllContacts = new CopyOnWriteArrayList<>();
        try {
            regexPattern = Pattern.compile(nameFilter);
            if (!nameFilter.isEmpty() || nameFilter.length() != 0) {
                listOfAllContacts.addAll(getAll().stream().filter(contact -> notDoMatch(contact.getName())).collect(Collectors.toList()));
            } else {
                LOG.warn("Regex parameter " + "'" + nameFilter + "'" + " is empty");
                throw new NotFoundException("Regex parameter is empty");
            }
            return listOfAllContacts;
        }
        catch (PatternSyntaxException exception){
            LOG.error("Regex parameter " + "'" + nameFilter + "'" + " is invalid");
            throw new NotFoundException("Regex parameter is invalid");
        }
    }

    @Override
    public List<Contact> getAll() {
        return proxyContactRepository.findAll();
    }

    private boolean notDoMatch(String word){
        Matcher matcher = regexPattern.matcher(word);
        return !matcher.matches();
    }
}

控制器:

@RestController
@RequestMapping("/contacts")
public class ContactController extends AbstractContactController{

    @RequestMapping(method = RequestMethod.GET, params = "nameFilter")
    public List<Contact> getSortedPage(@RequestParam("nameFilter") String nameFilter){
        return super.getAllSorted(nameFilter);
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<Contact> getAllPage(){
        return super.getAll();
    }
}

travis.yml

language: java
script: mvn clean verify
jdk: oraclejdk8

services:
- postgresql

before_script:
- psql -c 'create database hello;' -U postgres

还有 app.properties:

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/hello
spring.datasource.username=postgres
spring.datasource.password=password

spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect

我检查了与 PostgreSQL DB 的连接并且它工作正常(你可以在图片上看到它),但我没有在测试中使用实体管理器。 谁能告诉我这是什么?

【问题讨论】:

    标签: postgresql hibernate spring-mvc spring-boot travis-ci


    【解决方案1】:

    我建议使用 H2 而不是 PostgreSQL 进行集成测试。你不需要设置任何 TravisCI 服务。您的测试套件将更易于维护,并且不依赖于外部服务。你也可以使用H2's compatibility mode with PostgreSQL

    【讨论】:

    • 感谢您的回答,但我想知道,如何使用 PostgreSQL 避免此异常。
    • 我解决了我的问题。我将我的项目分为两个配置文件(测试和发布),并为每个配置文件创建了自己的数据库。这样,我有 PostgreSQL 数据库用于“发布”配置文件和 H2 数据库用于“测试”配置文件,因为我发现 Travis 在运行项目时使用嵌入式数据库。谢谢@luboskmac。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2018-03-14
    相关资源
    最近更新 更多