Commit dc5152ed by Patryk Czarnik

Odczyt list

parent d8d7f6d2
package programy;
import java.util.List;
import jakarta.persistence.*;
import model.*;
public class P02_OdczytajListe_JPQL {
public static void main(String[] args) {
EntityManagerFactory emf = null;
EntityManager em = null ;
try {
emf = Persistence.createEntityManagerFactory("hr_postgresql");
em = emf.createEntityManager();
// JPQL = Java Persistence Query Language
TypedQuery<Location> query = em.createQuery("SELECT loc FROM Location loc ORDER BY loc.country.countryId, loc.city", Location.class);
List<Location> locations = query.getResultList();
for (Location loc : locations) {
System.out.printf(" - %s, %s %s, %s\n", loc.getStreetAddress(), loc.getPostalCode(), loc.getCity(),
loc.getCountry().getCountryName());
}
} finally {
em.close();
emf.close();
}
}
}
package programy;
import java.util.List;
import jakarta.persistence.*;
import model.*;
public class P03_OdczytajListe_NamedQuery {
public static void main(String[] args) {
EntityManagerFactory emf = null;
EntityManager em = null ;
try {
emf = Persistence.createEntityManagerFactory("hr_postgresql");
em = emf.createEntityManager();
TypedQuery<Location> query = em.createNamedQuery("Location.findAll", Location.class);
List<Location> locations = query.getResultList();
for (Location loc : locations) {
System.out.printf(" - %s, %s %s, %s\n", loc.getStreetAddress(), loc.getPostalCode(), loc.getCity(),
loc.getCountry().getCountryName());
}
} finally {
em.close();
emf.close();
}
}
}
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