|
| 1 | +package com.baeldung.jdo.query; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import javax.jdo.JDOQLTypedQuery; |
| 6 | +import javax.jdo.PersistenceManager; |
| 7 | +import javax.jdo.PersistenceManagerFactory; |
| 8 | +import javax.jdo.Query; |
| 9 | + |
| 10 | +import org.datanucleus.api.jdo.JDOPersistenceManagerFactory; |
| 11 | +import org.datanucleus.metadata.PersistenceUnitMetaData; |
| 12 | + |
| 13 | +public class MyApp { |
| 14 | + |
| 15 | + private static PersistenceManagerFactory pmf; |
| 16 | + private static PersistenceManager pm; |
| 17 | + |
| 18 | + public static void main(String[] args) { |
| 19 | + |
| 20 | + defineDynamicPersistentUnit(); |
| 21 | + createTestData(); |
| 22 | + queryUsingJDOQL(); |
| 23 | + queryUsingTypedJDOQL(); |
| 24 | + queryUsingSQL(); |
| 25 | + queryUsingJPQL(); |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + public static void createTestData(){ |
| 30 | + ProductItem item1 = new ProductItem("supportedItem", "price less than 10", "SoldOut",5); |
| 31 | + ProductItem item2 = new ProductItem("pro2", "price less than 10","InStock", 8); |
| 32 | + ProductItem item3 = new ProductItem("pro3", "price more than 10","SoldOut", 15); |
| 33 | + |
| 34 | + if( pm != null ){ |
| 35 | + pm.makePersistent(item1); |
| 36 | + pm.makePersistent(item2); |
| 37 | + pm.makePersistent(item3); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public static void defineDynamicPersistentUnit(){ |
| 42 | + |
| 43 | + PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null); |
| 44 | + pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:mysql://localhost:3306/jdo_db"); |
| 45 | + pumd.addProperty("javax.jdo.option.ConnectionUserName", "root"); |
| 46 | + pumd.addProperty("javax.jdo.option.ConnectionPassword", "admin"); |
| 47 | + pumd.addProperty("javax.jdo.option.ConnectionDriverName", "com.mysql.jdbc.Driver"); |
| 48 | + pumd.addProperty("datanucleus.schema.autoCreateAll", "true"); |
| 49 | + |
| 50 | + pmf = new JDOPersistenceManagerFactory(pumd, null); |
| 51 | + pm = pmf.getPersistenceManager(); |
| 52 | + } |
| 53 | + |
| 54 | + public static void queryUsingJDOQL(){ |
| 55 | + |
| 56 | + Query query = pm.newQuery("SELECT FROM com.baeldung.jdo.query.ProductItem " |
| 57 | + + "WHERE price < threshold PARAMETERS double threshold"); |
| 58 | + List<ProductItem> explicitParamResults = (List<ProductItem>)query.execute(10); |
| 59 | + |
| 60 | + query = pm.newQuery("SELECT FROM " |
| 61 | + + "com.baeldung.jdo.query.ProductItem WHERE price < :threshold"); |
| 62 | + query.setParameters("double threshold"); |
| 63 | + List<ProductItem> explicitParamResults2 = (List<ProductItem>)query.execute(10); |
| 64 | + |
| 65 | + query = pm.newQuery("SELECT FROM " |
| 66 | + + "com.baeldung.jdo.query.ProductItem WHERE price < :threshold"); |
| 67 | + List<ProductItem> implicitParamResults = (List<ProductItem>)query.execute(10); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + public static void queryUsingTypedJDOQL(){ |
| 72 | + |
| 73 | + JDOQLTypedQuery<ProductItem> tq = pm.newJDOQLTypedQuery(ProductItem.class); |
| 74 | + QProductItem cand = QProductItem.candidate(); |
| 75 | + tq=tq.filter(cand.price.lt(10).and(cand.name.startsWith("pro"))); |
| 76 | + List<ProductItem> results = tq.executeList(); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + public static void queryUsingSQL(){ |
| 81 | + |
| 82 | + Query query = pm.newQuery("javax.jdo.query.SQL","select * from " |
| 83 | + + "product_item where price < ? and status = ?"); |
| 84 | + query.setClass(ProductItem.class); |
| 85 | + query.setParameters(10,"InStock"); |
| 86 | + List<ProductItem> results = query.executeList(); |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + public static void queryUsingJPQL(){ |
| 91 | + Query query = pm.newQuery("JPQL","select i from " |
| 92 | + + "com.baeldung.jdo.query.ProductItem i where i.price < 10" |
| 93 | + + " and i.status = 'InStock'"); |
| 94 | + List<ProductItem> results = (List<ProductItem>) query.execute(); |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + public static void namedQuery(){ |
| 99 | + Query<ProductItem> query = pm.newNamedQuery( |
| 100 | + ProductItem.class, "PriceBelow10"); |
| 101 | + List<ProductItem> results = query.executeList(); |
| 102 | + |
| 103 | + } |
| 104 | +} |
0 commit comments