Ario
Ario

Reputation: 1940

Spring data jpa find() without join relationship

I wrote the below query and there is an m:n relationship between "device" and "account" but with Native Query it still returns results with Inner Join!

public interface DeviceRepository extends JpaRepository<Device, Long> {
        @Query(value = "select device.* from device #pageable",
                countQuery = "select count(*) from device",
                nativeQuery = true)
        Page<Device> findByNative(Pageable peagble);
}

It returns all both table fields! (this is the generated query by Hibernate)

SELECT 
    accounts0_.device_id AS device_i1_2_0_,
    accounts0_.account_id AS account_2_2_0_,
    account1_.id AS id1_0_1_,
    account1_.broker_id AS broker_i2_0_1_,
    account1_.is_deleted AS is_delet3_0_1_,
    account1_.user_id AS user_id4_0_1_
FROM
    device_account accounts0_
        INNER JOIN
    account account1_ ON accounts0_.account_id = account1_.id
WHERE
    accounts0_.device_id = ?

Actually I need this because I don't want to have accounts[] in my findAll just in findById

Upvotes: 4

Views: 1717

Answers (1)

Ario
Ario

Reputation: 1940

Solved with @SqlResultSetMapping

Upvotes: 2

Related Questions