Commit ea24d192 by Patryk Czarnik

Przykłady z referencjami Hibernate

parent 029f5311
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
<property name="javax.persistence.jdbc.user" value="kurs"/> <property name="javax.persistence.jdbc.user" value="kurs"/>
<property name="javax.persistence.jdbc.password" value="abc123"/> <property name="javax.persistence.jdbc.password" value="abc123"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/> <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="hibernate.show_sql" value="true"/>
</properties> </properties>
</persistence-unit> </persistence-unit>
</persistence> </persistence>
...@@ -11,27 +11,34 @@ import sklep.model.Product; ...@@ -11,27 +11,34 @@ import sklep.model.Product;
public class OdczytajJedenProdukt2 { public class OdczytajJedenProdukt2 {
public static void main(String[] args) { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); EntityManagerFactory emf = null;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sklep"); EntityManager em = null;
EntityManager em = emf.createEntityManager(); try {
Scanner sc = new Scanner(System.in);
System.out.println("Aby zakończyć, podaj liczbę 0"); emf = Persistence.createEntityManagerFactory("sklep");
while(true) { em = emf.createEntityManager();
System.out.print("\nPodaj id produktu: ");
int id = sc.nextInt(); System.out.println("Aby zakończyć, podaj liczbę 0");
if(id == 0) break; while(true) {
System.out.print("\nPodaj id produktu: ");
Product product = em.find(Product.class, id); int id = sc.nextInt();
if(product != null) { if(id == 0) break;
System.out.println("Odczytany produkt: " + product);
System.out.println(product.getProductName() + " za cenę " + product.getPrice() + ", opis: " + product.getDescription()); Product product = em.find(Product.class, id);
} else { if(product != null) {
System.out.println("Nie ma produktu o id " + id); System.out.println("Odczytany produkt: " + product);
System.out.println(product.getProductName() + " za cenę " + product.getPrice() + ", opis: " + product.getDescription());
} else {
System.out.println("Nie ma produktu o id " + id);
}
} }
} catch(Exception e) {
e.printStackTrace();
} finally {
if(em != null)
em.close();
if(emf != null)
emf.close();
} }
em.close();
emf.close();
} }
} }
package sklep.przyklady;
import java.util.List;
import java.util.Scanner;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import sklep.model.Customer;
import sklep.model.Order;
import sklep.model.OrderProduct;
public class ReferencjeMiedzyObiektami {
// Ten przykład pokazuje, że z jednego obiektu wczytanego z bazy można zwykłymi metodami get???
// przejść do obiektów zapisanych w innych tabelach.
// Nie wymaga to pisania dodatkowych zapytań.
// Zwykłe obiekty dostępne poprzez gettery, np. op.getProduct(), są wczytywane od razu i są obecne w pamięci,
// natomiast kolekcje, np. customer.getOrders(), są uzupełniane przy pierwszym odczycie (ustawienie LAZY, które jest domyślne).
public static void main(String[] args) {
EntityManagerFactory emf = null;
EntityManager em = null;
try {
Scanner scanner = new Scanner(System.in);
emf = Persistence.createEntityManagerFactory("sklep");
em = emf.createEntityManager();
while(true) {
System.out.print("Podaj email klienta: ");
String email = scanner.nextLine();
if(email.isEmpty()) {
break;
}
Customer customer = em.find(Customer.class, email);
if(customer == null) {
System.out.println("Nie ma takiego klienta");
continue;
}
System.out.println("Znaleziono obiekt: " + customer);
System.out.println(" * imię/nazwa: " + customer.getCustomerName());
System.out.println(" * adres: " + customer.getAddress() + ", " + customer.getCity());
System.out.println(" * nr tel: " + customer.getPhoneNumber());
List<Order> orders = customer.getOrders();
System.out.println("Klient posiada " + orders.size() + " zamówień");
for(Order order : orders) {
System.out.println("Zamówienie nr " + order.getOrderId() + " z dnia " + order.getOrderDate());
for(OrderProduct op : order.getOrderProducts()) {
System.out.println(" + " + op.getProduct().getProductName()
+ ", " + op.getQuantity() + " sztuk po " + op.getActualPrice() + " zł");
}
}
System.out.println();
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(em != null)
em.close();
if(emf != null)
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