在最新的Spring Data JPA版本3.0.2中,@OneToMany注解与多个关联列存在绑定错误。

huangapple go评论134阅读模式
英文:

Incorrect binding for @OneToMany with multiple join columns in the latest spring data jpa version 3.0.2

问题

以下是我的实体类,我在其中使用@OneToMany注释,但绑定不正确。ref_no和seq_no是我的绑定参数。但绑定值的顺序混乱。ref_no的值绑定到seq_no,seq_no的值绑定到ref_no。

@OneToMany(fetch = FetchType.EAGER)
@JoinColumns({
    @JoinColumn(name = "REF_NO", referencedColumnName = "REF_NO"),
    @JoinColumn(name = "SEQ_NO", referencedColumnName = "SEQ_NO")
})
public Set<InProcessPartyEntity> getParties() {
    return parties;
}

输出跟踪日志:

Hibernate: select p1_0.seq_no, p1_0.ref_no, p1_0.party_code, p1_0.party_id from rbi_trade.ot_txn_party p1_0 where (p1_0.seq_no, p1_0.ref_no) in (?, ?)
2023-03-09T08:31:42.540+01:00 TRACE 15588 --- [scheduling-1] org.hibernate.orm.jdbc.bind : binding parameter [1] as [VARCHAR] - [E313100005323221237]
2023-03-09T08:31:42.540+01:00 TRACE 15588 --- [scheduling-1] org.hibernate.orm.jdbc.bind : binding parameter [2] as [VARCHAR] - [001]

在相同的实体类中,我还使用@OneToOne注释,具有多个连接列,它运行得非常好,并且导致正确的绑定参数值。

@OneToOne(fetch = FetchType.EAGER)
@JoinColumns({
    @JoinColumn(name = "REF_NO", referencedColumnName = "REF_NO"),
    @JoinColumn(name = "SEQ_NO", referencedColumnName = "SEQ_NO")
})
public InProcessWorkUnitEntity getWorkUnit() {
    return workUnit;
}

输出跟踪日志:

Hibernate: select i1_0.ref_no, i1_0.seq_no, i1_0.reg_by from rbi_trade.ot_workunit i1_0 where (i1_0.ref_no, i1_0.seq_no) in (?, ?)
2023-03-09T08:31:42.524+01:00 TRACE 15588 --- [scheduling-1] org.hibernate.orm.jdbc.bind : binding parameter [1] as [VARCHAR] - [E313100005323221237]
2023-03-09T08:31:42.525+01:00 TRACE 15588 --- [scheduling-1] org.hibernate.orm.jdbc.bind : binding parameter [2] as [VARCHAR] - [001]

我自从升级到Spring Boot JPA 3.0.2以后一直遇到这个问题。之前我使用的是版本2.7.2,在那个版本中@OneToMany的绑定正常工作。有什么建议吗?

英文:

Following is my entity class where I am using @OneToMany annotation, which leads to incorrect binding. ref_no and seq_no are my binding parameters. But the binding values are out of order. Value for ref_no binds with seq_no and value for seq_no binds with ref_no.

    @OneToMany(fetch = EAGER)
    @JoinColumns({
            @JoinColumn(name = &quot;REF_NO&quot;,referencedColumnName = &quot;REF_NO&quot;),
            @JoinColumn(name = &quot;SEQ_NO&quot;,referencedColumnName = &quot;SEQ_NO&quot;)
    })
    public Set&lt;InProcessPartyEntity&gt; getParties() {
        return parties;
    }

Output trace log:

Hibernate: select p1_0.seq_no,p1_0.ref_no,p1_0.party_code,p1_0.party_id from rbi_trade.ot_txn_party p1_0 where (p1_0.seq_no,p1_0.ref_no) in((?,?))
2023-03-09T08:31:42.540+01:00 TRACE 15588 --- [ scheduling-1] org.hibernate.orm.jdbc.bind : binding parameter [1] as [VARCHAR] - [E313100005323221237]
2023-03-09T08:31:42.540+01:00 TRACE 15588 --- [ scheduling-1] org.hibernate.orm.jdbc.bind : binding parameter [2] as [VARCHAR] - [001]

In the same entity class I am also using @OneToOne annotation with multiple join columns, which works perfectly fine and leads to the correct binding parameters values

    @OneToOne(fetch = EAGER)
    @JoinColumns({
            @JoinColumn(name = &quot;REF_NO&quot;,referencedColumnName = &quot;REF_NO&quot;),
            @JoinColumn(name = &quot;SEQ_NO&quot;,referencedColumnName = &quot;SEQ_NO&quot;)
    })
    public InProcessWorkUnitEntity getWorkUnit() {
        return workUnit;
    }

Output trace log:

Hibernate: select i1_0.ref_no,i1_0.seq_no,i1_0.reg_by from rbi_trade.ot_workunit i1_0 where (i1_0.ref_no,i1_0.seq_no) in((?,?))
2023-03-09T08:31:42.524+01:00 TRACE 15588 --- [ scheduling-1] org.hibernate.orm.jdbc.bind : binding parameter [1] as [VARCHAR] - [E313100005323221237]
2023-03-09T08:31:42.525+01:00 TRACE 15588 --- [ scheduling-1] org.hibernate.orm.jdbc.bind : binding parameter [2] as [VARCHAR] - [001]

I am facing this issue since I upgraded to spring boot jpa 3.0.2. I was using version 2.7.2 before where the binds with @OneToMany worked fine.

Any suggestions?

答案1

得分: 0

我没有足够的声誉积分来留下评论,所以我会在答案中写下我的考虑。

这种关系是双向的吗?如果您能分享InProcessPartyEntity类的代码,那将很有帮助。实际上,如果您能至少分享两个实体的属性和它们的映射,那将非常有帮助。

当我更新到JPA 3时,我注意到一个问题,Hibernate在验证注解映射时变得更加严格。

英文:

I don't have enough reputation points to leave a comment, so I'll write my considerations in an answer.

Is the relationship bidirectional? It would help if you could share the InProcessPartyEntity class code too. Actually, it would help a lot if you could share at least both entities properties and their mappings.

One thing I noticed when updating to JPA 3 was that Hibernate became stricter when validating annotations mapping.

答案2

得分: 0

这个问题的解决方法是从Id类中移除@Id注解。
这个问题将在新版本的Hibernate ORM中修复,即如果实体被@IdClass注释,需要忽略ID类中的@Id标记。

更多细节可以在我的帖子评论部分找到,链接在这里:https://hibernate.atlassian.net/browse/HHH-16274

英文:

The solution for this issue is to remove @Id annotation from the Id class.
This issue will be fixed in a newer version of Hibernate ORM. i.e. if the entity is annoted with the @IdClass , the tag @Id from the ID class needs to be ignored.

More details can be found in the comments section of my post here: https://hibernate.atlassian.net/browse/HHH-16274

huangapple
  • 本文由 发表于 2023年3月9日 15:41:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定