Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How to restrict other queries getting executed.
How to restrict other queries getting executed. [message #830964] Wed, 28 March 2012 09:27 Go to next message
chaitanya subbu is currently offline chaitanya subbuFriend
Messages: 1
Registered: March 2012
Junior Member
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;
}

}

Re: How to restrict other queries getting executed. [message #831199 is a reply to message #830964] Wed, 28 March 2012 15:46 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Hello,

Employee has 1:1 relationships to Address and Departments which need to be read in for the returned Employee entities to be complete. If the relationship were marked as lazy, this would allow the JPA provider to use a placeholder for these relationships instead assuming it were able to. See
http://wiki.eclipse.org/EclipseLink/FAQ/JPA/BestPractices#M4._Lazy_Loading

and
http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#What_You_May_Need_to_Know_About_EclipseLink_JPA_Lazy_Loading
for more information.

Best Regards,
Chris
Previous Topic:Configure Dali Entitty Generator to create all @column annotations
Next Topic:Reference to Superclass
Goto Forum:
  


Current Time: Tue Apr 23 09:59:09 GMT 2024

Powered by FUDForum. Page generated in 0.02964 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top