Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring启动-事务管理不工作_Java_Spring Boot_Transactions_Spring Data Jpa - Fatal编程技术网

Java Spring启动-事务管理不工作

Java Spring启动-事务管理不工作,java,spring-boot,transactions,spring-data-jpa,Java,Spring Boot,Transactions,Spring Data Jpa,期望-延迟加载不应该在事务范围外工作(例如在rest控制器中),但它可以工作 问题是@Transactional中的,在我的配置中,spring应用程序没有使用它。我怎么能修好它 …Rest控制器没有任何事务方法,它只使用指定的服务来加载实体。未在服务中加载的依赖集合应为空 应用入门级: @SpringBootApplication @EntityScan("com.vl.pmanager.domain.model") @EnableJpaRepositories("com.vl.pmanage

期望-延迟加载不应该在事务范围外工作(例如在rest控制器中),但它可以工作

问题是@Transactional中的,在我的配置中,spring应用程序没有使用它。我怎么能修好它

…Rest控制器没有任何事务方法,它只使用指定的服务来加载实体。未在服务中加载的依赖集合应为空

应用入门级:

@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> {
}
我知道只有5个额外的请求,因为我只有5个依赖于标记项目信息。 因此,作为结论,我的事务级别不管理事务。 …我已经检查了datasource和transactionManager注入bean-这些bean已创建。 开始时间或运行时间没有错误,没有警告…

@JBNizet


这不是交易问题。在文档中查找属性spring.jpa.open-in-view,如果不需要,将其设置为false

我已将此答案添加到标记问题,如“已关闭”。如果我关闭jpa open in view,事务将按我预期的方式工作


对于主持人,请将作者答案更改为@JBNizet

您的问题太长,而且不清楚。您正在执行什么代码,您期望它做什么,以及它做什么?期望-延迟加载不应该在事务范围之外工作。但它在控制器级别工作。似乎@Transactional annotation在我的配置中不起作用……这不是事务问题。在文档中查找属性spring.jpa.open-in-view,如果不需要,请将其设置为false。请注意,集合不会像您认为的那样为空。它将不会初始化,尝试访问其元素将抛出ExceptionEAA:)。谢谢你的帮助。它现在对我起作用了,因为它应该起作用。
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);
    }
}
// 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=?