mardi 27 février 2018

Eager load in JPA @Transactional test

I am trying to do a test where the data from one entity is loaded eager, so I have a code like the following:

Entity:

@Entity
@Table(name = "tableA")
@NamedEntityGraph(name = "tableA.tableB", attributeNodes = [NamedAttributeNode("tableB")])
class TableA (

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int = 0,

    @JoinColumn(name = "table_b_id", referencedColumnName = "id")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    var tableB: TableB? = null
)

Repository:

@Repository
interface tableARepository: JpaRepository<TableA, Int>, JpaSpecificationExecutor<TableA>{

    @EntityGraph(attributePaths = [ "tableB" ])
    fun findByModelId(modelId: Int) : List<DispersionDetail>
}

Test:

@Test
@Transactional
fun disperserTest(){
    //Here the code to create a tableA and the tableB
    val tableAList = tableARepository.findAll()
}

The problem is that when debug my code the result of the tableAList the attribute tableB is null but when delete the @Transactional annotation from the test then tableB is not null and is loaded eager just how I want.

My question is why this happened and how can load tableA with all his relations (tableB) with the @Transactional annotation in the test

Aucun commentaire:

Enregistrer un commentaire