Is there any solution that enable LAZY FETCH when implementing Serializable

33 Views Asked by At

I have 3 Entities Name A ,B and BAudits (here is the code snippet):

@Entity
...
@BatchSize(size = 1000)
@Table(name = "TABLE_A")
@Getter
@Setter
@NoArgsConstructor
public class A implements Serializable
{
    ...
    @ManyToOne(fetch = FetchType.LAZY)
    @Cascade({ CascadeType.MERGE })
    @JoinColumn(name = "B_CODE", referencedColumnName = "B_CODE", insertable = false, updatable = false)
    @JsonBackReference
    private B b;
    ...
}

@Entity
...
@BatchSize(size = 1000)
@Table(name = "TABLE_B")
@Getter
@Setter
@NoArgsConstructor
public class B implements Serializable
{
    ...
    @OneToMany(mappedBy = "b", fetch = FetchType.LAZY)
    @Cascade({ CascadeType.MERGE })
    @JsonManagedReference
    @NotAudited
    private Set<BAudits> bAudits;
    ...
}

BAudits is just a normal entity

Where B_CODE(String) is the fk for A and B .When I try to run the following SQL:

SELECT * FROM A a where a.id IN (**);
[ ** is just a list of id]

I found that the lazy fetch will not work and tons of B will be fetched(and that is why I set the fetchtype as lazy)

I knew that the problems should be Serializable and I tried different ways to let it works (and it fails) but I the have no idea if there are any ways that can let the lazy fetch work.

  • for the dao. It extended JpaRepository<A, Long>
0

There are 0 best solutions below