hibernate many relationship get duplicate data in one of the query, the number is the number of repeat multiport data solutions with @Fetch (FetchMode.SUBSELECT)

First look at the data table

Only two sections table data

Plate 1 is recommended, no child below the columns

Section 2 below the 14 sub-sections

At one end to a query, the findings have 16

That is, plate 1+ 2+ forum sections 2 and his 14 subsets are listed, and this is obviously incorrect

 

Plate configuration object

    @OneToMany (CascadeType.ALL in Cascade = {}, = FetchType.EAGER FETCH) 
    @JoinColumn (name = "sectionId") // foreign key in ColumnMoDel for association

 I guess what, there is an urgent load caused

Charles statement is required

Hibernate: 
    select
        this_.id as id1_17_1_,
        。。。。from
        t_section this_ 
    left outer join
        t_column columnmode2_ 
            on this_.id=columnmode2_.sectionId
Hibernate: 
    select
        columnmode0_.id as id1_7_,
       。。。。from
        t_column columnmode0_ 
    where
        columnmode0_.sectionid='1'

 

 

This policy is configured to delete

@OneToMany (CascadeType.ALL in Cascade = {} ) 
    @JoinColumn (name = "sectionId") // foreign key in ColumnMoDel for association

 

Or using lazy loading

@OneToMany (CascadeType.ALL in Cascade = {}, = FETCH to FetchType.LAZY) 
    @JoinColumn (name = "sectionId") // foreign key in ColumnMoDel for association

 

You can solve this problem of duplicate data sets

However, this has not acquired a collection of multi-party

Does not solve the problem looks like this

After inquiries that, can solve this problem using @Fetch

@OneToMany (Cascade = {} CascadeType.ALL in) 
    @JoinColumn (name = "sectionId") // columnModel the foreign key for associating 
    @Fetch (FetchMode.SUBSELECT)

 In the console that issued the query is two

Hibernate: 
    select
        this_.id as id1_17_0_,
        。。。。。from
        t_section this_
Hibernate: 
    select
        columnmode0_.id as id1_7_,
       。。。。from
        t_column columnmode0_ 
    where
        columnmode0_.sectionid='1'

If the notes and then change it

 @OneToMany ( Cascade  = {of CascadeType. ALL }, FETCH  = FetchType.EAGER)
     @JoinColumn (name = "sectionId") // foreign key ColumnMoDel is, for associating
     @Fetch (FetchMode.SUBSELECT)

Statement is changed again 

Hibernate: 
    select
        this_.id as id1_17_0_,
       。。。。from
        t_section this_
Hibernate: 
    select
        columnmode0_.sectionId as sectionI7_17_1_,
        。。。。from
        t_column columnmode0_ 
    where
        columnmode0_.sectionId in (
            select
                this_.id 
            from
                t_section this_
        )

 As query efficiency, currently there are no time to deal with, and then have time analyzer test, but up analysis from the statement, @ Fetch (FetchMode.SUBSELECT) with acute and certainly not as loaded with lazy load faster

Guess you like

Origin www.cnblogs.com/jnhs/p/11404572.html