Commit 1eb1a9ee by Patryk Czarnik

Ostatnie poprawki w projekcie Hibernate

parent 594034a7
...@@ -16,7 +16,7 @@ public class MiniEmp { ...@@ -16,7 +16,7 @@ public class MiniEmp {
this.lastName = lastName; this.lastName = lastName;
this.jobTitle = jobTitle; this.jobTitle = jobTitle;
this.salary = salary; this.salary = salary;
//System.out.println("salary YES"); System.out.println("MiniEmp konstruktor");
} }
public MiniEmp(String firstName, String lastName, String jobTitle) { public MiniEmp(String firstName, String lastName, String jobTitle) {
......
...@@ -17,19 +17,6 @@ public class PracownikWDepartamencie { ...@@ -17,19 +17,6 @@ public class PracownikWDepartamencie {
public PracownikWDepartamencie() { public PracownikWDepartamencie() {
} }
public PracownikWDepartamencie(int id, String departmentName, String firstName, String lastName, String jobTitle,
BigDecimal salary, BigDecimal depAvg, int depPos, int globalPos) {
this.id = id;
this.departmentName = departmentName;
this.firstName = firstName;
this.lastName = lastName;
this.jobTitle = jobTitle;
this.salary = salary;
this.depAvg = depAvg;
this.depPos = depPos;
this.globalPos = globalPos;
}
public int getId() { public int getId() {
return id; return id;
} }
......
...@@ -12,11 +12,8 @@ import jakarta.persistence.Query; ...@@ -12,11 +12,8 @@ import jakarta.persistence.Query;
public class Odczyt13_Native { public class Odczyt13_Native {
public static void main(String[] args) { public static void main(String[] args) {
EntityManagerFactory emf = null; try(EntityManagerFactory emf = Persistence.createEntityManagerFactory("hr_postgresql");
EntityManager em = null; EntityManager em = emf.createEntityManager()) {
try {
emf = Persistence.createEntityManagerFactory("hr_postgresql");
em = emf.createEntityManager();
Query query = em.createNativeQuery( Query query = em.createNativeQuery(
"SELECT first_name || ' ' || last_name, job_title, salary FROM employees JOIN jobs USING(job_id) ORDER BY employee_id"); "SELECT first_name || ' ' || last_name, job_title, salary FROM employees JOIN jobs USING(job_id) ORDER BY employee_id");
...@@ -24,8 +21,7 @@ public class Odczyt13_Native { ...@@ -24,8 +21,7 @@ public class Odczyt13_Native {
List<?> rows = query.getResultList(); List<?> rows = query.getResultList();
for (Object row : rows) { for (Object row : rows) {
// na wszelki wypadek sprawdzam, ale widzę, że Object[] działa // na wszelki wypadek sprawdzam, ale widzę, że Object[] działa
if(row instanceof Object[]) { if(row instanceof Object[] cols) {
Object[] cols = (Object[]) row;
System.out.println(cols[0] + " pracuje jako " + cols[1] + " i zarabia " + cols[2]); System.out.println(cols[0] + " pracuje jako " + cols[1] + " i zarabia " + cols[2]);
} }
} }
...@@ -35,9 +31,6 @@ public class Odczyt13_Native { ...@@ -35,9 +31,6 @@ public class Odczyt13_Native {
System.out.println("Sprawdzę typy kolumn:"); System.out.println("Sprawdzę typy kolumn:");
Object[] row = (Object[])rows.get(0); Object[] row = (Object[])rows.get(0);
System.out.println(row[0].getClass().getSimpleName() + " " + row[1].getClass().getSimpleName() + " " + row[2].getClass().getSimpleName()); System.out.println(row[0].getClass().getSimpleName() + " " + row[1].getClass().getSimpleName() + " " + row[2].getClass().getSimpleName());
} finally {
if(em != null) em.close();
if(emf != null) emf.close();
} }
} }
......
package hr.programy;
import java.util.List;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import jakarta.persistence.Query;
public class Odczyt13_Native_SprawdzanieTypow {
public static void main(String[] args) {
try(EntityManagerFactory emf = Persistence.createEntityManagerFactory("hr_postgresql");
EntityManager em = emf.createEntityManager()) {
int i;
List<?> wyniki1 = em.createNativeQuery("SELECT first_name || ' ' || last_name FROM employees").getResultList();
i = 0;
for(Object row : wyniki1) {
System.out.println(row + " klasy " + row.getClass().getSimpleName());
i++;
if(i >= 10) break;
}
System.out.println();
List<?> wyniki2 = em.createNativeQuery("SELECT salary FROM employees").getResultList();
i = 0;
for(Object row : wyniki2) {
System.out.println(row + " klasy " + row.getClass().getSimpleName());
i++;
if(i >= 10) break;
}
System.out.println();
List<?> wyniki3 = em.createNativeQuery("SELECT first_name, last_name, salary FROM employees").getResultList();
i = 0;
for(Object row : wyniki3) {
System.out.println(row + " klasy " + row.getClass().getSimpleName());
i++;
if(i >= 10) break;
}
System.out.println();
}
}
}
...@@ -14,6 +14,8 @@ import jakarta.persistence.Query; ...@@ -14,6 +14,8 @@ import jakarta.persistence.Query;
// - funkcjonalność znana głównie w Oracle, dostępna także w PostgreSQL. // - funkcjonalność znana głównie w Oracle, dostępna także w PostgreSQL.
// W tej wersji dodatkowo wyniki pakuję do własnej klasy PracownikWDepartamencie // W tej wersji dodatkowo wyniki pakuję do własnej klasy PracownikWDepartamencie
// Pola tej klasy muszą mieć nazwy odpowiadające nazwom kolumn w wyniku zapytania.
// W razie potrzeby możemy użyć aliasów kolumn w SQL.
public class Odczyt15_Native_DTO { public class Odczyt15_Native_DTO {
......
...@@ -25,11 +25,18 @@ public class P07_AktualizujRekord2 { ...@@ -25,11 +25,18 @@ public class P07_AktualizujRekord2 {
System.out.println(kochhar.getEmployeeId() + " " + kochhar.getFirstName() + " " + kochhar.getLastName()); System.out.println(kochhar.getEmployeeId() + " " + kochhar.getFirstName() + " " + kochhar.getLastName());
Department dep = em.find(Department.class, 90); Department dep = em.find(Department.class, 90);
System.out.println("departament: " + dep.getDepartmentName()); System.out.println("departament find: " + dep.getDepartmentName());
Department depN = (Department) em.createNativeQuery(
"SELECT * FROM departments WHERE department_id = 90", Department.class)
.getSingleResult();
System.out.println("departament native: " + depN.getDepartmentName());
System.out.println(dep == king.getDepartment()); System.out.println(dep == king.getDepartment());
System.out.println(dep == kochhar.getDepartment()); System.out.println(dep == kochhar.getDepartment());
System.out.println(dep == depN);
System.out.println(); System.out.println();
// Każdy z tych trzech sposobów uzyskania departamentu zwrócił referencję do tego samego obiektu.
System.out.println("Departamenty przed zmianą: " System.out.println("Departamenty przed zmianą: "
+ dep.getDepartmentName() + " " + dep.getDepartmentName() + " "
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment