不尊重Kotlin JPA Lazy RestAPI [英] Kotlin JPA Lazy RestAPI not respected

查看:73
本文介绍了不尊重Kotlin JPA Lazy RestAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java项目,正在转换为Kotlin,但是具有关系并映射@ManytoOne的实体存在问题.他们正在使用映射类型为LAZY的实体,不是在尊重LAZY并执行查询.

I have a project in Java and i am converting to Kotlin, but the entities with relations and mapping @ManytoOne they are with problems. The entities they are using mapping fecht type LAZY, aren't they respecting the LAZY and executing the query.

示例:

实体父亲:

@Entity
@Table(name = "TIPOSITUACAO")
data class TipoSituacao (
    @Id
    @Column(name = "ID")
    val id: Long? = null,

    @Column(name = "DESCRICAO")
    val descricao: String? = null
)

实体儿子:

@Entity
@Table(name = "SITUACAO")
data class Situacao (

    @Id
    @Column(name = "ID")
    val id: Long,

    @Column(name = "DESCRICAO")
    val descricao: String = "",

    @Column(name = "TIPOSITUACAO_ID")
    val tipoSituacaoId: Long? = null,

    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JoinColumn(name = "TIPOSITUACAO_ID", referencedColumnName = "ID", insertable = false, updatable = false)
    val tipoSituacao: TipoSituacao? = null
)

我的终点:

@RequestMapping("/api")
@RestController
class EndPoints {
    @Autowired
    private val situacaoRepository: SituacaoRepository? = null

   val situacao: List<Situacao>
        @GetMapping(value = ["/situacao"])
        get() = situacaoRepository!!.findAll()
}

返回:

[
    {
        "id": 1,
        "descricao": "SITUACAO 1.1",
        "tipoSituacaoId": 1,
        "tipoSituacao": {
            "id": 1,
            "descricao": "TIPO SITUACAO 1"
        }
    },
    {
        "id": 2,
        "descricao": "SITUACAO 1.2",
        "tipoSituacaoId": 1,
        "tipoSituacao": {
            "id": 1,
            "descricao": "TIPO SITUACAO 1"
        }
    },
    {
        "id": 3,
        "descricao": "SITUACAO 2.1",
        "tipoSituacaoId": 2,
        "tipoSituacao": {
            "id": 2,
            "descricao": "TIPO SITUACAO 2"
        }
    }
]

查询:

Hibernate: select situacao0_.id as id1_0_, situacao0_.descricao as descrica2_0_, situacao0_.tiposituacao_id as tipositu3_0_ from situacao situacao0_
Hibernate: select tiposituac0_.id as id1_1_0_, tiposituac0_.descricao as descrica2_1_0_ from tiposituacao tiposituac0_ where tiposituac0_.id=?
Hibernate: select tiposituac0_.id as id1_1_0_, tiposituac0_.descricao as descrica2_1_0_ from tiposituacao tiposituac0_ where tiposituac0_.id=?

如果我使用 @JsonIgnore @JsonManageReference @JsonBackReference o直到未返回,直到查询继续执行.

If i use @JsonIgnore, @JsonManageReference or @JsonBackReference o son tipoSituacao until not return , but the query continue executing.

我尝试使用DTO,但存在相同的问题.

I tried using the DTO, but the same problem.

每次我使用situacaoRepository.find或findAll都会执行查询.

Every time i use situacaoRepository.find ou findAll the query is executed.

有问题的项目链接: https://github.com/maxbrizolla/spring- kotlin-jpa问题

有人可以帮助我吗?

推荐答案

这里的问题是您的数据类会自动生成toString,equals和hashcode方法,这些方法会触发属性加载,因此休眠会为此执行额外的必需查询.

The problem here is your data class gets autogenerated toString, equals and hashcode methods which trigger property loading so hibernate execute extra required queries for that.

不建议将数据类与JPA一起使用.

Data classes are not encouraged to be used with JPA.

这篇关于不尊重Kotlin JPA Lazy RestAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆