Commit 3dea2f66 by Patryk Czarnik

Przykłady odczytu pojedynczych rekordów

parent de373991
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="PC27-Hibernate">
<persistence-unit name="sklep" transaction-type="RESOURCE_LOCAL">
<class>sklep.model.Customer</class>
<class>sklep.model.OrderProduct</class>
<class>sklep.model.OrderProductPK</class>
<class>sklep.model.Order</class>
<class>sklep.model.Product</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/sklep"/>
<property name="javax.persistence.jdbc.user" value="kurs"/>
<property name="javax.persistence.jdbc.password" value="abc123"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
package przyklady;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import sklep.model.Product;
public class Odczyt1_JedenProdukt {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sklep");
EntityManager em = emf.createEntityManager();
System.out.println("Udało się połączyć, em = " + em);
Product product = em.find(Product.class, 1);
System.out.println("Odczytany produkt: " + product);
System.out.println(product.getProductName() + " za cenę " + product.getPrice());
em.close();
emf.close();
}
}
package przyklady;
import java.util.Scanner;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import sklep.model.Product;
public class Odczyt2_Petla {
public static void main(String[] args) {
Scanner scanner = null;
EntityManagerFactory emf = null;
EntityManager em = null;
try {
scanner = new Scanner(System.in);
emf = Persistence.createEntityManagerFactory("sklep");
em = emf.createEntityManager();
while(true) {
System.out.print("\nPodaj id produktu: ");
int productId = scanner.nextInt();
if(productId == 0) break;
Product product = em.find(Product.class, productId);
if(product == null) {
System.out.println("Nie ma takiego produktu");
continue;
}
System.out.println("Odczytany produkt: " + product);
System.out.println(product.getProductName() + " za cenę " + product.getPrice());
if(product.getDescription() != null) {
System.out.println(product.getDescription());
}
}
} finally {
if(em != null) em.close();
if(emf != null) emf.close();
if(scanner != null) scanner.close();
}
}
}
......@@ -28,12 +28,12 @@ public class OrderProduct extends WspolnaNadklasa 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() {
......
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