【问题标题】:Spring data jpa find() without join relationshipSpring data jpa find() 没有连接关系
【发布时间】:2017-08-30 09:57:30
【问题描述】:

我编写了下面的查询,“设备”和“帐户”之间存在 m:n 关系,但使用 Native Query 它仍然返回带有 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);
}

它返回所有两个表字段! (这是 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 = ?

实际上我需要这个,因为我不想在 findById 中的 findAll 中拥有 accounts[]

【问题讨论】:

  • 你的意思是它仍然使用 Inner Join 返回结果?
  • 返回两个表中的所有字段,更新问题...
  • 这就是hibernate如何翻译你的问题,那么问题出在哪里?
  • 问题是本地查询,这里不是本地的!
  • 那么我该如何阻止加入呢?

标签: java mysql spring spring-data-jpa


【解决方案1】:

用@SqlResultSetMapping 解决

【讨论】:

    猜你喜欢
    • 2021-11-19
    • 2017-11-09
    • 1970-01-01
    • 2022-12-26
    • 2018-11-17
    • 2020-08-30
    • 1970-01-01
    • 2021-02-10
    • 2021-05-22
    相关资源
    最近更新 更多