Guide to JDO (#1736)
* Guide to JDO * Guide to JDO * Guide to JDO * Guide To JDO
This commit is contained in:
parent
e29b8394e0
commit
bf02497b7c
@ -1,33 +1,55 @@
|
|||||||
package com.baeldung.jdo;
|
package com.baeldung.jdo;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.jdo.JDOHelper;
|
|
||||||
import javax.jdo.PersistenceManager;
|
import javax.jdo.PersistenceManager;
|
||||||
import javax.jdo.PersistenceManagerFactory;
|
import javax.jdo.PersistenceManagerFactory;
|
||||||
import javax.jdo.Query;
|
import javax.jdo.Query;
|
||||||
import javax.jdo.Transaction;
|
import javax.jdo.Transaction;
|
||||||
|
|
||||||
|
import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
|
||||||
|
import org.datanucleus.metadata.PersistenceUnitMetaData;
|
||||||
|
|
||||||
public class GuideToJDO {
|
public class GuideToJDO {
|
||||||
|
|
||||||
private static final Logger LOGGER = Logger.getLogger(GuideToJDO.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(GuideToJDO.class.getName());
|
||||||
private Random rnd = new Random();
|
private Random rnd = new Random();
|
||||||
|
private PersistenceUnitMetaData pumd;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new GuideToJDO();
|
new GuideToJDO();
|
||||||
}
|
}
|
||||||
|
|
||||||
public GuideToJDO() {
|
public GuideToJDO() {
|
||||||
|
CreateProperties();
|
||||||
CreateProducts();
|
CreateProducts();
|
||||||
ListProducts();
|
ListProducts();
|
||||||
|
UpdateProducts();
|
||||||
|
ListProducts();
|
||||||
|
DeleteProducts();
|
||||||
|
ListProducts();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreateProperties(){
|
||||||
|
|
||||||
|
pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
|
||||||
|
pumd.addClassName("com.baeldung.jdo.Product");
|
||||||
|
pumd.setExcludeUnlistedClasses();
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
|
||||||
|
pumd.addProperty("datanucleus.autoCreateSchema", "true");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CreateProducts() {
|
public void CreateProducts() {
|
||||||
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("Tutorial");
|
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||||
PersistenceManager pm = pmf.getPersistenceManager();
|
PersistenceManager pm = pmf.getPersistenceManager();
|
||||||
Transaction tx = pm.currentTransaction();
|
Transaction tx = pm.currentTransaction();
|
||||||
try {
|
try {
|
||||||
@ -51,26 +73,64 @@ public class GuideToJDO {
|
|||||||
}
|
}
|
||||||
pm.close();
|
pm.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("rawtypes")
|
||||||
|
public void UpdateProducts(){
|
||||||
|
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||||
|
PersistenceManager pm = pmf.getPersistenceManager();
|
||||||
|
Transaction tx = pm.currentTransaction();
|
||||||
|
try {
|
||||||
|
tx.begin();
|
||||||
|
Query query = pm.newQuery(Product.class, "name == \"Phone\"");
|
||||||
|
Collection result = (Collection) query.execute();
|
||||||
|
Product product = (Product) result.iterator().next();
|
||||||
|
product.setName("Android Phone");
|
||||||
|
tx.commit();
|
||||||
|
} finally {
|
||||||
|
if (tx.isActive()) {
|
||||||
|
tx.rollback();
|
||||||
|
}
|
||||||
|
pm.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
public void DeleteProducts(){
|
||||||
|
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||||
|
PersistenceManager pm = pmf.getPersistenceManager();
|
||||||
|
Transaction tx = pm.currentTransaction();
|
||||||
|
try {
|
||||||
|
tx.begin();
|
||||||
|
Query query = pm.newQuery(Product.class, "name == \"Android Phone\"");
|
||||||
|
Collection result = (Collection) query.execute();
|
||||||
|
Product product = (Product) result.iterator().next();
|
||||||
|
pm.deletePersistent(product);
|
||||||
|
tx.commit();
|
||||||
|
} finally {
|
||||||
|
if (tx.isActive()) {
|
||||||
|
tx.rollback();
|
||||||
|
}
|
||||||
|
pm.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
public void ListProducts() {
|
public void ListProducts() {
|
||||||
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("Tutorial");
|
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||||
PersistenceManager pm = pmf.getPersistenceManager();
|
PersistenceManager pm = pmf.getPersistenceManager();
|
||||||
Transaction tx = pm.currentTransaction();
|
Transaction tx = pm.currentTransaction();
|
||||||
try {
|
try {
|
||||||
tx.begin();
|
tx.begin();
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
Query q = pm.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price > 10");
|
||||||
Query q = pm.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price < 1");
|
|
||||||
List<Product> products = (List<Product>) q.execute();
|
List<Product> products = (List<Product>) q.execute();
|
||||||
Iterator<Product> iter = products.iterator();
|
Iterator<Product> iter = products.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
Product p = iter.next();
|
Product p = iter.next();
|
||||||
LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
|
LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
|
||||||
}
|
}
|
||||||
|
LOGGER.log(Level.INFO, "--------------------------------------------------------------");
|
||||||
tx.commit();
|
tx.commit();
|
||||||
} finally {
|
} finally {
|
||||||
if (tx.isActive()) {
|
if (tx.isActive()) {
|
||||||
|
@ -6,18 +6,28 @@ import static org.junit.Assert.fail;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.jdo.JDOHelper;
|
|
||||||
import javax.jdo.PersistenceManager;
|
import javax.jdo.PersistenceManager;
|
||||||
import javax.jdo.PersistenceManagerFactory;
|
import javax.jdo.PersistenceManagerFactory;
|
||||||
import javax.jdo.Query;
|
import javax.jdo.Query;
|
||||||
import javax.jdo.Transaction;
|
import javax.jdo.Transaction;
|
||||||
|
|
||||||
|
import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
|
||||||
|
import org.datanucleus.metadata.PersistenceUnitMetaData;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class GuideToJDOTest {
|
public class GuideToJDOTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenProduct_WhenNewThenPerformTransaction() {
|
public void givenProduct_WhenNewThenPerformTransaction() {
|
||||||
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("Tutorial");
|
PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
|
||||||
|
pumd.addClassName("com.baeldung.jdo.Product");
|
||||||
|
pumd.setExcludeUnlistedClasses();
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
|
||||||
|
pumd.addProperty("datanucleus.autoCreateSchema", "true");
|
||||||
|
|
||||||
|
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||||
PersistenceManager pm = pmf.getPersistenceManager();
|
PersistenceManager pm = pmf.getPersistenceManager();
|
||||||
Transaction tx = pm.currentTransaction();
|
Transaction tx = pm.currentTransaction();
|
||||||
try {
|
try {
|
||||||
@ -43,7 +53,16 @@ public class GuideToJDOTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenProduct_WhenQueryThenExist() {
|
public void givenProduct_WhenQueryThenExist() {
|
||||||
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("Tutorial");
|
PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
|
||||||
|
pumd.addClassName("com.baeldung.jdo.Product");
|
||||||
|
pumd.setExcludeUnlistedClasses();
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
|
||||||
|
pumd.addProperty("datanucleus.autoCreateSchema", "true");
|
||||||
|
|
||||||
|
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||||
PersistenceManager pm = pmf.getPersistenceManager();
|
PersistenceManager pm = pmf.getPersistenceManager();
|
||||||
Transaction tx = pm.currentTransaction();
|
Transaction tx = pm.currentTransaction();
|
||||||
try {
|
try {
|
||||||
@ -66,7 +85,7 @@ public class GuideToJDOTest {
|
|||||||
|
|
||||||
pmf.close();
|
pmf.close();
|
||||||
|
|
||||||
PersistenceManagerFactory pmf2 = JDOHelper.getPersistenceManagerFactory("Tutorial");
|
PersistenceManagerFactory pmf2 = new JDOPersistenceManagerFactory(pumd, null);
|
||||||
PersistenceManager pm2 = pmf2.getPersistenceManager();
|
PersistenceManager pm2 = pmf2.getPersistenceManager();
|
||||||
Transaction tx2 = pm2.currentTransaction();
|
Transaction tx2 = pm2.currentTransaction();
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user