Skip to content

Commit 303db6c

Browse files
shaimaa-hshalabyzhendrikse
authored andcommitted
Intro to JDO Queries 2/2 article (eugenp#2871)
* ADD MyApp.java class ProductItem.java class modify: package.jdo to add a named query pom.xml to add datanucleus-jdo-query.jar * remove sys.out statements
1 parent 6d0d878 commit 303db6c

4 files changed

Lines changed: 214 additions & 22 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<parent>
55
<artifactId>parent-modules</artifactId>
66
<groupId>com.baeldung</groupId>
@@ -105,6 +105,13 @@
105105
</executions>
106106
</plugin>
107107
<!-- /Neuroph -->
108+
<plugin>
109+
<artifactId>maven-compiler-plugin</artifactId>
110+
<configuration>
111+
<source>1.8</source>
112+
<target>1.8</target>
113+
</configuration>
114+
</plugin>
108115
</plugins>
109116
</build>
110117
<dependencies>
@@ -326,6 +333,11 @@
326333
<artifactId>datanucleus-xml</artifactId>
327334
<version>5.0.0-release</version>
328335
</dependency>
336+
<dependency>
337+
<groupId>org.datanucleus</groupId>
338+
<artifactId>datanucleus-jdo-query</artifactId>
339+
<version>5.0.2</version>
340+
</dependency>
329341
<dependency>
330342
<groupId>net.openhft</groupId>
331343
<artifactId>chronicle</artifactId>
@@ -546,29 +558,29 @@
546558
<version>${protonpack.version}</version>
547559
</dependency>
548560
<dependency>
549-
<groupId>org.functionaljava</groupId>
550-
<artifactId>functionaljava</artifactId>
551-
<version>4.7</version>
552-
</dependency>
553-
<dependency>
554-
<groupId>org.functionaljava</groupId>
555-
<artifactId>functionaljava-java8</artifactId>
556-
<version>4.7</version>
561+
<groupId>org.functionaljava</groupId>
562+
<artifactId>functionaljava</artifactId>
563+
<version>4.7</version>
564+
</dependency>
565+
<dependency>
566+
<groupId>org.functionaljava</groupId>
567+
<artifactId>functionaljava-java8</artifactId>
568+
<version>4.7</version>
569+
</dependency>
570+
<dependency>
571+
<groupId>org.functionaljava</groupId>
572+
<artifactId>functionaljava-quickcheck</artifactId>
573+
<version>4.7</version>
557574
</dependency>
558575
<dependency>
559-
<groupId>org.functionaljava</groupId>
560-
<artifactId>functionaljava-quickcheck</artifactId>
561-
<version>4.7</version>
562-
</dependency>
563-
<dependency>
564-
<groupId>org.functionaljava</groupId>
565-
<artifactId>functionaljava-java-core</artifactId>
566-
<version>4.7</version>
576+
<groupId>org.functionaljava</groupId>
577+
<artifactId>functionaljava-java-core</artifactId>
578+
<version>4.7</version>
567579
</dependency>
568580
<dependency>
569-
<groupId>javax.cache</groupId>
570-
<artifactId>cache-api</artifactId>
571-
<version>${cache.version}</version>
581+
<groupId>javax.cache</groupId>
582+
<artifactId>cache-api</artifactId>
583+
<version>${cache.version}</version>
572584
</dependency>
573585
<dependency>
574586
<groupId>com.hazelcast</groupId>
@@ -581,7 +593,7 @@
581593
<version>1.0.1</version>
582594
</dependency>
583595
<dependency>
584-
<groupId>com.netopyr.wurmloch</groupId>
596+
<groupId>com.netopyr.wurmloch</groupId>
585597
<artifactId>wurmloch-crdt</artifactId>
586598
<version>${crdt.version}</version>
587599
</dependency>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.jdo.query;
2+
3+
import javax.jdo.annotations.IdGeneratorStrategy;
4+
import javax.jdo.annotations.PersistenceAware;
5+
import javax.jdo.annotations.PersistenceCapable;
6+
import javax.jdo.annotations.Persistent;
7+
import javax.jdo.annotations.PrimaryKey;
8+
9+
@PersistenceCapable(table = "product_item")
10+
public class ProductItem {
11+
12+
@PrimaryKey
13+
@Persistent(valueStrategy=IdGeneratorStrategy.INCREMENT)
14+
int id;
15+
String name;
16+
String description;
17+
String status;
18+
double price;
19+
20+
public ProductItem(){
21+
22+
}
23+
24+
public ProductItem(String name,String description,String status,double price){
25+
this.name=name;
26+
this.description = description;
27+
this.status = status;
28+
this.price = price;
29+
}
30+
31+
32+
public int getId() {
33+
return id;
34+
}
35+
36+
public void setId(int id) {
37+
this.id = id;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
public String getDescription() {
47+
return description;
48+
}
49+
public void setDescription(String description) {
50+
this.description = description;
51+
}
52+
public double getPrice() {
53+
return price;
54+
}
55+
public void setPrice(double price) {
56+
this.price = price;
57+
}
58+
59+
public String getStatus() {
60+
return status;
61+
}
62+
63+
public void setStatus(String status) {
64+
this.status = status;
65+
}
66+
67+
68+
}

‎libraries/src/main/resources/META-INF/package.jdo‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
</field>
1717
</class>
1818
</package>
19-
19+
<package name="com.baeldung.jdo.query">
20+
<class name="ProductItem" detachable="true" table="product_item">
21+
22+
<query name="PriceBelow10" language="javax.jdo.query.SQL">
23+
<![CDATA[SELECT * FROM PRODUCT_ITEM WHERE PRICE < 10
24+
]]></query>
25+
26+
</class>
27+
</package>
2028

2129
</jdo>

0 commit comments

Comments
 (0)