2017-12-18 5 views
0

트랜잭션 경계 구분은 세션 수명에 영향을 줍니까? 그것을 테스트하는@Transactional을 사용하여 LazyInitializationException을 수정할 수 있습니까?

@Entity 
public class Human { 
    @Id 
    @GeneratedValue 
    private Long id; 

    @OneToMany(cascade = { CascadeType.ALL }) 
    private List<Hobby> hobbies = new ArrayList<>(); 

    // getters/setters 
} 


@Entity 
public class Hobby { 
    @Id 
    @GeneratedValue 
    private Long id; 
    private String name; 

    // getters/setters 
} 

그리고 어떤 서비스 :

@SpringBootApplication 
public class SpringbootTransactionsApplication implements CommandLineRunner { 

    @Autowired 
    private HumanRepo humanRepo; 

    public static void main(String[] args) { 
     SpringApplication.run(SpringbootTransactionsApplication.class, args); 
    } 

    @Override 
    public void run(String... args) throws Exception { 
     foo(); 
    } 

    @javax.transaction.Transactional 
    public void foo() { 
     Human human = new Human(); 
     human.getHobbies().add(new Hobby()); 
     humanRepo.save(human); 

     for (Human h : humanRepo.findAll()) { 
      System.out.println(h.getHobbies()); 
     } 
    } 

} 

그래서 나는 메소드 foo는 같은 놓친 데이터 (취미)를 가져 프레임 워크를 기대

나는 몇 가지 간단한 스프링 부팅 응용 프로그램이 거래로 표시됩니다. 하지만 실패했습니다 org.hibernate.LazyInitializationException: failed to lazily initialize a collection 내가 놓친 게 있니?

답변

0

@EnableTransactionManagement 및 을 사용하여 트랜잭션 관리 사용 @javax.transaction.Transactional 대신 @org.springframework.transaction.annotation.Transactional 주석을 사용하십시오.

관련 문제