11package com .baeldung .jdo ;
22
3+ import java .util .Collection ;
34import java .util .Iterator ;
45import java .util .List ;
56import java .util .Random ;
67import java .util .logging .Level ;
78import java .util .logging .Logger ;
89
9- import javax .jdo .JDOHelper ;
1010import javax .jdo .PersistenceManager ;
1111import javax .jdo .PersistenceManagerFactory ;
1212import javax .jdo .Query ;
1313import javax .jdo .Transaction ;
1414
15+ import org .datanucleus .api .jdo .JDOPersistenceManagerFactory ;
16+ import org .datanucleus .metadata .PersistenceUnitMetaData ;
17+
1518public class GuideToJDO {
1619
1720 private static final Logger LOGGER = Logger .getLogger (GuideToJDO .class .getName ());
1821 private Random rnd = new Random ();
22+ private PersistenceUnitMetaData pumd ;
1923
2024 public static void main (String [] args ) {
2125 new GuideToJDO ();
2226 }
2327
2428 public GuideToJDO () {
29+ CreateProperties ();
2530 CreateProducts ();
2631 ListProducts ();
32+ UpdateProducts ();
33+ ListProducts ();
34+ DeleteProducts ();
35+ ListProducts ();
2736 }
2837
38+ public void CreateProperties (){
39+
40+ pumd = new PersistenceUnitMetaData ("dynamic-unit" , "RESOURCE_LOCAL" , null );
41+ pumd .addClassName ("com.baeldung.jdo.Product" );
42+ pumd .setExcludeUnlistedClasses ();
43+ pumd .addProperty ("javax.jdo.option.ConnectionDriverName" , "org.h2.Driver" );
44+ pumd .addProperty ("javax.jdo.option.ConnectionURL" , "jdbc:h2:mem:mypersistence" );
45+ pumd .addProperty ("javax.jdo.option.ConnectionUserName" , "sa" );
46+ pumd .addProperty ("javax.jdo.option.ConnectionPassword" , "" );
47+ pumd .addProperty ("datanucleus.autoCreateSchema" , "true" );
48+
49+ }
50+
2951 public void CreateProducts () {
30- PersistenceManagerFactory pmf = JDOHelper . getPersistenceManagerFactory ( "Tutorial" );
52+ PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory ( pumd , null );
3153 PersistenceManager pm = pmf .getPersistenceManager ();
3254 Transaction tx = pm .currentTransaction ();
3355 try {
@@ -51,26 +73,64 @@ public void CreateProducts() {
5173 }
5274 pm .close ();
5375 }
54-
5576 }
5677
57- @ SuppressWarnings ("unchecked" )
78+ @ SuppressWarnings ("rawtypes" )
79+ public void UpdateProducts (){
80+ PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory (pumd , null );
81+ PersistenceManager pm = pmf .getPersistenceManager ();
82+ Transaction tx = pm .currentTransaction ();
83+ try {
84+ tx .begin ();
85+ Query query = pm .newQuery (Product .class , "name == \" Phone\" " );
86+ Collection result = (Collection ) query .execute ();
87+ Product product = (Product ) result .iterator ().next ();
88+ product .setName ("Android Phone" );
89+ tx .commit ();
90+ } finally {
91+ if (tx .isActive ()) {
92+ tx .rollback ();
93+ }
94+ pm .close ();
95+ }
96+ }
97+
98+ @ SuppressWarnings ("rawtypes" )
99+ public void DeleteProducts (){
100+ PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory (pumd , null );
101+ PersistenceManager pm = pmf .getPersistenceManager ();
102+ Transaction tx = pm .currentTransaction ();
103+ try {
104+ tx .begin ();
105+ Query query = pm .newQuery (Product .class , "name == \" Android Phone\" " );
106+ Collection result = (Collection ) query .execute ();
107+ Product product = (Product ) result .iterator ().next ();
108+ pm .deletePersistent (product );
109+ tx .commit ();
110+ } finally {
111+ if (tx .isActive ()) {
112+ tx .rollback ();
113+ }
114+ pm .close ();
115+ }
116+ }
117+
118+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
58119 public void ListProducts () {
59- PersistenceManagerFactory pmf = JDOHelper . getPersistenceManagerFactory ( "Tutorial" );
120+ PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory ( pumd , null );
60121 PersistenceManager pm = pmf .getPersistenceManager ();
61122 Transaction tx = pm .currentTransaction ();
62123 try {
63124 tx .begin ();
64125
65- @ SuppressWarnings ("rawtypes" )
66- Query q = pm .newQuery ("SELECT FROM " + Product .class .getName () + " WHERE price < 1" );
126+ Query q = pm .newQuery ("SELECT FROM " + Product .class .getName () + " WHERE price > 10" );
67127 List <Product > products = (List <Product >) q .execute ();
68128 Iterator <Product > iter = products .iterator ();
69129 while (iter .hasNext ()) {
70130 Product p = iter .next ();
71131 LOGGER .log (Level .WARNING , "Product name: {0} - Price: {1}" , new Object [] { p .name , p .price });
72132 }
73-
133+ LOGGER . log ( Level . INFO , "--------------------------------------------------------------" );
74134 tx .commit ();
75135 } finally {
76136 if (tx .isActive ()) {
@@ -80,4 +140,4 @@ public void ListProducts() {
80140 pm .close ();
81141 }
82142 }
83- }
143+ }
0 commit comments