Commit cd2af3f4 by Patryk Czarnik

Poprawka konfiguracji i pierwszy program Hibernate/JPA

parent 0d81e9d4
...@@ -27,7 +27,7 @@ public class Customer { ...@@ -27,7 +27,7 @@ public class Customer {
@Column(name = "city", length = 100) @Column(name = "city", length = 100)
private String city; private String city;
@OneToMany(mappedBy = "customerEmail") @OneToMany(mappedBy = "customer")
private Set<Order> orders = new LinkedHashSet<>(); private Set<Order> orders = new LinkedHashSet<>();
public String getCustomerEmail() { public String getCustomerEmail() {
......
...@@ -19,7 +19,7 @@ public class Order { ...@@ -19,7 +19,7 @@ public class Order {
@ManyToOne(fetch = FetchType.LAZY, optional = false) @ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "customer_email", nullable = false) @JoinColumn(name = "customer_email", nullable = false)
private Customer customerEmail; private Customer customer;
@ColumnDefault("CURRENT_TIMESTAMP") @ColumnDefault("CURRENT_TIMESTAMP")
@Column(name = "order_date", nullable = false) @Column(name = "order_date", nullable = false)
...@@ -27,6 +27,7 @@ public class Order { ...@@ -27,6 +27,7 @@ public class Order {
@Column(name = "delivery_date") @Column(name = "delivery_date")
private LocalDate deliveryDate; private LocalDate deliveryDate;
@OneToMany(mappedBy = "order") @OneToMany(mappedBy = "order")
private Set<OrderProduct> orderProducts = new LinkedHashSet<>(); private Set<OrderProduct> orderProducts = new LinkedHashSet<>();
...@@ -38,12 +39,12 @@ public class Order { ...@@ -38,12 +39,12 @@ public class Order {
this.id = id; this.id = id;
} }
public Customer getCustomerEmail() { public Customer getCustomer() {
return customerEmail; return customer;
} }
public void setCustomerEmail(Customer customerEmail) { public void setCustomer(Customer customerEmail) {
this.customerEmail = customerEmail; this.customer = customerEmail;
} }
public Instant getOrderDate() { public Instant getOrderDate() {
......
package sklep.programy;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import sklep.model.Product;
import javax.swing.*;
public class P01_JedenProdukt {
public static void main(String[] args) {
System.out.println("Początek programu");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sklep_pu");
System.out.println("Mam factory: " + emf);
EntityManager em = emf.createEntityManager();
System.out.println("Mam em: " + em);
Integer id = Integer.valueOf(JOptionPane.showInputDialog("Podaj id produktu"));
Product product = em.find(Product.class, id);
// w razie braku takiego rekordu, wynikiem jest null
if(product == null) {
System.out.println("Nie ma produktu o numerze " + id);
} else {
System.out.println("Odczytany produkt: " + product);
System.out.println(product.getProductName() + " za cenę " + product.getPrice());
if(product.getDescription() != null) {
System.out.println(product.getDescription());
}
}
em.close();
emf.close();
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0">
<persistence-unit name="sklep_pu" transaction-type="RESOURCE_LOCAL">
<class>sklep.model.Product</class>
<class>sklep.model.Customer</class>
<class>sklep.model.Order</class>
<class>sklep.model.OrderProduct</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/sklep"/>
<property name="javax.persistence.jdbc.user" value="alx"/>
<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>
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