Intro to JDO Queries 2/2 article (#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
This commit is contained in:
parent
6d0d878ba4
commit
303db6c663
@ -105,6 +105,13 @@
|
|||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<!-- /Neuroph -->
|
<!-- /Neuroph -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -326,6 +333,11 @@
|
|||||||
<artifactId>datanucleus-xml</artifactId>
|
<artifactId>datanucleus-xml</artifactId>
|
||||||
<version>5.0.0-release</version>
|
<version>5.0.0-release</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.datanucleus</groupId>
|
||||||
|
<artifactId>datanucleus-jdo-query</artifactId>
|
||||||
|
<version>5.0.2</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.openhft</groupId>
|
<groupId>net.openhft</groupId>
|
||||||
<artifactId>chronicle</artifactId>
|
<artifactId>chronicle</artifactId>
|
||||||
|
104
libraries/src/main/java/com/baeldung/jdo/query/MyApp.java
Normal file
104
libraries/src/main/java/com/baeldung/jdo/query/MyApp.java
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
package com.baeldung.jdo.query;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.jdo.JDOQLTypedQuery;
|
||||||
|
import javax.jdo.PersistenceManager;
|
||||||
|
import javax.jdo.PersistenceManagerFactory;
|
||||||
|
import javax.jdo.Query;
|
||||||
|
|
||||||
|
import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
|
||||||
|
import org.datanucleus.metadata.PersistenceUnitMetaData;
|
||||||
|
|
||||||
|
public class MyApp {
|
||||||
|
|
||||||
|
private static PersistenceManagerFactory pmf;
|
||||||
|
private static PersistenceManager pm;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
defineDynamicPersistentUnit();
|
||||||
|
createTestData();
|
||||||
|
queryUsingJDOQL();
|
||||||
|
queryUsingTypedJDOQL();
|
||||||
|
queryUsingSQL();
|
||||||
|
queryUsingJPQL();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void createTestData(){
|
||||||
|
ProductItem item1 = new ProductItem("supportedItem", "price less than 10", "SoldOut",5);
|
||||||
|
ProductItem item2 = new ProductItem("pro2", "price less than 10","InStock", 8);
|
||||||
|
ProductItem item3 = new ProductItem("pro3", "price more than 10","SoldOut", 15);
|
||||||
|
|
||||||
|
if( pm != null ){
|
||||||
|
pm.makePersistent(item1);
|
||||||
|
pm.makePersistent(item2);
|
||||||
|
pm.makePersistent(item3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void defineDynamicPersistentUnit(){
|
||||||
|
|
||||||
|
PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:mysql://localhost:3306/jdo_db");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionUserName", "root");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionPassword", "admin");
|
||||||
|
pumd.addProperty("javax.jdo.option.ConnectionDriverName", "com.mysql.jdbc.Driver");
|
||||||
|
pumd.addProperty("datanucleus.schema.autoCreateAll", "true");
|
||||||
|
|
||||||
|
pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||||
|
pm = pmf.getPersistenceManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void queryUsingJDOQL(){
|
||||||
|
|
||||||
|
Query query = pm.newQuery("SELECT FROM com.baeldung.jdo.query.ProductItem "
|
||||||
|
+ "WHERE price < threshold PARAMETERS double threshold");
|
||||||
|
List<ProductItem> explicitParamResults = (List<ProductItem>)query.execute(10);
|
||||||
|
|
||||||
|
query = pm.newQuery("SELECT FROM "
|
||||||
|
+ "com.baeldung.jdo.query.ProductItem WHERE price < :threshold");
|
||||||
|
query.setParameters("double threshold");
|
||||||
|
List<ProductItem> explicitParamResults2 = (List<ProductItem>)query.execute(10);
|
||||||
|
|
||||||
|
query = pm.newQuery("SELECT FROM "
|
||||||
|
+ "com.baeldung.jdo.query.ProductItem WHERE price < :threshold");
|
||||||
|
List<ProductItem> implicitParamResults = (List<ProductItem>)query.execute(10);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void queryUsingTypedJDOQL(){
|
||||||
|
|
||||||
|
JDOQLTypedQuery<ProductItem> tq = pm.newJDOQLTypedQuery(ProductItem.class);
|
||||||
|
QProductItem cand = QProductItem.candidate();
|
||||||
|
tq=tq.filter(cand.price.lt(10).and(cand.name.startsWith("pro")));
|
||||||
|
List<ProductItem> results = tq.executeList();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void queryUsingSQL(){
|
||||||
|
|
||||||
|
Query query = pm.newQuery("javax.jdo.query.SQL","select * from "
|
||||||
|
+ "product_item where price < ? and status = ?");
|
||||||
|
query.setClass(ProductItem.class);
|
||||||
|
query.setParameters(10,"InStock");
|
||||||
|
List<ProductItem> results = query.executeList();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void queryUsingJPQL(){
|
||||||
|
Query query = pm.newQuery("JPQL","select i from "
|
||||||
|
+ "com.baeldung.jdo.query.ProductItem i where i.price < 10"
|
||||||
|
+ " and i.status = 'InStock'");
|
||||||
|
List<ProductItem> results = (List<ProductItem>) query.execute();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void namedQuery(){
|
||||||
|
Query<ProductItem> query = pm.newNamedQuery(
|
||||||
|
ProductItem.class, "PriceBelow10");
|
||||||
|
List<ProductItem> results = query.executeList();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.baeldung.jdo.query;
|
||||||
|
|
||||||
|
import javax.jdo.annotations.IdGeneratorStrategy;
|
||||||
|
import javax.jdo.annotations.PersistenceAware;
|
||||||
|
import javax.jdo.annotations.PersistenceCapable;
|
||||||
|
import javax.jdo.annotations.Persistent;
|
||||||
|
import javax.jdo.annotations.PrimaryKey;
|
||||||
|
|
||||||
|
@PersistenceCapable(table = "product_item")
|
||||||
|
public class ProductItem {
|
||||||
|
|
||||||
|
@PrimaryKey
|
||||||
|
@Persistent(valueStrategy=IdGeneratorStrategy.INCREMENT)
|
||||||
|
int id;
|
||||||
|
String name;
|
||||||
|
String description;
|
||||||
|
String status;
|
||||||
|
double price;
|
||||||
|
|
||||||
|
public ProductItem(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProductItem(String name,String description,String status,double price){
|
||||||
|
this.name=name;
|
||||||
|
this.description = description;
|
||||||
|
this.status = status;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStatus(String status) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -16,6 +16,14 @@
|
|||||||
</field>
|
</field>
|
||||||
</class>
|
</class>
|
||||||
</package>
|
</package>
|
||||||
|
<package name="com.baeldung.jdo.query">
|
||||||
|
<class name="ProductItem" detachable="true" table="product_item">
|
||||||
|
|
||||||
|
<query name="PriceBelow10" language="javax.jdo.query.SQL">
|
||||||
|
<![CDATA[SELECT * FROM PRODUCT_ITEM WHERE PRICE < 10
|
||||||
|
]]></query>
|
||||||
|
|
||||||
|
</class>
|
||||||
|
</package>
|
||||||
|
|
||||||
</jdo>
|
</jdo>
|
Loading…
x
Reference in New Issue
Block a user