- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
期望 - 延迟加载不应在事务范围之外工作(例如在其余 Controller 中),但它可以工作。
问题出在@Transactional中,在我的配置中Spring应用程序没有使用它。我该如何修复它?
...Rest Controller 没有任何事务方法,它仅使用指定的服务来加载实体。如果未在服务中加载,则依赖集合应为空。
应用程序入门类:
@SpringBootApplication
@EntityScan("com.vl.pmanager.domain.model")
@EnableJpaRepositories("com.vl.pmanager.domain.repository")
@EnableTransactionManagement
public class ProjectManagerApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectManagerApplication.class, args);
}
}
我知道 spring boot 自动配置存储库并扫描实体,但我希望添加...
@EntityScan("com.vl.pmanager.domain.model")
@EnableJpaRepositories("com.vl.pmanager.domain.repository")
我还尝试将@Transactional添加到存储库界面,但它对我不起作用
package com.vl.pmanager.domain.repository;
import com.vl.pmanager.domain.model.Tag;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public interface TagRepository extends PagingAndSortingRepository<Tag, Long> {
}
所以我从存储库中删除了@Transactional,创建了其他服务层来使用注释对其进行管理并将服务注入(inject)到 Controller :
package com.vl.pmanager.domain.db.service.api;
import com.vl.pmanager.domain.model.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface ITagManager {
Page<Tag> findAll(Pageable pageable);
Tag findOne(Long id);
}
// ======================== TAG SERVICE WRAPPED WITH TRANSACTION ==========================
package com.vl.pmanager.domain.db.service;
import com.vl.pmanager.domain.db.service.api.ITagManager;
import com.vl.pmanager.domain.model.Tag;
import com.vl.pmanager.domain.repository.TagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class TagManager implements ITagManager {
@Autowired
private TagRepository tagRepository;
@Override
public Page<Tag> findAll(Pageable pageable) {
return tagRepository.findAll(pageable);
}
@Override
public Tag findOne(Long id) {
return tagRepository.findOne(id);
}
}
// ====================== REST CONTROLLER ============================
package com.vl.pmanager.web;
import com.vl.pmanager.domain.db.service.api.ITagManager;
import com.vl.pmanager.domain.model.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/tags")
public class TagController {
private static final int DEFAULT_PAGE_SIZE = 50;
@Autowired
private ITagManager tagManager;
@RequestMapping(method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
public Page<Tag> getTags(@PageableDefault(size = DEFAULT_PAGE_SIZE) Pageable pageable) {
return tagManager.findAll(pageable);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
public Tag getTag(@PathVariable("id") Long id) {
return tagManager.findOne(id);
}
}
Tag 实体有一个字段 Set projectInfo 映射到具有 @ManyToMany 关系的其他实体,且 FetchType fetch() 默认 LAZY; 因此返回结果不能包含依赖实体,但它确实包含。
我还监控了数据库日志:
// make request - load data using db service to controller level
Hibernate: select count(tag0_.id) as col_0_0_ from tag tag0_
Hibernate: select tag0_.id as id1_6_, tag0_.description as descript2_6_, tag0_.name as name3_6_ from tag tag0_ limit ?
// converting data to JSON automatically
Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
我知道只有 5 个额外请求,因为我只有 5 个依赖于 tag project_info。因此,作为结论,我的交易级别不管理交易。...我已经检查了注入(inject)bean的数据源和transactionManager - 已创建。启动时或运行时没有错误、没有警告...
最佳答案
@JBNizet
这不是交易问题。在文档中查找属性 spring.jpa.open-in-view ,如果不需要则将其设置为 false
我已添加此答案以将问题标记为已关闭。如果我关闭jpa open in view,事务就会像我预期的那样工作。
对于版主请将作者答案更改为@JBNizet
关于java - Spring Boot - 事务管理不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38208545/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!