문제 문을 실행 : JPA에서 I이 방법을 실행 동면JPA 최대 절전 여러 선택 쿼리를 내부적
Contact contact = entityManager.find(Contact.class, Integer.valueOf(contactId));
가 다른 선택을 발사 이후이지만, 접촉의 목적을 얻기 위해 EntityManager
화재 선택 쿼리 예상대로 쿼리를 통해 내가 원하지 않는 고객 오브젝트를 얻을 수 있습니다.
고객 개체는 연락처 개체의 자식 개체입니다.
contact entiry에는 외래 키 customerId가 있습니다.
Requirment : 하나의 선택 쿼리를 실행하여 연락처 개체를 가져온 다음 두 번째 아무것도 선택하지 않아도 아무것도 수행하지 않고 join 쿼리를 쿼리하지 않도록합니다. 연락처 개체 :
@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;
}
}
아니요, find()를 실행할 때 해당 쿼리를 실행하지 않습니다. JSON에 컨택을 직렬화 할 때 쿼리를 실행할 가능성이 높습니다. 컨택의 고객을 직렬화하지 않도록 잭슨에게 말하지 않았기 때문에 잭슨이 직렬화하므로 Hibernate는 고객 상태를로드하기 위해 qery를 실행해야합니다. –
연락처를 찾는 데 사용 된 코드를 추가 할 수 있습니까? –