when i execute below query why DEPARTMENTS query and Address query are getting executed.I think first query is enough to fetch the results.How to restrcit this?
String query="SELECT e FROM Employee e WHERE e.address.address1 IN ('NY', 'CA')";
SELECT t1.EMPLOYEE_ID, t1.EMAIL, t1.FIRST_NAME, t1.HIRE_DATE, t1.JOB_ID, t1.LAST_NAME, t1.SALARY, t1.DEPARTMENT_ID FROM ADDRESS t0, EMPLOYEES t1 WHERE ((t0.ADDRESS1 IN (?, ?)) AND (t0.EMPLOYEE_ID = t1.EMPLOYEE_ID))
bind => [NY, CA]
SELECT DEPARTMENT_ID, DEPARTMENT_NAME, LOCATION_ID FROM DEPARTMENTS WHERE (DEPARTMENT_ID = ?)
bind => [60]
SELECT t1.id, t1.ADDRESS1, t1.ADDRESS2, t1.EMPLOYEE_ID, t0.EMPLOYEE_ID, t0.EMAIL, t0.FIRST_NAME, t0.HIRE_DATE, t0.JOB_ID, t0.LAST_NAME, t0.SALARY, t0.DEPARTMENT_ID FROM ADDRESS t1 LEFT OUTER JOIN EMPLOYEES t0 ON (t0.EMPLOYEE_ID = t1.EMPLOYEE_ID) WHERE (t1.EMPLOYEE_ID = ?)
bind => [104]
@Entity
@Table(name="EMPLOYEES")
public class Employee {
@Id
@Column(name="EMPLOYEE_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_sequence")
@SequenceGenerator(name = "employee_sequence", sequenceName = "EMPLOYEES_SEQ", allocationSize = 1)
private int employeeId;
@Column(name="FIRST_NAME")
private String firstName;
@Column(name="LAST_NAME")
private String lastName;
private String email;
private int salary;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="DEPARTMENT_ID")
private Dept dept;
@OneToOne(mappedBy="employee",fetch=FetchType.LAZY)
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Collection<Project> getProjects() {
return projects;
}
public void setProjects(Collection<Project> projects) {
this.projects = projects;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
}
Department.java
***************
@Entity
@Table(name="DEPARTMENTS")
public class Dept {
@Id
@Column(name="DEPARTMENT_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "departments_sequence")
@SequenceGenerator(name = "departments_sequence", sequenceName = "DEPARTMENTS_SEQ", allocationSize = 1)
private int deptno;
@Column(name="DEPARTMENT_NAME")
private String dname;
@Column(name="LOCATION_ID")
private int loc;
@OneToMany(mappedBy ="dept",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
private List<Employee> employees = new ArrayList<Employee>();
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public int getLoc() {
return loc;
}
public void setLoc(int loc) {
this.loc = loc;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
Address.java
************
@Entity
@Table(name="ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "address_sequence")
@SequenceGenerator(name = "address_sequence", sequenceName = "ADDRESS_SEQ", allocationSize = 1)
@Column(name="id")
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="ADDRESS1")
private String address1;
@Column(name="ADDRESS2")
private String address2;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="EMPLOYEE_ID")
private Employee employee;
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}