【问题标题】:Inner Join query Not Working inside JPA custom query内部联接查询在 JPA 自定义查询中不起作用
【发布时间】:2021-11-11 08:32:58
【问题描述】:

我想使用 JPA 自定义查询执行内部联接查询,当我 定义内部联接查询在 @Query() 内部它会抛出一个异常的结果。 当我在 MYSQL yog 编辑器中编写相同的查询时,此查询工作正常 ,没有问题。但是使用 JPA 生成问题 在我的数据库表组织中存储 address_id 作为外键。 address_id 列名自动生成,没有我定义的地方 address_id 在我的实体类中。当我通过调用触发内部联接查询时 organization.address=Address.id 显示以下错误类型

Type mismatch: com.nilmani.onetooneunidirectional.entiry.Address type is expected 

这里是查询 组织存储库.kt

package com.nilmani.onetooneunidirectional.repository

import com.nilmani.onetooneunidirectional.entiry.Organization
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface OrganizationRepository : JpaRepository<Organization,Long> {
    @Query("SELECT Organization .id,Organization .name,Address .building,Address .city FROM  Organization INNER JOIN Address WHERE Organization.address =Address .id")
    fun findSomeRelationalFeatures()
}

组织.kt

package com.nilmani.onetooneunidirectional.entiry

import javax.persistence.*

@Entity
data class Organization(
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    val id:Long=-1,
    var name:String="",
    var orgId:String="",
    @OneToOne(targetEntity = Address::class,cascade = [CascadeType.ALL])
    var address: Address
)

地址.kt

package com.nilmani.onetooneunidirectional.entiry

import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

@Entity
data class Address(
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    val id:Long=-1,
    val building:String="",
    val street:String="",
    val city:String="",
    val state:String="",
    val country:String="",
    val zipCode:String=""
)

【问题讨论】:

    标签: mysql jpa


    【解决方案1】:

    试试这个:

    select o from Organization o inner join fetch o.address
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 2014-06-10
      • 2020-09-24
      • 2016-04-06
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      相关资源
      最近更新 更多