diff --git a/Hibernate_CS308/.gitignore b/Hibernate_CS308/.gitignore
new file mode 100644
index 0000000..5ff6309
--- /dev/null
+++ b/Hibernate_CS308/.gitignore
@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store
\ No newline at end of file
diff --git a/Hibernate_CS308/.idea/compiler.xml b/Hibernate_CS308/.idea/compiler.xml
deleted file mode 100644
index c409301..0000000
--- a/Hibernate_CS308/.idea/compiler.xml
+++ /dev/null
@@ -1,14 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Hibernate_CS308/.idea/dataSources.xml b/Hibernate_CS308/.idea/dataSources.xml
new file mode 100644
index 0000000..6cb171e
--- /dev/null
+++ b/Hibernate_CS308/.idea/dataSources.xml
@@ -0,0 +1,22 @@
+
+
+
+
+ oracle.19
+ true
+ true
+ Hibernate
+
+C:/Users/MohammedRagab/Documents/SQL_Projects/308/PLSQL_CS308_2024/Hibernate_CS308/src/main/resources/hibernate.cfg.xml
+ false
+ oracle.jdbc.OracleDriver
+ jdbc:oracle:thin:@//localhost:1521/freepdb1
+
+
+
+
+
+ $ProjectFileDir$
+
+
+
\ No newline at end of file
diff --git a/Hibernate_CS308/.idea/jarRepositories.xml b/Hibernate_CS308/.idea/jarRepositories.xml
deleted file mode 100644
index 712ab9d..0000000
--- a/Hibernate_CS308/.idea/jarRepositories.xml
+++ /dev/null
@@ -1,20 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Hibernate_CS308/.idea/misc.xml b/Hibernate_CS308/.idea/misc.xml
index 3be61e2..f24c79d 100644
--- a/Hibernate_CS308/.idea/misc.xml
+++ b/Hibernate_CS308/.idea/misc.xml
@@ -8,5 +8,5 @@
-
+
\ No newline at end of file
diff --git a/Hibernate_CS308/.idea/modules.xml b/Hibernate_CS308/.idea/modules.xml
deleted file mode 100644
index a679cec..0000000
--- a/Hibernate_CS308/.idea/modules.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Hibernate_CS308/CS308Spring2022-master/CS308Spring2022.iml b/Hibernate_CS308/CS308Spring2022-master/CS308Spring2022.iml
deleted file mode 100644
index 2cc9fa5..0000000
--- a/Hibernate_CS308/CS308Spring2022-master/CS308Spring2022.iml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Hibernate_CS308/CS308Spring2022.iml b/Hibernate_CS308/CS308Spring2022.iml
deleted file mode 100644
index 2450ac9..0000000
--- a/Hibernate_CS308/CS308Spring2022.iml
+++ /dev/null
@@ -1,14 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Hibernate_CS308/pom.xml b/Hibernate_CS308/pom.xml
index 7effd55..a229dda 100644
--- a/Hibernate_CS308/pom.xml
+++ b/Hibernate_CS308/pom.xml
@@ -1,51 +1,72 @@
- 4.0.0
-
- com.sci
- CS308Spring2022
- 1.0.0
-
-
- 8
- 8
-
-
-
-
-
- org.projectlombok
- lombok
- 1.18.22
-
-
-
- com.oracle.database.jdbc
- ojdbc8
- 21.5.0.0
-
-
-
- org.hibernate
- hibernate-core
- 5.6.7.Final
-
-
-
- net.sf.ehcache
- ehcache
- 2.10.9.2
-
-
-
- org.hibernate
- hibernate-ehcache
- 5.6.8.Final
-
-
-
-
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ 4.0.0
+
+ com.sci
+ Hibernate_CS308
+ 1.0.0
+
+
+ 8
+ 8
+
+
+
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.36
+ provided
+
+
+
+
+ com.oracle.database.jdbc
+ ojdbc8
+ 23.7.0.25.01
+
+
+
+
+ org.hibernate.orm
+ hibernate-core
+ 6.6.11.Final
+
+
+
+
+ org.hibernate.orm
+ hibernate-jcache
+ 6.6.11.Final
+
+
+
+
+ org.ehcache
+ ehcache
+ 3.9.11
+
+
+
+
+ org.slf4j
+ slf4j-api
+ 2.0.17
+
+
+
+
+ ch.qos.logback
+ logback-classic
+ 1.5.18
+ compile
+
+
+
+
\ No newline at end of file
diff --git a/Hibernate_CS308/src/main/java/NormalizeAlgo.java b/Hibernate_CS308/src/main/java/NormalizeAlgo.java
index 60a9a2c..e5fa6d4 100644
--- a/Hibernate_CS308/src/main/java/NormalizeAlgo.java
+++ b/Hibernate_CS308/src/main/java/NormalizeAlgo.java
@@ -1,152 +1,152 @@
-/* A class involving Normalization Theoretic Algorithms.
- * No need to review it but if it peeks your interest...
- * Go right ahead. */
-
-import java.util.*;
-
-public class NormalizeAlgo {
- public static void main(String[] args) {
- // Example sets of functional dependencies
- Set F = new HashSet<>();
- Set E = new HashSet<>();
-
- F.add(new FunctionalDependency(new HashSet<>(Collections.singletonList("A")),
- new HashSet<>(Collections.singletonList("C"))));
- F.add(new FunctionalDependency(new HashSet<>(Arrays.asList("A", "C")),
- new HashSet<>(Collections.singletonList("D"))));
- F.add(new FunctionalDependency(new HashSet<>(Collections.singletonList("E")),
- new HashSet<>(Arrays.asList("A", "D"))));
- F.add(new FunctionalDependency(new HashSet<>(Collections.singletonList("E")),
- new HashSet<>(Collections.singletonList("H"))));
- E.add(new FunctionalDependency(new HashSet<>(Collections.singletonList("A")),
- new HashSet<>(Arrays.asList("C", "D"))));
- E.add(new FunctionalDependency(new HashSet<>(Collections.singletonList("E")),
- new HashSet<>(Arrays.asList("A", "H"))));
-
- // Check if F covers E
- boolean coversE = FunctionalDependencyCoverage.covers(F, E);
- System.out.println("Does F cover E? " + coversE);
-
- // Check if E covers F
- boolean coversF = FunctionalDependencyCoverage.covers(E, F);
- System.out.println("Does E cover F? " + coversF);
-
- // Check if F and E are equivalent
- boolean areEquivalent = coversE && coversF;
- System.out.println("Are F and E equivalent? " + areEquivalent);
- }
-}
-
-class FunctionalDependency {
- Set leftSide;
- Set rightSide;
-
- public FunctionalDependency(Set leftSide, Set rightSide) {
- this.leftSide = leftSide;
- this.rightSide = rightSide;
- }
-
- @Override
- public String toString() {
- return leftSide + " -> " + rightSide;
- }
-}
-
-class ClosureFinder {
- Set closure(Set attributes, Set fds) {
- Set closure = new HashSet<>(attributes);
- boolean changed;
- do {
- changed = false;
- for (FunctionalDependency fd : fds) {
- if (closure.containsAll(fd.leftSide) && !closure.containsAll(fd.rightSide)) {
- closure.addAll(fd.rightSide);
- changed = true;
- }
- }
- } while (changed);
- System.out.println("Attributes: " + attributes + ", Closure: " + closure);
- return closure;
- }
-}
-
-class KeyFinder {
- Set findKey(Set attributes, Set fds) {
- ClosureFinder closureFinder = new ClosureFinder();
- Set candidateKey = new HashSet<>(attributes);
- for (String attr : attributes) {
- Set temp = new HashSet<>(candidateKey);
- temp.remove(attr);
- if (closureFinder.closure(temp, fds).equals(attributes)) {
- candidateKey.remove(attr);
- }
- }
- return candidateKey;
- }
-}
-
-class MinimalCoverFinder {
- Set findMinimalCover(Set fds) {
- Set minimalCover = new HashSet<>(fds);
-
- // Step 1: Remove extraneous attributes from right side of each FD
- for (FunctionalDependency fd : fds) {
- Set rightSide = new HashSet<>(fd.rightSide);
- for (String attr : rightSide) {
- Set modifiedRightSide = new HashSet<>(rightSide);
- modifiedRightSide.remove(attr);
- Set temp = new HashSet<>(minimalCover);
- temp.remove(fd);
- temp.add(new FunctionalDependency(fd.leftSide, modifiedRightSide));
- if (new ClosureFinder().closure(fd.leftSide, temp).containsAll(fd.rightSide)) {
- minimalCover.remove(fd);
- minimalCover.add(new FunctionalDependency(fd.leftSide, modifiedRightSide));
- }
- }
- }
-
- // Step 2: Remove redundant FDs
- Set removed = new HashSet<>();
- for (FunctionalDependency fd1 : minimalCover) {
- Set temp = new HashSet<>(minimalCover);
- temp.remove(fd1);
- if (new ClosureFinder().closure(fd1.leftSide, temp).containsAll(fd1.rightSide)) {
- removed.add(fd1);
- }
- }
- minimalCover.removeAll(removed);
-
- return minimalCover;
- }
-}
-
-
-class FunctionalDependencyCoverage {
- public static boolean covers(Set F, Set E) {
- for (FunctionalDependency fd : E) {
- Set X = fd.leftSide;
- @SuppressWarnings("SuspiciousNameCombination")
- Set Y = fd.rightSide;
- Set closure = calculateClosure(X, F);
- if (!closure.containsAll(Y)) {
- return false;
- }
- }
- return true;
- }
-
- private static Set calculateClosure(Set X, Set F) {
- Set closure = new HashSet<>(X);
- boolean changed;
- do {
- changed = false;
- for (FunctionalDependency fd : F) {
- if (closure.containsAll(fd.leftSide) && !closure.containsAll(fd.rightSide)) {
- closure.addAll(fd.rightSide);
- changed = true;
- }
- }
- } while (changed);
- return closure;
- }
+/* A class involving Normalization Theoretic Algorithms.
+ * No need to review it but if it peeks your interest...
+ * Go right ahead. */
+
+import java.util.*;
+
+public class NormalizeAlgo {
+ public static void main(String[] args) {
+ // Example sets of functional dependencies
+ Set F = new HashSet<>();
+ Set E = new HashSet<>();
+
+ F.add(new FunctionalDependency(new HashSet<>(Collections.singletonList("A")),
+ new HashSet<>(Collections.singletonList("C"))));
+ F.add(new FunctionalDependency(new HashSet<>(Arrays.asList("A", "C")),
+ new HashSet<>(Collections.singletonList("D"))));
+ F.add(new FunctionalDependency(new HashSet<>(Collections.singletonList("E")),
+ new HashSet<>(Arrays.asList("A", "D"))));
+ F.add(new FunctionalDependency(new HashSet<>(Collections.singletonList("E")),
+ new HashSet<>(Collections.singletonList("H"))));
+ E.add(new FunctionalDependency(new HashSet<>(Collections.singletonList("A")),
+ new HashSet<>(Arrays.asList("C", "D"))));
+ E.add(new FunctionalDependency(new HashSet<>(Collections.singletonList("E")),
+ new HashSet<>(Arrays.asList("A", "H"))));
+
+ // Check if F covers E
+ boolean coversE = FunctionalDependencyCoverage.covers(F, E);
+ System.out.println("Does F cover E? " + coversE);
+
+ // Check if E covers F
+ boolean coversF = FunctionalDependencyCoverage.covers(E, F);
+ System.out.println("Does E cover F? " + coversF);
+
+ // Check if F and E are equivalent
+ boolean areEquivalent = coversE && coversF;
+ System.out.println("Are F and E equivalent? " + areEquivalent);
+ }
+}
+
+class FunctionalDependency {
+ Set leftSide;
+ Set rightSide;
+
+ public FunctionalDependency(Set leftSide, Set rightSide) {
+ this.leftSide = leftSide;
+ this.rightSide = rightSide;
+ }
+
+ @Override
+ public String toString() {
+ return leftSide + " -> " + rightSide;
+ }
+}
+
+class ClosureFinder {
+ Set closure(Set attributes, Set fds) {
+ Set closure = new HashSet<>(attributes);
+ boolean changed;
+ do {
+ changed = false;
+ for (FunctionalDependency fd : fds) {
+ if (closure.containsAll(fd.leftSide) && !closure.containsAll(fd.rightSide)) {
+ closure.addAll(fd.rightSide);
+ changed = true;
+ }
+ }
+ } while (changed);
+ System.out.println("Attributes: " + attributes + ", Closure: " + closure);
+ return closure;
+ }
+}
+
+class KeyFinder {
+ Set findKey(Set attributes, Set fds) {
+ ClosureFinder closureFinder = new ClosureFinder();
+ Set candidateKey = new HashSet<>(attributes);
+ for (String attr : attributes) {
+ Set temp = new HashSet<>(candidateKey);
+ temp.remove(attr);
+ if (closureFinder.closure(temp, fds).equals(attributes)) {
+ candidateKey.remove(attr);
+ }
+ }
+ return candidateKey;
+ }
+}
+
+class MinimalCoverFinder {
+ Set findMinimalCover(Set fds) {
+ Set minimalCover = new HashSet<>(fds);
+
+ // Step 1: Remove extraneous attributes from right side of each FD
+ for (FunctionalDependency fd : fds) {
+ Set rightSide = new HashSet<>(fd.rightSide);
+ for (String attr : rightSide) {
+ Set modifiedRightSide = new HashSet<>(rightSide);
+ modifiedRightSide.remove(attr);
+ Set temp = new HashSet<>(minimalCover);
+ temp.remove(fd);
+ temp.add(new FunctionalDependency(fd.leftSide, modifiedRightSide));
+ if (new ClosureFinder().closure(fd.leftSide, temp).containsAll(fd.rightSide)) {
+ minimalCover.remove(fd);
+ minimalCover.add(new FunctionalDependency(fd.leftSide, modifiedRightSide));
+ }
+ }
+ }
+
+ // Step 2: Remove redundant FDs
+ Set removed = new HashSet<>();
+ for (FunctionalDependency fd1 : minimalCover) {
+ Set temp = new HashSet<>(minimalCover);
+ temp.remove(fd1);
+ if (new ClosureFinder().closure(fd1.leftSide, temp).containsAll(fd1.rightSide)) {
+ removed.add(fd1);
+ }
+ }
+ minimalCover.removeAll(removed);
+
+ return minimalCover;
+ }
+}
+
+
+class FunctionalDependencyCoverage {
+ public static boolean covers(Set F, Set E) {
+ for (FunctionalDependency fd : E) {
+ Set X = fd.leftSide;
+ @SuppressWarnings("SuspiciousNameCombination")
+ Set Y = fd.rightSide;
+ Set closure = calculateClosure(X, F);
+ if (!closure.containsAll(Y)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static Set calculateClosure(Set X, Set F) {
+ Set closure = new HashSet<>(X);
+ boolean changed;
+ do {
+ changed = false;
+ for (FunctionalDependency fd : F) {
+ if (closure.containsAll(fd.leftSide) && !closure.containsAll(fd.rightSide)) {
+ closure.addAll(fd.rightSide);
+ changed = true;
+ }
+ }
+ } while (changed);
+ return closure;
+ }
}
\ No newline at end of file
diff --git a/Hibernate_CS308/src/main/java/com/sci/TesterApp.java b/Hibernate_CS308/src/main/java/com/sci/TesterApp.java
index 1c9fa2a..f2c7200 100644
--- a/Hibernate_CS308/src/main/java/com/sci/TesterApp.java
+++ b/Hibernate_CS308/src/main/java/com/sci/TesterApp.java
@@ -1,31 +1,60 @@
-package com.sci;
-
-import com.sci.criteria.FilterQuery;
-import com.sci.criteria.Operator;
-import com.sci.dao.DBConfig;
-import com.sci.dao.DBManagement;
-import com.sci.models.*;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-public class TesterApp {
- public static void main(String[] args) {
- DBManagement.DBEmployee emp = new DBManagement.DBEmployee();
- List filters = new ArrayList<>();
-
- // Q13, Les02 Oracle SQL Slides:
- filters.add(new FilterQuery("jobId", Arrays.asList("SA_REP", "ST_CLERK"),
- Operator.In));
- filters.add(new FilterQuery("salary", Arrays.asList(2500, 3500, 7000),
- Operator.In));
- List emps = emp.getByFilter(filters);
- System.out.println("LastName, JobId, Salary");
- for (Employee employee : emps) {
- System.out.println(employee.getLastName() + " " + employee.getJobId() + " "
- + employee.getSalary());
- }
- DBConfig.shutdown();
- }
-}
+package com.sci;
+
+import com.sci.criteria.FilterQuery;
+import com.sci.criteria.Operator;
+import com.sci.dao.DBConfig;
+import com.sci.dao.GenericDAO;
+import com.sci.models.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class TesterApp {
+ public static void main(String[] args) {
+ // Create a generic DAO for Employee entity
+ GenericDAO employeeDAO = new GenericDAO<>(Employee.class);
+
+ //* Use the findByFilter method from GenericDAO
+/*
+ List filters = new ArrayList<>();
+
+ // Q13, Les02 Oracle SQL Slides:
+ filters.add(new FilterQuery("jobId", Arrays.asList("SA_REP", "ST_CLERK"),
+ Operator.In));
+ filters.add(new FilterQuery("salary", Arrays.asList(2500, 3500, 7000),
+ Operator.In));
+
+ List emps = employeeDAO.findByFilter(filters, false);
+*/
+ //* Use the findByFilter method from GenericDAO
+ List emps = employeeDAO.findWithCustomPredicate((cb, root) ->
+ cb.and(
+ root.get("jobId").in(Arrays.asList("SA_REP", "ST_CLERK")),
+ cb.or(
+ cb.equal(root.get("salary"), 2500),
+ cb.equal(root.get("salary"), 3500),
+ cb.equal(root.get("salary"), 7000)
+ )
+/*
+ cb.in(root.get("salary"))
+ .value(2500)
+ .value(3500)
+ .value(7000)
+*/
+
+ )
+ );
+
+
+ System.out.printf("%-15s %-10s %-10s%n", "LastName", "JobId", "Salary");
+ for (Employee employee : emps) {
+ System.out.printf("%-15s %-10s %-10d%n",
+ employee.getLastName(),
+ employee.getJobId(),
+ employee.getSalary());
+ }
+
+ DBConfig.shutdown();
+ }
+}
diff --git a/Hibernate_CS308/src/main/java/com/sci/dao/DBConfig.java b/Hibernate_CS308/src/main/java/com/sci/dao/DBConfig.java
index 6df3bbb..8b87167 100644
--- a/Hibernate_CS308/src/main/java/com/sci/dao/DBConfig.java
+++ b/Hibernate_CS308/src/main/java/com/sci/dao/DBConfig.java
@@ -5,12 +5,13 @@
public class DBConfig {
- public static final SessionFactory SESSION_FACTORY = new Configuration().configure()
- .buildSessionFactory();
+ public static final SessionFactory SESSION_FACTORY = new Configuration()
+ .configure()
+ .buildSessionFactory();
- public static void shutdown() {
- if (SESSION_FACTORY != null) {
- SESSION_FACTORY.close();
+ public static void shutdown() {
+ if (SESSION_FACTORY.isOpen()) {
+ SESSION_FACTORY.close();
+ }
}
- }
}
diff --git a/Hibernate_CS308/src/main/java/com/sci/dao/DBManagement.java b/Hibernate_CS308/src/main/java/com/sci/dao/DBManagement.java
deleted file mode 100644
index 6b5d741..0000000
--- a/Hibernate_CS308/src/main/java/com/sci/dao/DBManagement.java
+++ /dev/null
@@ -1,1005 +0,0 @@
-package com.sci.dao;
-
-import com.sci.criteria.FilterQuery;
-import com.sci.models.*;
-
-import java.sql.Date;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.hibernate.Session;
-import org.hibernate.Transaction;
-import org.hibernate.query.Query;
-
-import javax.persistence.criteria.CriteriaBuilder;
-import javax.persistence.criteria.CriteriaQuery;
-import javax.persistence.criteria.Predicate;
-import javax.persistence.criteria.Root;
-
-@SuppressWarnings("ALL")
-public class DBManagement {
- public static class DBCar {
-
- public List get() {
-
- try (Session session = DBConfig.SESSION_FACTORY.openSession()) {
-
- //noinspection unchecked
- return session.createQuery("FROM Car ").list();
-
- } catch (Exception ex) {
- System.err.println(ex.getMessage());
- }
-
- return null;
- }
-
- public Car read(Integer Id) {
-
- try (Session session = DBConfig.SESSION_FACTORY.openSession()) {
-
- return session.get(Car.class, Id);
-
- } catch (Exception ex) {
- System.err.println(ex.getMessage());
- }
-
- return null;
- }
-
-
- public Integer create(Car car) {
-
- Transaction transaction = null;
- int Id = 0;
-
- try (Session session = DBConfig.SESSION_FACTORY.openSession()) {
-
- transaction = session.beginTransaction();
-
- Id = (Integer) session.save(car);
-
- transaction.commit();
-
- } catch (Exception ex) {
- if (transaction != null) {
- transaction.rollback();
- }
- System.err.println(ex.getMessage());
- }
-
- return Id;
- }
-
- public void update(Car car) {
-
- Transaction transaction = null;
-
- try (Session session = DBConfig.SESSION_FACTORY.openSession()) {
-
- transaction = session.beginTransaction();
-
- session.update(car);
-
- transaction.commit();
-
- } catch (Exception ex) {
- if (transaction != null) {
- transaction.rollback();
- }
- System.err.println(ex.getMessage());
- }
- }
-
- public void delete(Integer Id) {
-
- Transaction transaction = null;
-
- try (Session session = DBConfig.SESSION_FACTORY.openSession()) {
-
- transaction = session.beginTransaction();
-
- Car car = read(Id);
-
- session.delete(car);
-
- transaction.commit();
-
- } catch (Exception ex) {
- if (transaction != null) {
- transaction.rollback();
- }
- System.err.println(ex.getMessage());
- }
- }
-
- }
-
- public static class DBPerson {
-
- public List get() {
-
- try (Session session = DBConfig.SESSION_FACTORY.openSession()) {
-
- //noinspection unchecked
- return session.createQuery("FROM Person ").list();
-
- } catch (Exception ex) {
- System.err.println(ex.getMessage());
- }
-
- return null;
- }
-
- public Person read(Integer Id) {
-
- try (Session session = DBConfig.SESSION_FACTORY.openSession()) {
-
- return session.get(Person.class, Id);
-
- } catch (Exception ex) {
- System.err.println(ex.getMessage());
- }
-
- return null;
- }
-
-
- public Integer create(Person person) {
-
- Transaction transaction = null;
- int Id = 0;
-
- try (Session session = DBConfig.SESSION_FACTORY.openSession()) {
-
- transaction = session.beginTransaction();
-
- Id = (Integer) session.save(person);
-
- transaction.commit();
-
- } catch (Exception ex) {
- if (transaction != null) {
- transaction.rollback();
- }
- System.err.println(ex.getMessage());
- }
-
- return Id;
- }
-
- public void update(Person person) {
-
- Transaction transaction = null;
-
- try (Session session = DBConfig.SESSION_FACTORY.openSession()) {
-
- transaction = session.beginTransaction();
-
- session.update(person);
-
- transaction.commit();
-
- } catch (Exception ex) {
- if (transaction != null) {
- transaction.rollback();
- }
- System.err.println(ex.getMessage());
- }
- }
-
- public void delete(Integer Id) {
-
- Transaction transaction = null;
-
- try (Session session = DBConfig.SESSION_FACTORY.openSession()) {
-
- transaction = session.beginTransaction();
-
- Person person = read(Id);
-
- session.delete(person);
-
- transaction.commit();
-
- } catch (Exception ex) {
- if (transaction != null) {
- transaction.rollback();
- }
- System.err.println(ex.getMessage());
- }
- }
-
- }
-
- public static class DBEmployee {
- public List getByFilter(List filterQueries) {
-
- try (Session session = DBConfig.SESSION_FACTORY.openSession()) {
- // To be edited in other relations CRUD OPs:
- CriteriaBuilder cb = session.getCriteriaBuilder();
- CriteriaQuery cr = cb.createQuery(Employee.class);
- Root root = cr.from(Employee.class);
-
- Predicate[] predicates = new Predicate[filterQueries.size()];
- for (int i = 0; i < filterQueries.size(); i++) {
- switch (filterQueries.get(i).getOp()) {
- case Equal:
- predicates[i] =
- cb.equal(root.get(filterQueries.get(i).
- getAttributeName()),
- filterQueries.get(i).getAttributeValue());
- break;
- case GreaterThan:
- predicates[i] =
- cb.greaterThan(root.get(filterQueries.
- get(i).getAttributeName()),
- (Comparable) filterQueries.get(i).
- getAttributeValue());
- break;
- case LessThan:
- predicates[i] =
- cb.lessThan(root.get(filterQueries.get(i).
- getAttributeName()),
- (Comparable) filterQueries.get(i).
- getAttributeValue());
- break;
- case NotEqual:
- predicates[i] =
- cb.notEqual(root.get(filterQueries.get(i).
- getAttributeName()),
- filterQueries.get(i).
- getAttributeValue());
- break;
- case IsNull:
- predicates[i] =
- cb.isNull(root.get(filterQueries.get(i).
- getAttributeName())
- );
- break;
- case GreaterThanOrEqual:
- predicates[i] =
- cb.greaterThanOrEqualTo(
- root.get(filterQueries.get(i).
- getAttributeName()),
- (Comparable) filterQueries.get(i).
- getAttributeValue());
- break;
- case LessThanOrEqual:
- predicates[i] =
- cb.lessThanOrEqualTo(
- root.get(filterQueries.get(i).
- getAttributeName()),
- (Comparable) filterQueries.get(i).
- getAttributeValue());
- break;
- case Like:
- /* <===> Where attribute like '%string%', can be cutomized. */
- predicates[i] =
- cb.like(root.get(filterQueries.get(i).
- getAttributeName()),
- "%" + (String) (filterQueries.get(i).
- getAttributeValue()) + "%");
- break;
- case Between:
- List values =
- (List) filterQueries.get(i).
- getAttributeValue();
- predicates[i] =
- cb.between(root.get(filterQueries.get(i).
- getAttributeName()),
- values.get(0), values.get(1));
- break;
- case In:
- List