uncomment classes in core-java (#1777)
* uncomment classes in core-java * Add content to guide to JDO
This commit is contained in:
parent
5f02c64c95
commit
3e50b39fed
@ -1,6 +1,4 @@
|
||||
package com.baeldung.java8.comparator;
|
||||
/*
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
@ -22,4 +20,3 @@ public class Employee implements Comparable<Employee>{
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
@ -1,5 +1,4 @@
|
||||
package com.baeldung.java8.comparator;
|
||||
/*
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
@ -164,5 +163,3 @@ public class Java8ComparatorTest {
|
||||
|
||||
|
||||
}
|
||||
|
||||
*/
|
26
libraries/myPersistence.xml
Normal file
26
libraries/myPersistence.xml
Normal file
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?><root>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</root>
|
@ -251,6 +251,11 @@
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.194</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.datanucleus</groupId>
|
||||
<artifactId>datanucleus-xml</artifactId>
|
||||
<version>5.0.0-release</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
@ -20,22 +20,29 @@ public class GuideToJDO {
|
||||
private static final Logger LOGGER = Logger.getLogger(GuideToJDO.class.getName());
|
||||
private Random rnd = new Random();
|
||||
private PersistenceUnitMetaData pumd;
|
||||
private PersistenceUnitMetaData pumdXML;
|
||||
|
||||
public static void main(String[] args) {
|
||||
new GuideToJDO();
|
||||
}
|
||||
|
||||
public GuideToJDO() {
|
||||
CreateProperties();
|
||||
CreateH2Properties();
|
||||
CreateXMLProperties();
|
||||
CreateProducts();
|
||||
ListProducts();
|
||||
QueryJDOQL();
|
||||
QuerySQL();
|
||||
QueryJPQL();
|
||||
UpdateProducts();
|
||||
ListProducts();
|
||||
DeleteProducts();
|
||||
ListProducts();
|
||||
persistXML();
|
||||
listXMLProducts();
|
||||
}
|
||||
|
||||
public void CreateProperties(){
|
||||
public void CreateH2Properties(){
|
||||
|
||||
pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
|
||||
pumd.addClassName("com.baeldung.jdo.Product");
|
||||
@ -48,6 +55,14 @@ public class GuideToJDO {
|
||||
|
||||
}
|
||||
|
||||
public void CreateXMLProperties(){
|
||||
pumdXML = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
|
||||
pumdXML.addClassName("com.baeldung.jdo.ProductXML");
|
||||
pumdXML.setExcludeUnlistedClasses();
|
||||
pumdXML.addProperty("javax.jdo.option.ConnectionURL", "xml:file:myPersistence.xml");
|
||||
pumdXML.addProperty("datanucleus.autoCreateSchema", "true");
|
||||
}
|
||||
|
||||
public void CreateProducts() {
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
@ -140,4 +155,145 @@ public class GuideToJDO {
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void QueryJDOQL (){
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
|
||||
// Declarative JDOQL :
|
||||
LOGGER.log(Level.INFO, "Declarative JDOQL --------------------------------------------------------------");
|
||||
Query qDJDOQL = pm.newQuery(Product.class);
|
||||
qDJDOQL.setFilter("name == 'Tablet' && price == price_value");
|
||||
qDJDOQL.declareParameters("double price_value");
|
||||
List<Product> resultsqDJDOQL = qDJDOQL.setParameters(80.0).executeList();
|
||||
|
||||
Iterator<Product> iterDJDOQL = resultsqDJDOQL.iterator();
|
||||
while (iterDJDOQL.hasNext()) {
|
||||
Product p = iterDJDOQL.next();
|
||||
LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
|
||||
}
|
||||
LOGGER.log(Level.INFO, "--------------------------------------------------------------");
|
||||
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void QuerySQL (){
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
|
||||
//SQL :
|
||||
LOGGER.log(Level.INFO, "SQL --------------------------------------------------------------");
|
||||
Query query = pm.newQuery("javax.jdo.query.SQL", "SELECT * FROM PRODUCT");
|
||||
query.setClass(Product.class);
|
||||
List<Product> results = query.executeList();
|
||||
|
||||
Iterator<Product> iter = results.iterator();
|
||||
while (iter.hasNext()) {
|
||||
Product p = iter.next();
|
||||
LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
|
||||
}
|
||||
LOGGER.log(Level.INFO, "--------------------------------------------------------------");
|
||||
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void QueryJPQL (){
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
|
||||
//JPQL :
|
||||
LOGGER.log(Level.INFO, "JPQL --------------------------------------------------------------");
|
||||
Query q = pm.newQuery("JPQL", "SELECT p FROM "+Product.class.getName()+" p WHERE p.name = 'Laptop'");
|
||||
List results = (List)q.execute();
|
||||
|
||||
Iterator<Product> iter = results.iterator();
|
||||
while (iter.hasNext()) {
|
||||
Product p = iter.next();
|
||||
LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
|
||||
}
|
||||
LOGGER.log(Level.INFO, "--------------------------------------------------------------");
|
||||
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void persistXML(){
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumdXML, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
ProductXML productXML = new ProductXML(0,"Tablet", 80.0);
|
||||
pm.makePersistent(productXML);
|
||||
ProductXML productXML2 = new ProductXML(1,"Phone", 20.0);
|
||||
pm.makePersistent(productXML2);
|
||||
ProductXML productXML3 = new ProductXML(2,"Laptop", 200.0);
|
||||
pm.makePersistent(productXML3);
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void listXMLProducts(){
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumdXML, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
|
||||
Query q = pm.newQuery("SELECT FROM " + ProductXML.class.getName());
|
||||
List<ProductXML> products = (List<ProductXML>) q.execute();
|
||||
Iterator<ProductXML> iter = products.iterator();
|
||||
while (iter.hasNext()) {
|
||||
ProductXML p = iter.next();
|
||||
LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.getName(), p.getPrice() });
|
||||
pm.deletePersistent(p);
|
||||
}
|
||||
LOGGER.log(Level.INFO, "--------------------------------------------------------------");
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
}
|
44
libraries/src/main/java/com/baeldung/jdo/ProductXML.java
Normal file
44
libraries/src/main/java/com/baeldung/jdo/ProductXML.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.baeldung.jdo;
|
||||
|
||||
import javax.jdo.annotations.PersistenceCapable;
|
||||
import javax.jdo.annotations.PrimaryKey;
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
|
||||
@PersistenceCapable()
|
||||
public class ProductXML {
|
||||
|
||||
@XmlAttribute
|
||||
private long productNumber = 0;
|
||||
@PrimaryKey
|
||||
private String name = null;
|
||||
private Double price = 0.0;
|
||||
|
||||
public ProductXML() {
|
||||
this.productNumber = 0;
|
||||
this.name = null;
|
||||
this.price = 0.0;
|
||||
}
|
||||
|
||||
public ProductXML(long productNumber, String name, Double price) {
|
||||
this.productNumber = productNumber;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user