2017-12-18 6 views
0

トランザクション境界の境界はセッションの有効期間に影響しますか?@ TransactionalでLazyInitializationExceptionを修正できますか?

私はいくつかの簡単な春ブーツアプリがあります。だから私は、メソッドfooとして(趣味)ミスしたデータをフェッチするためのフレームワークを期待し

@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()); 
     } 
    } 

} 

@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 
} 

そして、それをテストするためのいくつかのサービスをトランザクションとしてマークされます。しかし失敗したorg.hibernate.LazyInitializationException: failed to lazily initialize a collection私は何かが恋しいですか?

答えて

0

@EnableTransactionManagementと を使用してトランザクション管理を有効にする@javax.transaction.Transactionalの代わりに@org.springframework.transaction.annotation.Transactional注釈を使用してください。

関連する問題