gpt4 book ai didi

hibernate - 为什么 Hibernate 选择相同的列 4 次?帮助绘制 map ?

转载 作者:行者123 更新时间:2023-12-02 23:40:06 25 4
gpt4 key购买 nike

我的类(class)看起来像这样。为什么同一列被选中 4 次?映射有什么问题?

@Entity @Table(name="CLIENTS")
public class Client implements Serializable {

@Id @GeneratedValue @Column(name="GENERATED_ID")
private Long id;

@Column(name="NAME")
private String name;

@OneToMany(cascade=CascadeType.ALL, mappedBy="client", fetch=FetchType.EAGER)
private Map<ParamPK, Param> params = new HashMap<ParamPK, Param>();
}

@Entity @Table(name="PARAMS")
public class Param implements Serializable {

@EmbeddedId
private ParamPK paramPK;

@Column(name="VALUE")
private String value;

@ManyToOne @MapsId("clientId")
private Client client;
}

@Embeddable
public class ParamPK implements Serializable {

@Column(name="PARAM_KEY")
private String key;

@Column(name="CLIENT_GENERATED_ID")
private Long clientId;
}

select 生成的查询获取同一列 4 次。

/* from Client */ 
select
client0_.GENERATED_ID as GENERATED1_1_,
client0_.NAME as NAME1_
from
CLIENTS client0_

/* load one-to-many Client.params */
select
params0_.client_GENERATED_ID as client3_1_1_,
params0_.client_GENERATED_ID as client3_1_,
params0_.PARAM_KEY as PARAM1_1_,
params0_.CLIENT_GENERATED_ID as CLIENT3_1_,
params0_.client_GENERATED_ID as client3_0_0_,
params0_.PARAM_KEY as PARAM1_0_0_,
params0_.VALUE as VALUE0_0_
from
PARAMS params0_
where
params0_.client_GENERATED_ID=?

注意使用 Hibernate 3.5.3。其余样板代码已因不相关而被删除。

最佳答案

您忘记告诉 Hibernate 参数映射的键是什么。将以下注释添加到该 map :

@OneToMany(cascade=CascadeType.ALL, mappedBy="client", fetch=FetchType.EAGER)
@MapKey(name = "paramPK")
private Map<ParamPK, Param> params = new HashMap<ParamPK, Param>();

这告诉 Hibernate Param 实体的 paramPK 属性是映射的键。

关于hibernate - 为什么 Hibernate 选择相同的列 4 次?帮助绘制 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7191351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com