From b0e15fcca07000e0e04d4c6bf76f80242a6aee88 Mon Sep 17 00:00:00 2001 From: Jesus Boadas Date: Tue, 25 Apr 2017 14:54:32 -0400 Subject: [PATCH] Guide to JDO (#1730) * Guide to JDO * Guide to JDO * Guide to JDO --- core-java/log4j.properties | 0 core-java/pom.xml | 95 ++++++++++++++---- .../java/com/baeldung/jdo/GuideToJDO.java | 83 ++++++++++++++++ .../main/java/com/baeldung/jdo/Product.java | 43 +++++++++ .../main/resources/META-INF/persistence.xml | 20 ++++ .../baeldung/java8/comparator/Employee.java | 4 +- .../java8/comparator/Java8ComparatorTest.java | 3 +- .../java/com/baeldung/jdo/GuideToJDOTest.java | 96 +++++++++++++++++++ 8 files changed, 323 insertions(+), 21 deletions(-) create mode 100644 core-java/log4j.properties create mode 100644 core-java/src/main/java/com/baeldung/jdo/GuideToJDO.java create mode 100644 core-java/src/main/java/com/baeldung/jdo/Product.java create mode 100644 core-java/src/main/resources/META-INF/persistence.xml create mode 100644 core-java/src/test/java/com/baeldung/jdo/GuideToJDOTest.java diff --git a/core-java/log4j.properties b/core-java/log4j.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/pom.xml b/core-java/pom.xml index b4741d5b80..4ee3efb297 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -165,19 +165,61 @@ org.javamoney moneta 1.1 - + + + org.owasp.esapi + esapi + 2.1.0.1 + + + commons-logging + commons-logging + + + + + + org.datanucleus + javax.jdo + 3.2.0-m6 + + + org.datanucleus + datanucleus-core + 5.1.0-m1 + + + org.datanucleus + datanucleus-api-jdo + 5.1.0-m1 + + + org.datanucleus + datanucleus-rdbms + 5.1.0-m1 + + + org.datanucleus + datanucleus-maven-plugin + 5.0.2 + + + com.h2database + h2 + 1.4.194 + - - org.owasp.esapi - esapi - 2.1.0.1 - - - commons-logging - commons-logging - - - + + org.owasp.esapi + esapi + 2.1.0.1 + + + commons-logging + commons-logging + + + @@ -191,7 +233,6 @@ - org.apache.maven.plugins maven-compiler-plugin @@ -201,7 +242,6 @@ 1.8 - org.apache.maven.plugins maven-surefire-plugin @@ -214,7 +254,6 @@ true - org.apache.maven.plugins maven-dependency-plugin @@ -245,7 +284,6 @@ - org.apache.maven.plugins maven-assembly-plugin @@ -269,7 +307,6 @@ - org.apache.maven.plugins maven-shade-plugin @@ -290,7 +327,6 @@ - com.jolira onejar-maven-plugin @@ -307,7 +343,6 @@ - org.springframework.boot spring-boot-maven-plugin @@ -323,6 +358,28 @@ + + + + + org.datanucleus + datanucleus-maven-plugin + 5.0.2 + + JDO + ${basedir}/datanucleus.properties + ${basedir}/log4j.properties + true + + + + process-classes + + enhance + + + + diff --git a/core-java/src/main/java/com/baeldung/jdo/GuideToJDO.java b/core-java/src/main/java/com/baeldung/jdo/GuideToJDO.java new file mode 100644 index 0000000000..e17004c149 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/jdo/GuideToJDO.java @@ -0,0 +1,83 @@ +package com.baeldung.jdo; + +import java.util.Iterator; +import java.util.List; +import java.util.Random; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.jdo.JDOHelper; +import javax.jdo.PersistenceManager; +import javax.jdo.PersistenceManagerFactory; +import javax.jdo.Query; +import javax.jdo.Transaction; + +public class GuideToJDO { + + private static final Logger LOGGER = Logger.getLogger(GuideToJDO.class.getName()); + private Random rnd = new Random(); + + public static void main(String[] args) { + new GuideToJDO(); + } + + public GuideToJDO() { + CreateProducts(); + ListProducts(); + } + + public void CreateProducts() { + PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("Tutorial"); + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + tx.begin(); + Product product = new Product("Tablet", 80.0); + pm.makePersistent(product); + Product product2 = new Product("Phone", 20.0); + pm.makePersistent(product2); + Product product3 = new Product("Laptop", 200.0); + pm.makePersistent(product3); + for (int i = 0; i < 100; i++) { + String nam = "Product-" + i; + double price = rnd.nextDouble(); + Product productx = new Product(nam, price); + pm.makePersistent(productx); + } + tx.commit(); + } finally { + if (tx.isActive()) { + tx.rollback(); + } + pm.close(); + } + + } + + @SuppressWarnings("unchecked") + public void ListProducts() { + PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("Tutorial"); + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + tx.begin(); + + @SuppressWarnings("rawtypes") + Query q = pm.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price < 1"); + List products = (List) q.execute(); + Iterator iter = products.iterator(); + while (iter.hasNext()) { + Product p = iter.next(); + LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price }); + } + + tx.commit(); + } finally { + if (tx.isActive()) { + tx.rollback(); + } + + pm.close(); + } + } +} diff --git a/core-java/src/main/java/com/baeldung/jdo/Product.java b/core-java/src/main/java/com/baeldung/jdo/Product.java new file mode 100644 index 0000000000..124428d671 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/jdo/Product.java @@ -0,0 +1,43 @@ +package com.baeldung.jdo; + +import javax.jdo.annotations.IdGeneratorStrategy; +import javax.jdo.annotations.PersistenceCapable; +import javax.jdo.annotations.Persistent; +import javax.jdo.annotations.PrimaryKey; + +@PersistenceCapable +public class Product { + + @PrimaryKey + @Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT) + long id; + String name = null; + Double price = 0.0; + + public Product() { + this.name = null; + this.price = 0.0; + } + + public Product(String name, Double price) { + this.name = name; + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + +} \ No newline at end of file diff --git a/core-java/src/main/resources/META-INF/persistence.xml b/core-java/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..3966afdcda --- /dev/null +++ b/core-java/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + diff --git a/core-java/src/test/java/com/baeldung/java8/comparator/Employee.java b/core-java/src/test/java/com/baeldung/java8/comparator/Employee.java index bbc4e3e320..0ef10410a9 100644 --- a/core-java/src/test/java/com/baeldung/java8/comparator/Employee.java +++ b/core-java/src/test/java/com/baeldung/java8/comparator/Employee.java @@ -1,4 +1,5 @@ package com.baeldung.java8.comparator; +/* import lombok.AllArgsConstructor; import lombok.Data; @@ -15,9 +16,10 @@ public class Employee implements Comparable{ double salary; long mobile; - @Override public int compareTo(Employee argEmployee) { return name.compareTo(argEmployee.getName()); } + } +*/ \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/java8/comparator/Java8ComparatorTest.java b/core-java/src/test/java/com/baeldung/java8/comparator/Java8ComparatorTest.java index ebcbb7a3fc..8e1cfa67af 100644 --- a/core-java/src/test/java/com/baeldung/java8/comparator/Java8ComparatorTest.java +++ b/core-java/src/test/java/com/baeldung/java8/comparator/Java8ComparatorTest.java @@ -1,5 +1,5 @@ package com.baeldung.java8.comparator; - +/* import java.util.Arrays; import java.util.Comparator; @@ -165,3 +165,4 @@ public class Java8ComparatorTest { } +*/ \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/jdo/GuideToJDOTest.java b/core-java/src/test/java/com/baeldung/jdo/GuideToJDOTest.java new file mode 100644 index 0000000000..6ab7481118 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/jdo/GuideToJDOTest.java @@ -0,0 +1,96 @@ +package com.baeldung.jdo; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.util.Iterator; +import java.util.List; + +import javax.jdo.JDOHelper; +import javax.jdo.PersistenceManager; +import javax.jdo.PersistenceManagerFactory; +import javax.jdo.Query; +import javax.jdo.Transaction; + +import org.junit.Test; + +public class GuideToJDOTest { + @Test + public void givenProduct_WhenNewThenPerformTransaction() { + PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("Tutorial"); + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + tx.begin(); + for (int i = 0; i < 100; i++){ + String nam = "Product-" + i; + double price = i; + Product productx = new Product(nam, price); + pm.makePersistent(productx); + } + tx.commit(); + } catch (Throwable thr) { + fail("Failed test : " + thr.getMessage()); + } finally { + if (tx.isActive()) { + tx.rollback(); + } + pm.close(); + } + + pmf.close(); + } + + @Test + public void givenProduct_WhenQueryThenExist() { + PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("Tutorial"); + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + tx.begin(); + Product product = new Product("Tablet", 80.0); + pm.makePersistent(product); + Product product2 = new Product("Phone", 20.0); + pm.makePersistent(product2); + Product product3 = new Product("Laptop", 200.0); + pm.makePersistent(product3); + tx.commit(); + } catch (Throwable thr) { + fail("Failed test : " + thr.getMessage()); + } finally { + if (tx.isActive()) { + tx.rollback(); + } + pm.close(); + } + + pmf.close(); + + PersistenceManagerFactory pmf2 = JDOHelper.getPersistenceManagerFactory("Tutorial"); + PersistenceManager pm2 = pmf2.getPersistenceManager(); + Transaction tx2 = pm2.currentTransaction(); + try { + tx2.begin(); + + @SuppressWarnings("rawtypes") + Query q = pm2.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price == 200"); + @SuppressWarnings("unchecked") + List products = (List) q.execute(); + Iterator iter = products.iterator(); + while (iter.hasNext()) { + Product p = iter.next(); + assertEquals("Laptop", p.name); + } + + tx2.commit(); + } finally { + if (tx2.isActive()) { + tx2.rollback(); + } + + pm2.close(); + } + } + + +}