gpt4 book ai didi

postgresql - 在 Travis CI、Spring Boot 和 PostgreSQL 中运行时出现 EntityManagerException

转载 作者:行者123 更新时间:2023-11-29 13:21:29 25 4
gpt4 key购买 nike

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

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is 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();
}
}

Controller :

@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

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

最佳答案

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

关于postgresql - 在 Travis CI、Spring Boot 和 PostgreSQL 中运行时出现 EntityManagerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40799006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com