gpt4 book ai didi

java - 如何使用 FetchType.EAGER

转载 作者:行者123 更新时间:2023-11-30 03:35:45 26 4
gpt4 key购买 nike

我无法从数据库加载对象及其相对映射对象。可能我的“ map 系统”是错误的。

在数据库中保存对象(及其相对映射对象)时,一切正常,因此对象似乎已正确映射。

Utente是我的“主”对象

   public class Utente implements Serializable{
.......
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_utente", unique = true, nullable = false)
private Integer idUtente;
.......

我有另一个名为 Autenticazione 的对象,我在其中将 Autenticazione 链接到 Utente

public class Autenticazione implements Serializable{    
....
@OneToOne(targetEntity=Utente.class)
@JoinColumn(name="utente", referencedColumnName="id_utente")
private Utente utente;
...

当我从数据库获取 Utente 对象时,我还想获取它的 Autenticazione 对象。我尝试了一些代码(为避免混淆而未在此发布),但没有找到解决方案。

最佳答案

您还需要添加从 Utente 到 Autenticazione 的关联:

public class Utente implements Serializable{
.......
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_utente", unique = true, nullable = false)
private Integer idUtente;
.......

@OneToOne(mappedBy = "utente")
private Autenticazione autenticazione;

确保使用辅助方法设置关联的两侧,例如您应该添加到 Utente 的方法:

    public void addAutenticazione(Autenticazione autenticazione) {
autenticazione.setUtente(this);
this.autenticazione = autenticazione;
}

关于java - 如何使用 FetchType.EAGER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27942284/

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