Commit e60d927f by Patryk Czarnik

Odczyt wg ID interaktywny

parent e9a3f138
package programy;
import java.util.Scanner;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import model.Department;
import model.Employee;
public class P01_OdczytajWgId {
......@@ -12,17 +15,29 @@ public class P01_OdczytajWgId {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hr_postgresql");
EntityManager em = emf.createEntityManager();
System.out.println("Mam EntityManagera: " + em);
Integer id = 103;
Employee emp = em.find(Employee.class, id);
System.out.println("Odczytany obiekt: " + emp);
if(emp != null) {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.print("Podaj ID pracownika (0 kończy program): ");
Integer id = sc.nextInt();
if(id == 0) break;
Employee emp = em.find(Employee.class, id);
System.out.println("Odczytany obiekt: " + emp);
if(emp == null) {
System.out.printf("Nie ma pracownika o id %d\n", id);
continue;
}
System.out.printf("%s %s (%s) zarabia %s\n",
emp.getFirstName(), emp.getLastName(), emp.getJob().getJobTitle(), emp.getSalary());
Department dep = emp.getDepartment();
if(dep != null) {
System.out.printf("Departament %s, %s, %s, %s\n",
dep.getDepartmentName(), dep.getLocation().getStreetAddress(),
dep.getLocation().getCity(), dep.getLocation().getCountry().getCountryName());
}
}
sc.close();
em.close();
emf.close();
System.out.println("Koniec");
......
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