2016-07-05 136 views
1

期望 - 延遲加載不應該超出事務範圍(例如,在其他控制器中),但它有效。Spring Boot - 事務管理不起作用

問題是@Transactional,它沒有被我的配置中的彈簧應用程序使用。我該如何修復它?

...其餘控制器沒有任何事務性方法,它只使用specifiedServices來加載實體。如果未加載服務,則依賴收集應爲空。

應用啓動器類:

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

我知道,春天開機自動配置存儲庫和掃描實體,但我希望在加...

@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從庫中,創建了其它服務層與註釋來管理它,並注入服務控制器:

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

標籤實體有一個場集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只依賴於標籤project_info。 所以結論我的交易水平不管理交易。 ...我檢查了數據源和transactionManager注入bean - 創建了一個。 沒有錯誤,在啓動時或運行時沒有警告......

+0

你的問題太長,不清晰。你正在執行什麼代碼,你期望它做什麼,它做什麼呢? –

+0

期望值 - 延遲加載不應超出事務範圍。但它在控制器級別工作。似乎@Transactional註釋不起作用在我的配置.... – Sergii

+1

這不是一個交易問題。在文檔中查找spring.jpa.open-in-view屬性,如果不需要,則將其設置爲false。 –

回答

1

@JBNizet

這不是一個交易的問題。在文檔中查找屬性spring.jpa.open-in-view並將其設置爲false,如果您不需要它

我已添加此答案來標記問題,如關閉。如果我關閉jpa在視圖中打開事務工作正如我所料。

對於主持人請更改筆者的答案@JBNizet