@@ -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<Product> products = (List<Product>) q.execute();
|
||||
Iterator<Product> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">
|
||||
|
||||
<!-- JDO tutorial "unit" -->
|
||||
<persistence-unit name="Tutorial">
|
||||
<exclude-unlisted-classes/>
|
||||
<properties>
|
||||
<property name="javax.jdo.PersistenceManagerFactoryClass" value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory"/>
|
||||
<property name="javax.jdo.option.ConnectionURL" value="jdbc:h2:mem:mypersistence"/>
|
||||
<property name="javax.jdo.option.ConnectionDriverName" value="org.h2.Driver"/>
|
||||
<property name="javax.jdo.option.ConnectionUserName" value="sa"/>
|
||||
<property name="javax.jdo.option.ConnectionPassword" value=""/>
|
||||
<property name="datanucleus.schema.autoCreateAll" value="true"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
package com.baeldung.java8.comparator;
|
||||
/*
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@@ -15,9 +16,10 @@ public class Employee implements Comparable<Employee>{
|
||||
double salary;
|
||||
long mobile;
|
||||
|
||||
|
||||
@Override
|
||||
public int compareTo(Employee argEmployee) {
|
||||
return name.compareTo(argEmployee.getName());
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.baeldung.java8.comparator;
|
||||
|
||||
/*
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
@@ -165,3 +165,4 @@ public class Java8ComparatorTest {
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
@@ -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<Product> products = (List<Product>) q.execute();
|
||||
Iterator<Product> iter = products.iterator();
|
||||
while (iter.hasNext()) {
|
||||
Product p = iter.next();
|
||||
assertEquals("Laptop", p.name);
|
||||
}
|
||||
|
||||
tx2.commit();
|
||||
} finally {
|
||||
if (tx2.isActive()) {
|
||||
tx2.rollback();
|
||||
}
|
||||
|
||||
pm2.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user