Commit 029f5311 by Patryk Czarnik

Odczyt jednego produktu

parent 613fe5ef
......@@ -25,12 +25,12 @@ public class OrderProduct implements Serializable {
//bi-directional many-to-one association to Order
@ManyToOne
@JoinColumn(name="order_id")
@JoinColumn(name="order_id", insertable=false, updatable=false)
private Order order;
//uni-directional many-to-one association to Product
@ManyToOne
@JoinColumn(name="product_id")
@JoinColumn(name="product_id", insertable=false, updatable=false)
private Product product;
public OrderProduct() {
......
package sklep.przyklady;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import sklep.model.Product;
public class OdczytajJedenProdukt1 {
public static void main(String[] args) {
System.out.println("Początek programu");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sklep");
EntityManager em = emf.createEntityManager();
System.out.println("Mam entity managera: " + em);
Product product = em.find(Product.class, 1);
System.out.println("Odczytany produkt: " + product);
System.out.println(product.getProductName() + " za cenę " + product.getPrice() + ", opis: " + product.getDescription());
em.close();
emf.close();
}
}
package sklep.przyklady;
import java.util.Scanner;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import sklep.model.Product;
public class OdczytajJedenProdukt2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sklep");
EntityManager em = emf.createEntityManager();
System.out.println("Aby zakończyć, podaj liczbę 0");
while(true) {
System.out.print("\nPodaj id produktu: ");
int id = sc.nextInt();
if(id == 0) break;
Product product = em.find(Product.class, id);
if(product != null) {
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);
}
}
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