Home > Mobile >  Spring Data JPA how to avoid loading a column
Spring Data JPA how to avoid loading a column

Time:01-25

I have two entities:

@Entity
@Table(name = "locations")
@Data
@NoArgsConstructor
@RequiredArgsConstructor
public class Location {

    @Id
    @Column(name = "location_id", nullable = false, unique = true)
    @Type(type="org.hibernate.type.UUIDCharType")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID locationId;

    @NonNull
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.ms")
    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;

    @NonNull
    @Type(type="org.hibernate.type.UUIDCharType")
    @Column(name = "user_id", nullable = false)
    private UUID userId;

    @NonNull
    @Column(name = "latitude", nullable = false)
    private Double latitude;

    @NonNull
    @Column(name = "longitude", nullable = false)
    private Double longitude;
}

and

@Entity
@Table(name = "users")
@Data
@NoArgsConstructor
@RequiredArgsConstructor
public class User {

    @NonNull
    @Id
    @Type(type = "org.hibernate.type.UUIDCharType")
    @Column(name = "user_id", nullable = false, unique = true)
    private UUID userId;

    @NonNull
    @Column(name = "email", length = 100, nullable = false, unique = true)
    private String email;

    @NonNull
    @Column(name = "first_name", nullable = false)
    private String firstName;

    @NonNull
    @Column(name = "second_name", nullable = false)
    private String secondName;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", referencedColumnName = "user_id")
    private List<Location> locations;
}

and then i have

@Repository
public interface UserRepository extends JpaRepository<User, UUID> {
    User save(User user);
}

userRepository.save(user) - this method return User with all locations which referenced to this user.

I want to avoid this join query in sql since there could be a lot of referenced locations to the user. This could be cause of OutOfMemory etc. So, i want to get only User without List locations after userRepository.save(user)

I tried to write query this way:

@Query(nativeQuery = true,
        value = "update users "  
                "set email = :email, first_name = :firstName, second_name = :secondName "  
                "where user_id = :userId "  
                "returning user_id, email, first_name, second_name;")
    User update(
        @Param("userId") @NonNull String userId,
        @Param("firstName") @NonNull String firstName,
        @Param("secondName") @NonNull String secondName,
        @Param("email") @NonNull String email
    );

but got the same result with locations in response from DB

This is a hibernate log:

Hibernate: 
    /* dynamic native SQL query */ update
        users 
    set
        email = ?,
        first_name = ?,
        second_name = ? 
    where
        user_id = ? returning user_id, email, first_name, second_name;
Hibernate: 
    select
        locations0_.user_id as user_id5_0_0_,
        locations0_.location_id as location1_0_0_,
        locations0_.location_id as location1_0_1_,
        locations0_.created_at as created_2_0_1_,
        locations0_.latitude as latitude3_0_1_,
        locations0_.longitude as longitud4_0_1_,
        locations0_.user_id as user_id5_0_1_ 
    from
        locations locations0_ 
    where
        locations0_.user_id=?

CodePudding user response:

Most probably, you get the locations field loaded due to debugging. Debugging triggers get method and it makes hibernate to load all locations.

  • Related