@@ -20,22 +20,29 @@ public class GuideToJDO {
2020 private static final Logger LOGGER = Logger .getLogger (GuideToJDO .class .getName ());
2121 private Random rnd = new Random ();
2222 private PersistenceUnitMetaData pumd ;
23+ private PersistenceUnitMetaData pumdXML ;
2324
2425 public static void main (String [] args ) {
2526 new GuideToJDO ();
2627 }
2728
2829 public GuideToJDO () {
29- CreateProperties ();
30+ CreateH2Properties ();
31+ CreateXMLProperties ();
3032 CreateProducts ();
3133 ListProducts ();
34+ QueryJDOQL ();
35+ QuerySQL ();
36+ QueryJPQL ();
3237 UpdateProducts ();
3338 ListProducts ();
3439 DeleteProducts ();
3540 ListProducts ();
41+ persistXML ();
42+ listXMLProducts ();
3643 }
3744
38- public void CreateProperties (){
45+ public void CreateH2Properties (){
3946
4047 pumd = new PersistenceUnitMetaData ("dynamic-unit" , "RESOURCE_LOCAL" , null );
4148 pumd .addClassName ("com.baeldung.jdo.Product" );
@@ -48,6 +55,14 @@ public void CreateProperties(){
4855
4956 }
5057
58+ public void CreateXMLProperties (){
59+ pumdXML = new PersistenceUnitMetaData ("dynamic-unit" , "RESOURCE_LOCAL" , null );
60+ pumdXML .addClassName ("com.baeldung.jdo.ProductXML" );
61+ pumdXML .setExcludeUnlistedClasses ();
62+ pumdXML .addProperty ("javax.jdo.option.ConnectionURL" , "xml:file:myPersistence.xml" );
63+ pumdXML .addProperty ("datanucleus.autoCreateSchema" , "true" );
64+ }
65+
5166 public void CreateProducts () {
5267 PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory (pumd , null );
5368 PersistenceManager pm = pmf .getPersistenceManager ();
@@ -140,4 +155,145 @@ public void ListProducts() {
140155 pm .close ();
141156 }
142157 }
158+
159+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
160+ public void QueryJDOQL (){
161+ PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory (pumd , null );
162+ PersistenceManager pm = pmf .getPersistenceManager ();
163+ Transaction tx = pm .currentTransaction ();
164+ try {
165+ tx .begin ();
166+
167+ // Declarative JDOQL :
168+ LOGGER .log (Level .INFO , "Declarative JDOQL --------------------------------------------------------------" );
169+ Query qDJDOQL = pm .newQuery (Product .class );
170+ qDJDOQL .setFilter ("name == 'Tablet' && price == price_value" );
171+ qDJDOQL .declareParameters ("double price_value" );
172+ List <Product > resultsqDJDOQL = qDJDOQL .setParameters (80.0 ).executeList ();
173+
174+ Iterator <Product > iterDJDOQL = resultsqDJDOQL .iterator ();
175+ while (iterDJDOQL .hasNext ()) {
176+ Product p = iterDJDOQL .next ();
177+ LOGGER .log (Level .WARNING , "Product name: {0} - Price: {1}" , new Object [] { p .name , p .price });
178+ }
179+ LOGGER .log (Level .INFO , "--------------------------------------------------------------" );
180+
181+ tx .commit ();
182+ } finally {
183+ if (tx .isActive ()) {
184+ tx .rollback ();
185+ }
186+
187+ pm .close ();
188+ }
189+ }
190+
191+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
192+ public void QuerySQL (){
193+ PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory (pumd , null );
194+ PersistenceManager pm = pmf .getPersistenceManager ();
195+ Transaction tx = pm .currentTransaction ();
196+ try {
197+ tx .begin ();
198+
199+ //SQL :
200+ LOGGER .log (Level .INFO , "SQL --------------------------------------------------------------" );
201+ Query query = pm .newQuery ("javax.jdo.query.SQL" , "SELECT * FROM PRODUCT" );
202+ query .setClass (Product .class );
203+ List <Product > results = query .executeList ();
204+
205+ Iterator <Product > iter = results .iterator ();
206+ while (iter .hasNext ()) {
207+ Product p = iter .next ();
208+ LOGGER .log (Level .WARNING , "Product name: {0} - Price: {1}" , new Object [] { p .name , p .price });
209+ }
210+ LOGGER .log (Level .INFO , "--------------------------------------------------------------" );
211+
212+ tx .commit ();
213+ } finally {
214+ if (tx .isActive ()) {
215+ tx .rollback ();
216+ }
217+
218+ pm .close ();
219+ }
220+ }
221+
222+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
223+ public void QueryJPQL (){
224+ PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory (pumd , null );
225+ PersistenceManager pm = pmf .getPersistenceManager ();
226+ Transaction tx = pm .currentTransaction ();
227+ try {
228+ tx .begin ();
229+
230+ //JPQL :
231+ LOGGER .log (Level .INFO , "JPQL --------------------------------------------------------------" );
232+ Query q = pm .newQuery ("JPQL" , "SELECT p FROM " +Product .class .getName ()+" p WHERE p.name = 'Laptop'" );
233+ List results = (List )q .execute ();
234+
235+ Iterator <Product > iter = results .iterator ();
236+ while (iter .hasNext ()) {
237+ Product p = iter .next ();
238+ LOGGER .log (Level .WARNING , "Product name: {0} - Price: {1}" , new Object [] { p .name , p .price });
239+ }
240+ LOGGER .log (Level .INFO , "--------------------------------------------------------------" );
241+
242+ tx .commit ();
243+ } finally {
244+ if (tx .isActive ()) {
245+ tx .rollback ();
246+ }
247+
248+ pm .close ();
249+ }
250+ }
251+
252+ public void persistXML (){
253+ PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory (pumdXML , null );
254+ PersistenceManager pm = pmf .getPersistenceManager ();
255+ Transaction tx = pm .currentTransaction ();
256+ try {
257+ tx .begin ();
258+ ProductXML productXML = new ProductXML (0 ,"Tablet" , 80.0 );
259+ pm .makePersistent (productXML );
260+ ProductXML productXML2 = new ProductXML (1 ,"Phone" , 20.0 );
261+ pm .makePersistent (productXML2 );
262+ ProductXML productXML3 = new ProductXML (2 ,"Laptop" , 200.0 );
263+ pm .makePersistent (productXML3 );
264+ tx .commit ();
265+ } finally {
266+ if (tx .isActive ()) {
267+ tx .rollback ();
268+ }
269+ pm .close ();
270+ }
271+ }
272+
273+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
274+ public void listXMLProducts (){
275+ PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory (pumdXML , null );
276+ PersistenceManager pm = pmf .getPersistenceManager ();
277+ Transaction tx = pm .currentTransaction ();
278+ try {
279+ tx .begin ();
280+
281+ Query q = pm .newQuery ("SELECT FROM " + ProductXML .class .getName ());
282+ List <ProductXML > products = (List <ProductXML >) q .execute ();
283+ Iterator <ProductXML > iter = products .iterator ();
284+ while (iter .hasNext ()) {
285+ ProductXML p = iter .next ();
286+ LOGGER .log (Level .WARNING , "Product name: {0} - Price: {1}" , new Object [] { p .getName (), p .getPrice () });
287+ pm .deletePersistent (p );
288+ }
289+ LOGGER .log (Level .INFO , "--------------------------------------------------------------" );
290+ tx .commit ();
291+ } finally {
292+ if (tx .isActive ()) {
293+ tx .rollback ();
294+ }
295+
296+ pm .close ();
297+ }
298+ }
143299}
0 commit comments