admin 管理员组文章数量: 1086019
This is the infra module from a hexagonal architecture that contains all from database related code.
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories("be.inbo.watervogels.repository")
@EntityScan("be.inbo.watervogels.repository.entity")
@EnableJpaAuditing
public class PersistenceConfig {
}
Repository implementation, interface imported from other module
package be.inbo.watervogels.repository.impl;
import be.inbo.watervogels.domain.SurveySummary;
import be.inbo.watervogels.repository.SurveyRepository;
import be.inbo.watervogels.repository.entity.SurveySummaryViewEntity;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import .springframework.beans.factory.annotation.Autowired;
import .springframework.data.domain.PageImpl;
import .springframework.data.domain.PageRequest;
import .springframework.data.domain.Pageable;
import .springframework.data.domain.Sort;
import .springframework.stereotype.Repository;
import java.time.Clock;
import java.time.LocalDate;
import java.util.List;
@Repository
public class SurveyRepositoryImpl implements SurveyRepository {
@Autowired
private Clock clock;
@PersistenceContext
private EntityManager entityManager;
private final String SELECT_CURRENT_SEASON_QUERY = """
SELECT s.id, s.surveyname, ss.name, es.label, se.startDate, se.endDate
FROM SurveyEntity s
JOIN s.surveySeasons ss
JOIN ss.surveyEvents se
JOIN se.eventSequence es
WHERE se.endDate > ?1 and s.code not like 'ZSCH'
ORDER BY s.sequence, s.surveyname, es.sortOrder
""";
private final String COUNT_CURRENT_SEASON_QUERY = """
SELECT count(*)
FROM SurveyEntity s
JOIN s.surveySeasons ss
JOIN ss.surveyEvents se
JOIN se.eventSequence es
WHERE se.endDate > ?1 and s.code not like 'ZSCH'
ORDER BY s.sequence, s.surveyname, es.sortOrder
""";
public PageImpl<SurveySummary> getCurrentSeason(int page, int size, String sortBy, boolean ascending) {
Pageable pageable = PageRequest.of(page, size, ascending ? Sort.by(sortBy).ascending() : Sort.by(sortBy).descending());
List<SurveySummaryViewEntity> entities = this.entityManager.createQuery(SELECT_CURRENT_SEASON_QUERY, SurveySummaryViewEntity.class)
.setParameter(1, LocalDate.now(clock))
.setFirstResult(page * size)
.setMaxResults(size)
.getResultList();
long total = (long) this.entityManager.createQuery(COUNT_CURRENT_SEASON_QUERY)
.setParameter(1, LocalDate.now(clock))
.getSingleResult();
List<SurveySummary> surveySummaries = entities.stream()
.map(entity -> new SurveySummary(entity.getSurveyName(), entity.getSeasonName(), entity.getSeasonStartDate(), entity.getSeasonEndDate(), entity.getEventSequenceLabel()))
.toList();
return new PageImpl<>(surveySummaries, pageable, total);
}
}
build.gradle
plugins {
`java-library`
}
dependencies {
implementation(".springframework.boot:spring-boot-starter-data-jpa")
compileOnly(".projectlombok:lombok")
implementation("com.h2database:h2")
testImplementation("com.h2database:h2")
runtimeOnly("com.microsoft.sqlserver:mssql-jdbc")
annotationProcessor(".projectlombok:lombok")
testImplementation(".springframework.boot:spring-boot-starter-test")
testRuntimeOnly(".junit.platform:junit-platform-launcher")
testImplementation(".projectlombok:lombok")
implementation(project(":watervogels-backend-domain"))
testImplementation(project(":watervogels-backend-domain"))
}
tasks.withType<Test> {
useJUnitPlatform()
}
For simplicity I use h2 for now. My assumption was that as long as you import spring-boot-data-jpa, you set your properties correclty and import some kind of jdbc driver (h2 in my case), spring can figure out what needs to be set up.
spring.application.name=watervogels-backend-persistence
spring.datasource.driver-class-name=.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
本文标签:
版权声明:本文标题:spring boot - required a bean of type 'jakarta.persistence.EntityManagerFactory' that could not be found - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744083554a2530701.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论