我有一个基本的SpringBoot应用程序。使用Spring Initializer,内嵌Tomcat,Thymeleaf模板引擎,并打包为可执行的JAR文件。
使用以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
我创建了这个声明为readOnly的服务:
@Service
@Transactional(readOnly = true)
public class TimeLapseService {
@Autowired
TimeLapseRepository timeLapseRepository;
public Set<TimeLapse> findAllByCompanyId(long companyId) {
return timeLapseRepository.findAllByCompanyId(companyId);
}
public Iterable<TimeLapse> findAll (User user) {
if (user.isAdmin()) {
return timeLapseRepository.findAll();
} else {
return timeLapseRepository.findAllByCompanyId(user.getCompany().getId());
}
}
public void createTimeLapse (TimeLapse timeLapse) {
timeLapseRepository.save (timeLapse);
}
}
并且
public interface TimeLapseRepository extends CrudRepository<TimeLapse, Long> {
....
}
据我所知,由于该服务被声明为只读,因此创建新服务不应该将任何内容保存到数据库中,但它会在表中创建一行数据。
timeLapseService.createTimeLapse(timeLapse24h);
JPA 属性:
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
hibernate.dialect=org.hibernate.dialect.H2Dialect