0

Given the following


@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
@Audited
@Builder
public class User extends BaseEntity {
    @NotNull
    private String firstName;

    @OneToMany(fetch = FetchType.LAZY)
    @NotAudited
    @JoinColumn(name = "id")
    private List<UserAud> audit;
}
@Entity
@Table(name = "USER_AUD")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserAud extends BaseAudEntity {
    @Column(name = "FIRST_NAME")
    private String firstName;
}

and

@Repository
@RepositoryRestResource(path="users")
public interface UserRepository extends JpaRepository<User, Long> {}

I see that when I @Autowire UserRepository userRepository and use userRepository.findAll() audit field is lazy loaded. However when I access it thru @RepositoryRestResource all child audit are loaded as well.

e.g

{
  "_embedded": {
    "users": [
      {
        "id": 1,
        "firstName": "cfo"
        "_embedded": {
          "audit": [
            {
              "firstName": "cfo"
            }
          ]
        }
     ]
....

I could not find on whether this is possible at all thru @RepositoryRestResource or not. Off course this eager loading affects performance of controller. I am going to have many audit records.

Ideally I would like to have LAZY loading thru controller endpoint GET /api/users

and EAGER loading thru GET /api/users/1/audit

I later realized that most probably serialization is triggering getAudit I tried the following

    @Override
    public void configureHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {

        Hibernate5Module module = new Hibernate5Module(); // or Hibernate4Module ... depends on hibernate version you are using
        module.disable(Hibernate5Module.Feature.USE_TRANSIENT_ANNOTATION);
        module.enable(Hibernate5Module.Feature.FORCE_LAZY_LOADING);
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(module);

        messageConverters.add(new MappingJackson2HttpMessageConverter(mapper));
    }

and

    @Bean
    public Module hibernate5Module() {
        Hibernate5Module hibernate5Module = new Hibernate5Module();
        hibernate5Module.enable(Hibernate5Module.Feature.FORCE_LAZY_LOADING);
        return hibernate5Module;
    }

and

   @Bean
    public Jackson2ObjectMapperBuilder configureObjectMapper() {
        return new Jackson2ObjectMapperBuilder()
                .modulesToInstall(Hibernate5Module.class);
    }

but none of them worked :(

H4F
  • 91
  • 8
  • so I tried this article https://programmer.group/solution-of-spring-boot-jpa-entity-jackson-serialization-triggering-lazy-load.html unfortunately that did not help either – H4F Sep 18 '19 at 17:05
  • I have tried `@LazyCollection(LazyCollectionOption.TRUE)` on User.class but that did not have any effects – H4F Sep 18 '19 at 17:36
  • Possible duplicate of [Make collection propertie render as relation instead of property in json HAL representaion](https://stackoverflow.com/questions/57005713/make-collection-propertie-render-as-relation-instead-of-property-in-json-hal-rep) – Alan Hay Sep 19 '19 at 07:35

0 Answers0