【发布时间】: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