From ce4001680ebaa6fbb150f4299064a18cad5345f2 Mon Sep 17 00:00:00 2001 From: Daniel <59575049+lumijiez@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:27:54 +0300 Subject: [PATCH] add hibernate support, and example --- .idea/dataSources.xml | 21 +++++ .idea/inspectionProfiles/Project_Default.xml | 8 ++ pom.xml | 27 ++++++- .../java/io/github/lumijiez/example/Main.java | 9 +++ .../lumijiez/example/daos/ProductDao.java | 78 +++++++++++++++++++ .../lumijiez/example/models/Product.java | 70 +++++++++++++++++ src/main/resources/hibernate.cfg.xml | 24 ++++++ 7 files changed, 234 insertions(+), 3 deletions(-) create mode 100644 .idea/dataSources.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 src/main/java/io/github/lumijiez/example/daos/ProductDao.java create mode 100644 src/main/java/io/github/lumijiez/example/models/Product.java create mode 100644 src/main/resources/hibernate.cfg.xml diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..da61cdb --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,21 @@ + + + + + postgresql + true + true + Hibernate + +C:/Users/danth/IdeaProjects/Wirestream/src/main/resources/hibernate.cfg.xml + org.postgresql.Driver + jdbc:postgresql://localhost:5432/accounting + + + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..eb5b7b9 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2b4629e..ea1ee91 100644 --- a/pom.xml +++ b/pom.xml @@ -47,11 +47,34 @@ gson 2.11.0 + + + org.postgresql + postgresql + 42.6.2 + + + + org.hibernate.orm + hibernate-core + 6.6.1.Final + + + + org.hibernate.orm + hibernate-c3p0 + 6.6.1.Final + + + + javax.persistence + javax.persistence-api + 2.2 + - org.apache.maven.plugins maven-jar-plugin @@ -65,7 +88,6 @@ - org.apache.maven.plugins maven-source-plugin @@ -108,7 +130,6 @@ - org.apache.maven.plugins maven-javadoc-plugin diff --git a/src/main/java/io/github/lumijiez/example/Main.java b/src/main/java/io/github/lumijiez/example/Main.java index d6d3c1b..80c75e1 100644 --- a/src/main/java/io/github/lumijiez/example/Main.java +++ b/src/main/java/io/github/lumijiez/example/Main.java @@ -3,10 +3,14 @@ package io.github.lumijiez.example; import io.github.lumijiez.core.config.ServerConfig; import io.github.lumijiez.core.http.HttpServer; import io.github.lumijiez.core.http.HttpStatus; +import io.github.lumijiez.example.daos.ProductDao; +import io.github.lumijiez.example.models.Product; import io.github.lumijiez.logging.Logger; public class Main { public static void main(String[] args) { + ProductDao productDao = new ProductDao(); + ServerConfig config = new ServerConfig.Builder() .port(8080) .keepAliveTimeout(30000) @@ -25,6 +29,11 @@ public class Main { res.sendResponse(HttpStatus.OK, "All good, lil bro"); }); + server.GET("/products", (req, res) -> { + Product product = productDao.getProductById(5); + res.sendResponse(HttpStatus.OK, product.toString()); + }); + server.start(); } } \ No newline at end of file diff --git a/src/main/java/io/github/lumijiez/example/daos/ProductDao.java b/src/main/java/io/github/lumijiez/example/daos/ProductDao.java new file mode 100644 index 0000000..f219700 --- /dev/null +++ b/src/main/java/io/github/lumijiez/example/daos/ProductDao.java @@ -0,0 +1,78 @@ +package io.github.lumijiez.example.daos; + +import io.github.lumijiez.example.models.Product; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.cfg.Configuration; + +import java.util.List; + +public class ProductDao { + + public void saveProduct(Product product) { + Transaction transaction = null; + try (Session session = new Configuration().configure().buildSessionFactory().openSession()) { + transaction = session.beginTransaction(); + session.save(product); + transaction.commit(); + System.out.println("Product saved successfully!"); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + e.printStackTrace(); + } + } + + public Product getProductById(int id) { + try (Session session = new Configuration().configure().buildSessionFactory().openSession()) { + return session.get(Product.class, id); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + @SuppressWarnings("unchecked") + public List getAllProducts() { + try (Session session = new Configuration().configure().buildSessionFactory().openSession()) { + return session.createQuery("from Product").list(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public void updateProduct(Product product) { + Transaction transaction = null; + try (Session session = new Configuration().configure().buildSessionFactory().openSession()) { + transaction = session.beginTransaction(); + session.update(product); + transaction.commit(); + System.out.println("Product updated successfully!"); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + e.printStackTrace(); + } + } + + public void deleteProduct(int id) { + Transaction transaction = null; + try (Session session = new Configuration().configure().buildSessionFactory().openSession()) { + transaction = session.beginTransaction(); + Product product = session.get(Product.class, id); + if (product != null) { + session.delete(product); + System.out.println("Product deleted successfully!"); + } + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + e.printStackTrace(); + } + } +} diff --git a/src/main/java/io/github/lumijiez/example/models/Product.java b/src/main/java/io/github/lumijiez/example/models/Product.java new file mode 100644 index 0000000..30e1252 --- /dev/null +++ b/src/main/java/io/github/lumijiez/example/models/Product.java @@ -0,0 +1,70 @@ +package io.github.lumijiez.example.models; + +import jakarta.persistence.*; + +@Entity +@Table(name = "products") +public class Product { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + + @Column(name = "product_code") + private String productCode; + + @Column(name = "name") + private String name; + + @Column(name = "category") + private String category; + + @Column(name = "price") + private double price; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getProductCode() { + return productCode; + } + + public void setProductCode(String productCode) { + this.productCode = productCode; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + @Override + public String toString() { + return "Product [id=" + id + ", productCode=" + productCode + ", name=" + name + + ", category=" + category + ", price=" + price + "]"; + } +} \ No newline at end of file diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml new file mode 100644 index 0000000..db6bfa3 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,24 @@ + + + + org.postgresql.Driver + jdbc:postgresql://localhost:5432/accounting + postgres + postgres + + 5 + 20 + 300 + 50 + 3000 + 2 + + + + update + + + + \ No newline at end of file