add hibernate support, and example
This commit is contained in:
21
.idea/dataSources.xml
generated
Normal file
21
.idea/dataSources.xml
generated
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="hibernate.cfg.xml/Hibernate" uuid="0be9ae1d-14ae-4499-97db-8c29829f36b6">
|
||||
<driver-ref>postgresql</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<imported>true</imported>
|
||||
<remarks>Hibernate
|
||||
|
||||
C:/Users/danth/IdeaProjects/Wirestream/src/main/resources/hibernate.cfg.xml</remarks>
|
||||
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:postgresql://localhost:5432/accounting</jdbc-url>
|
||||
<jdbc-additional-properties>
|
||||
<property name="com.intellij.clouds.kubernetes.db.host.port" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.container.port" />
|
||||
</jdbc-additional-properties>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
8
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.lang.foreign.Arena,ofAuto,java.lang.foreign.Arena,global,org.hibernate.cfg.Configuration,buildSessionFactory" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
||||
27
pom.xml
27
pom.xml
@@ -47,11 +47,34 @@
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.11.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.6.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>6.6.1.Final</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-c3p0</artifactId>
|
||||
<version>6.6.1.Final</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.persistence</groupId>
|
||||
<artifactId>javax.persistence-api</artifactId>
|
||||
<version>2.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- Maven JAR Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
@@ -65,7 +88,6 @@
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- Maven Source Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
@@ -108,7 +130,6 @@
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- Maven Javadoc Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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<Product> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
70
src/main/java/io/github/lumijiez/example/models/Product.java
Normal file
70
src/main/java/io/github/lumijiez/example/models/Product.java
Normal file
@@ -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 + "]";
|
||||
}
|
||||
}
|
||||
24
src/main/resources/hibernate.cfg.xml
Normal file
24
src/main/resources/hibernate.cfg.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
|
||||
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/accounting</property>
|
||||
<property name="hibernate.connection.username">postgres</property>
|
||||
<property name="hibernate.connection.password">postgres</property>
|
||||
|
||||
<property name="hibernate.c3p0.min_size">5</property>
|
||||
<property name="hibernate.c3p0.max_size">20</property>
|
||||
<property name="hibernate.c3p0.timeout">300</property>
|
||||
<property name="hibernate.c3p0.max_statements">50</property>
|
||||
<property name="hibernate.c3p0.idle_test_period">3000</property>
|
||||
<property name="hibernate.c3p0.acquire_increment">2</property>
|
||||
|
||||
<!-- <property name="hibernate.show_sql">true</property>-->
|
||||
|
||||
<property name="hibernate.hbm2ddl.auto">update</property>
|
||||
|
||||
<mapping class="io.github.lumijiez.example.models.Product"/>
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
||||
Reference in New Issue
Block a user