gpt4 book ai didi

java - JPA hibernate内部执行多选择查询

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

问题陈述:在JPA hibernate中我执行一个方法

联系人 contact =entityManager.find(Contact.class, Integer.valueOf(contactId));

正如预期的那样,EntityManager 触发一个选择查询来获取联系人对象,但之后它触发另一个选择查询来获取我不想要的客户对象。
客户对象是联系人对象中的子对象。
联系人条目有一个外键 customerId。

要求:我希望entityManager触发一个选择查询来获取联系人对象,之后不执行任何操作,既没有第二个选择查询也没有连接查询。联系对象:

@Entity
@Table(name = "contact")
public class Contact {
@JsonProperty("contactId")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Id")
int contactId;

@Column(name = "firstname")
@JsonProperty("firstName")
String firstName;

@Column(name = "lastname")
@JsonProperty("lastName")
String lastName;

@Column(name = "phone1")
@JsonProperty("phone1")
String phone1;

@ManyToOne(optional = false, fetch = FetchType.LAZY, targetEntity = Customer.class)
@JoinColumn(name = "customer_id", updatable = false)
@Fetch(FetchMode.JOIN)
Customer customer;

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}
}

最佳答案

我认为您的问题来自于将 @Fetch(FetchMode.join) 与延迟加载一起使用。如果您不想急切地加载 Customer,那么您应该删除 @Fetch(FetchMode.join) 并仅使用延迟加载

在此处查看有关此内容的更多信息 ( https://stackoverflow.com/a/29667050/2637940 ):

First of all, @Fetch(FetchMode.JOIN) and @ManyToOne(fetch = FetchType.LAZY) are antagonistic, one instructing an EAGER fetching, while the other suggesting a LAZY fetch.

关于java - JPA hibernate内部执行多选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47735447/

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