EclipseLink – 19 – Query – 02 – Select

Merhaba Arkadaslar
Onceki bolumde JPQL e basit olarak giris yaptik ve bazi temel konulari inceledik.

Java Persistence Query Language (JPQL) , JPA (Java Persistence API) icin standard sorgu dilidir. (query language)
JPQL kullanilarak yazilan sorgular tum buyuk veritabani yonetisim sistemleriyle uyumlu sekilde calisacaktir.

JPQL her ne kadar SQL’e benzese de JPQL SQL degildir! JPQL Entity’leri sorgulayan bir dildir. SQL ise tablolar uzerinde calisir.
JPQL e ihtiyac duyulmasinin nedeni ; portability/tasinabilirlik konusudur. JPQL , tum buyuk veritabanlarina uyumlu sekilde calisacak SQL sorgularina donusturulur.

Sorgular terminology olarak 4 kategoriye ayrilir ;

  • select
  • aggregate
  • update
  • delete

Select Queries

Select sorgulari icin genel format ;

SELECT 
FROM 
[WHERE ]
[ORDER BY ]

En temel Select sorgusu icin gerekli olan Select clause ve From clause’tur.

Select e from Employee e;

Yukaridaki JPQL sorgusu ile SQL sorgusu birbirine buyuk oranda benzemektedir. Onceki bolumde belirttigim gibi unutmamamiz gereken nokta JPQL sorgulari Entity’lere yapilmaktadir. Dolayisiyla burada Employee tablo degil entity’dir. SQL dilinde employee ile Employee fark olusturmazken burada employee seklinde yazarsak hata aliriz , from clause’ta Entity ismini vermemiz gereklidir.

JPQLQuery.java

public class JPQLQuery {
///
public final static String SELECT_ALL_EMPLOYEES = "Select e from Employee29 e";
///
}

EmployeeTest.java

...
System.out.println("SELECT_ALL_EMPLOYEES");

List<Employee29> allEmployeeList = jpqlService.executeResultListQuery(SELECT_ALL_EMPLOYEES, Employee29.class);

for (Employee29 emp : allEmployeeList) {
	System.out.println(emp);
}
...

Eclipse Console ciktimiz;

SELECT_ALL_EMPLOYEES
Employee29 [id=1, name=Levent, surname=Erguder, salary=1000]
Employee29 [id=2, name=James, surname=Gosling, salary=10000]
Employee29 [id=3, name=Joshua, surname=Bloch, salary=6000]
Employee29 [id=4, name=Kathy, surname=Sierra, salary=4000]
Employee29 [id=5, name=Linda, surname=DeMichiel, salary=300]
Employee29 [id=6, name=Rod , surname=Johnson , salary=5000]
Employee29 [id=7, name=Gavin, surname=King, salary=5000]

Bir baska basit Select sorgumuzu inceleyecek olursak , ornegin tum calisanlarin ismini donduren bir sorgu yazalim ;

JPQLQuery.java

public class JPQLQuery {
///
public final static String SELECT_NAME_OF_EMPLOYEES = "Select e.name from Employee29 e";
///
}

EmployeeTest.java

System.out.println("SELECT_NAME_OF_EMPLOYEES");
List<String> employeeNameList = jpqlService.executeResultListQuery(SELECT_NAME_OF_EMPLOYEES, String.class);

for (String name : employeeNameList) {
	System.out.println(name);
}


Ornegimizi calistirdigimizda ;

SELECT_NAME_OF_EMPLOYEES
Levent
James
Joshua
Kathy
Linda
Rod 
Gavin

Bir baska sorgu ornegi olarak ornegin calisanlara ait isim ve maas bilgisini getirmek istersek ;

public final static String SELECT_NAME_AND_SALARY = "Select e.name , e.salary from Employee29 e";

Burada sorgu sonucumuz List<Object[]> olacaktir. Her Object[] icin 2 tane eleman yer alacaktir.

System.out.println("SELECT_NAME_AND_SALARY");
List<Object[]> nameAndSalaryList = jpqlService.executeResultListQuery(SELECT_NAME_AND_SALARY, Object[].class);

for (Object[] nameAndSalaryElement : nameAndSalaryList) {
	System.out.println(nameAndSalaryElement[0] + " " + nameAndSalaryElement[1]);
}

Ornegimizi calistirdigimizda ;

SELECT_NAME_AND_SALARY
Levent 1000
James 10000
Joshua 6000
Kathy 4000
Linda 300
Rod  5000
Gavin 5000

Constructor Expressions

Multiple expressionlar icin daha esni bir yapi olarak constructor expression kullanilabilir.
Bunun icin bir model sinifi olusturabiliriz.

EmployeeDetails.java

package _29.jpql.model;

public class EmployeeDetails {

	private String name;
	private int salary;
	private String departmentName;

	public EmployeeDetails(String name, int salary, String departmentName) {
		super();
		this.name = name;
		this.salary = salary;
		this.departmentName = departmentName;
	}
///
}

Result type example.EmployeeDetails Java sinifidir. Yeni bir EmployeeDetails objesi olusturulur , bunun icin ilgili constructor tanimlanmis olmalidir.

public final static String SELECT_EMPLOYEE_DETAILS = "Select New _29.jpql.model.EmployeeDetails (e.name , e.salary , e.department.name) from Employee29 e";
System.out.println("SELECT_EMPLOYEE_DETAILS");
List<EmployeeDetails> employeeDetailsList = jpqlService.executeResultListQuery(SELECT_EMPLOYEE_DETAILS, EmployeeDetails.class);
for (EmployeeDetails empDetails : employeeDetailsList) {
	System.out.println(empDetails);
}

Kaynak kodlar : Injavawetrust.jpa

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published.