I was wondering, How does hibernate query the table internally when we query on a column which is foreign key. Please see the code for more understanding
Employee Table
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
@Id
private String userId;
private String name;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinTable(
name = "department",
joinColumns = @JoinColumn(name = "employee_id"),
inverseJoinColumns = @JoinColumn(name = "department_code")
private Department department;
}
Department Table
@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Department{
@Id
@Column(name = "code", unique = true)
private String code;
@Column(name = "name", nullable = false)
private String name;
}
So once the application is deployed, we will have three tables i.e. employee
, department
and employee_department
. Now if I write hibernate like
findByDepartmentCodeIn(Department Code)
, How will hibernate internally parse this query ? I want to create an index on column department_code
to make the former query faster so where do I create an index, in the department
table or in the resulting merger employee_department
table ?
