BAEL-1734 move packages from libraries to libraries-data
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.hikaricp;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
public class DataSource {
|
||||
|
||||
private static HikariConfig config = new HikariConfig();
|
||||
private static HikariDataSource ds;
|
||||
|
||||
static {
|
||||
// config = new HikariConfig("datasource.properties");
|
||||
|
||||
// Properties props = new Properties();
|
||||
// props.setProperty("dataSourceClassName", "org.h2.Driver");
|
||||
// props.setProperty("dataSource.user", "");
|
||||
// props.setProperty("dataSource.password", "");
|
||||
// props.put("dataSource.logWriter", new PrintWriter(System.out));
|
||||
// config = new HikariConfig(props);
|
||||
|
||||
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'");
|
||||
config.setUsername("");
|
||||
config.setPassword("");
|
||||
config.addDataSourceProperty("cachePrepStmts", "true");
|
||||
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||
ds = new HikariDataSource(config);
|
||||
|
||||
// ds.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'");
|
||||
// ds.setUsername("");
|
||||
// ds.setPassword("");
|
||||
}
|
||||
|
||||
private DataSource() {
|
||||
}
|
||||
|
||||
public static Connection getConnection() throws SQLException {
|
||||
return ds.getConnection();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package com.baeldung.hikaricp;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private int empNo;
|
||||
private String ename;
|
||||
private String job;
|
||||
private int mgr;
|
||||
private Date hiredate;
|
||||
private int sal;
|
||||
private int comm;
|
||||
private int deptno;
|
||||
|
||||
public int getEmpNo() {
|
||||
return empNo;
|
||||
}
|
||||
|
||||
public void setEmpNo(int empNo) {
|
||||
this.empNo = empNo;
|
||||
}
|
||||
|
||||
public String getEname() {
|
||||
return ename;
|
||||
}
|
||||
|
||||
public void setEname(String ename) {
|
||||
this.ename = ename;
|
||||
}
|
||||
|
||||
public String getJob() {
|
||||
return job;
|
||||
}
|
||||
|
||||
public void setJob(String job) {
|
||||
this.job = job;
|
||||
}
|
||||
|
||||
public int getMgr() {
|
||||
return mgr;
|
||||
}
|
||||
|
||||
public void setMgr(int mgr) {
|
||||
this.mgr = mgr;
|
||||
}
|
||||
|
||||
public Date getHiredate() {
|
||||
return hiredate;
|
||||
}
|
||||
|
||||
public void setHiredate(Date hiredate) {
|
||||
this.hiredate = hiredate;
|
||||
}
|
||||
|
||||
public int getSal() {
|
||||
return sal;
|
||||
}
|
||||
|
||||
public void setSal(int sal) {
|
||||
this.sal = sal;
|
||||
}
|
||||
|
||||
public int getComm() {
|
||||
return comm;
|
||||
}
|
||||
|
||||
public void setComm(int comm) {
|
||||
this.comm = comm;
|
||||
}
|
||||
|
||||
public int getDeptno() {
|
||||
return deptno;
|
||||
}
|
||||
|
||||
public void setDeptno(int deptno) {
|
||||
this.deptno = deptno;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Employee [empNo=%d, ename=%s, job=%s, mgr=%d, hiredate=%s, sal=%d, comm=%d, deptno=%d]", empNo, ename, job, mgr, hiredate, sal, comm, deptno);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.hikaricp;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class HikariCPDemo {
|
||||
|
||||
public static List<Employee> fetchData() {
|
||||
final String SQL_QUERY = "select * from emp";
|
||||
List<Employee> employees = null;
|
||||
try (Connection con = DataSource.getConnection(); PreparedStatement pst = con.prepareStatement(SQL_QUERY); ResultSet rs = pst.executeQuery();) {
|
||||
employees = new ArrayList<Employee>();
|
||||
Employee employee;
|
||||
while (rs.next()) {
|
||||
employee = new Employee();
|
||||
employee.setEmpNo(rs.getInt("empno"));
|
||||
employee.setEname(rs.getString("ename"));
|
||||
employee.setJob(rs.getString("job"));
|
||||
employee.setMgr(rs.getInt("mgr"));
|
||||
employee.setHiredate(rs.getDate("hiredate"));
|
||||
employee.setSal(rs.getInt("sal"));
|
||||
employee.setComm(rs.getInt("comm"));
|
||||
employee.setDeptno(rs.getInt("deptno"));
|
||||
employees.add(employee);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return employees;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
fetchData();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.baeldung.jcache;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.cache.event.CacheEntryCreatedListener;
|
||||
import javax.cache.event.CacheEntryEvent;
|
||||
import javax.cache.event.CacheEntryListenerException;
|
||||
import javax.cache.event.CacheEntryUpdatedListener;
|
||||
|
||||
public class SimpleCacheEntryListener implements CacheEntryCreatedListener<String, String>, CacheEntryUpdatedListener<String, String>, Serializable {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -712657810462878763L;
|
||||
private boolean updated;
|
||||
private boolean created;
|
||||
|
||||
public boolean getUpdated() {
|
||||
return this.updated;
|
||||
}
|
||||
|
||||
public boolean getCreated() {
|
||||
return this.created;
|
||||
}
|
||||
|
||||
public void onUpdated(Iterable<CacheEntryEvent<? extends String, ? extends String>> events) throws CacheEntryListenerException {
|
||||
this.updated = true;
|
||||
}
|
||||
|
||||
public void onCreated(Iterable<CacheEntryEvent<? extends String, ? extends String>> events) throws CacheEntryListenerException {
|
||||
this.created = true;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.jcache;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.cache.integration.CacheLoader;
|
||||
import javax.cache.integration.CacheLoaderException;
|
||||
|
||||
public class SimpleCacheLoader implements CacheLoader<Integer, String> {
|
||||
|
||||
@Override
|
||||
public String load(Integer key) throws CacheLoaderException {
|
||||
return "fromCache" + key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Integer, String> loadAll(Iterable<? extends Integer> keys) throws CacheLoaderException {
|
||||
Map<Integer, String> data = new HashMap<>();
|
||||
for (int key : keys) {
|
||||
data.put(key, load(key));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.jcache;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.cache.processor.EntryProcessor;
|
||||
import javax.cache.processor.EntryProcessorException;
|
||||
import javax.cache.processor.MutableEntry;
|
||||
|
||||
public class SimpleEntryProcessor implements EntryProcessor<String, String, String>, Serializable {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -5616476363722945132L;
|
||||
|
||||
public String process(MutableEntry<String, String> entry, Object... args) throws EntryProcessorException {
|
||||
|
||||
if (entry.exists()) {
|
||||
String current = entry.getValue();
|
||||
entry.setValue(current + " - modified");
|
||||
return current;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,299 +0,0 @@
|
||||
package com.baeldung.jdo;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.jdo.PersistenceManager;
|
||||
import javax.jdo.PersistenceManagerFactory;
|
||||
import javax.jdo.Query;
|
||||
import javax.jdo.Transaction;
|
||||
|
||||
import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
|
||||
import org.datanucleus.metadata.PersistenceUnitMetaData;
|
||||
|
||||
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() {
|
||||
CreateH2Properties();
|
||||
CreateXMLProperties();
|
||||
CreateProducts();
|
||||
ListProducts();
|
||||
QueryJDOQL();
|
||||
QuerySQL();
|
||||
QueryJPQL();
|
||||
UpdateProducts();
|
||||
ListProducts();
|
||||
DeleteProducts();
|
||||
ListProducts();
|
||||
persistXML();
|
||||
listXMLProducts();
|
||||
}
|
||||
|
||||
public void CreateH2Properties() {
|
||||
|
||||
pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
|
||||
pumd.addClassName("com.baeldung.jdo.Product");
|
||||
pumd.setExcludeUnlistedClasses();
|
||||
pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
|
||||
pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
|
||||
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
|
||||
pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
|
||||
pumd.addProperty("datanucleus.autoCreateSchema", "true");
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
Product product = new Product("Tablet", 80.0);
|
||||
pm.makePersistent(product);
|
||||
Product product2 = new Product("Phone", 20.0);
|
||||
pm.makePersistent(product2);
|
||||
Product product3 = new Product("Laptop", 200.0);
|
||||
pm.makePersistent(product3);
|
||||
for (int i = 0; i < 100; i++) {
|
||||
String nam = "Product-" + i;
|
||||
double price = rnd.nextDouble();
|
||||
Product productx = new Product(nam, price);
|
||||
pm.makePersistent(productx);
|
||||
}
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void UpdateProducts() {
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
Query query = pm.newQuery(Product.class, "name == \"Phone\"");
|
||||
Collection result = (Collection) query.execute();
|
||||
Product product = (Product) result.iterator().next();
|
||||
product.setName("Android Phone");
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void DeleteProducts() {
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
Query query = pm.newQuery(Product.class, "name == \"Android Phone\"");
|
||||
Collection result = (Collection) query.execute();
|
||||
Product product = (Product) result.iterator().next();
|
||||
pm.deletePersistent(product);
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void ListProducts() {
|
||||
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
PersistenceManager pm = pmf.getPersistenceManager();
|
||||
Transaction tx = pm.currentTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
|
||||
Query q = pm.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price > 10");
|
||||
List<Product> products = (List<Product>) q.execute();
|
||||
Iterator<Product> iter = products.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 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.baeldung.jdo;
|
||||
|
||||
import javax.jdo.annotations.IdGeneratorStrategy;
|
||||
import javax.jdo.annotations.PersistenceCapable;
|
||||
import javax.jdo.annotations.Persistent;
|
||||
import javax.jdo.annotations.PrimaryKey;
|
||||
|
||||
@PersistenceCapable
|
||||
public class Product {
|
||||
|
||||
@PrimaryKey
|
||||
@Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
|
||||
long id;
|
||||
String name = null;
|
||||
Double price = 0.0;
|
||||
|
||||
public Product() {
|
||||
this.name = null;
|
||||
this.price = 0.0;
|
||||
}
|
||||
|
||||
public Product(String name, Double price) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
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();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
package com.baeldung.jdo.query;
|
||||
|
||||
import javax.jdo.annotations.IdGeneratorStrategy;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package com.baeldung.jdo.xml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.jdo.annotations.Element;
|
||||
import javax.jdo.annotations.PersistenceCapable;
|
||||
import javax.jdo.annotations.PrimaryKey;
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||
|
||||
@PersistenceCapable(schema = "/myproduct/people", table = "person")
|
||||
public class AnnotadedPerson {
|
||||
@XmlAttribute
|
||||
private long personNum;
|
||||
|
||||
@PrimaryKey
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
@XmlElementWrapper(name = "phone-numbers")
|
||||
@XmlElement(name = "phone-number")
|
||||
@Element(types = String.class)
|
||||
private List phoneNumbers = new ArrayList();
|
||||
|
||||
public AnnotadedPerson(long personNum, String firstName, String lastName) {
|
||||
super();
|
||||
this.personNum = personNum;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public long getPersonNum() {
|
||||
return personNum;
|
||||
}
|
||||
|
||||
public void setPersonNum(long personNum) {
|
||||
this.personNum = personNum;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public List getPhoneNumbers() {
|
||||
return phoneNumbers;
|
||||
}
|
||||
|
||||
public void setPhoneNumbers(List phoneNumbers) {
|
||||
this.phoneNumbers = phoneNumbers;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
package com.baeldung.jdo.xml;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.jdo.JDOHelper;
|
||||
import javax.jdo.PersistenceManager;
|
||||
import javax.jdo.PersistenceManagerFactory;
|
||||
import javax.jdo.Query;
|
||||
import javax.jdo.Transaction;
|
||||
|
||||
import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
|
||||
import org.datanucleus.metadata.PersistenceUnitMetaData;
|
||||
|
||||
public class MyApp {
|
||||
|
||||
private static PersistenceUnitMetaData pumd;
|
||||
private static PersistenceManagerFactory pmf;
|
||||
private static PersistenceManager pm;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// persist product object using dynamic persistence unit
|
||||
defineDynamicPersistentUnit();
|
||||
Product product = new Product("id1", "Sony Discman", "A standard discman from Sony", 49.99);
|
||||
persistObject(product);
|
||||
closePersistenceManager();
|
||||
|
||||
// persist AnnotatedPerson object using named pmf
|
||||
defineNamedPersistenceManagerFactory("XmlDatastore");
|
||||
AnnotadedPerson annotatedPerson = new AnnotadedPerson(654320, "annotated", "person");
|
||||
annotatedPerson.getPhoneNumbers().add("999999999");
|
||||
annotatedPerson.getPhoneNumbers().add("000000000");
|
||||
persistObject(annotatedPerson);
|
||||
queryAnnotatedPersonsInXML();
|
||||
closePersistenceManager();
|
||||
|
||||
// persist Person object using PMF created by properties file
|
||||
definePersistenceManagerFactoryUsingPropertiesFile("META-INF\\datanucleus.properties");
|
||||
Person person = new Person(654321, "bealdung", "author");
|
||||
person.getPhoneNumbers().add("123456789");
|
||||
person.getPhoneNumbers().add("987654321");
|
||||
persistObject(person);
|
||||
queryPersonsInXML();
|
||||
closePersistenceManager();
|
||||
}
|
||||
|
||||
public static void defineDynamicPersistentUnit() {
|
||||
|
||||
PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
|
||||
pumd.addProperty("javax.jdo.option.ConnectionURL", "xml:file:myfile_dynamicPMF.xml");
|
||||
pumd.addProperty("datanucleus.schema.autoCreateAll", "true");
|
||||
pumd.addProperty("datanucleus.xml.indentSize", "4");
|
||||
|
||||
pmf = new JDOPersistenceManagerFactory(pumd, null);
|
||||
pm = pmf.getPersistenceManager();
|
||||
}
|
||||
|
||||
public static void defineNamedPersistenceManagerFactory(String pmfName) {
|
||||
|
||||
pmf = JDOHelper.getPersistenceManagerFactory("XmlDatastore");
|
||||
pm = pmf.getPersistenceManager();
|
||||
}
|
||||
|
||||
public static void definePersistenceManagerFactoryUsingPropertiesFile(String filePath) {
|
||||
|
||||
pmf = JDOHelper.getPersistenceManagerFactory(filePath);
|
||||
pm = pmf.getPersistenceManager();
|
||||
}
|
||||
|
||||
public static void closePersistenceManager() {
|
||||
|
||||
if (pm != null && !pm.isClosed()) {
|
||||
pm.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void persistObject(Object obj) {
|
||||
|
||||
Transaction tx = pm.currentTransaction();
|
||||
|
||||
try {
|
||||
tx.begin();
|
||||
pm.makePersistent(obj);
|
||||
tx.commit();
|
||||
} finally {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void queryPersonsInXML() {
|
||||
|
||||
Query<Person> query = pm.newQuery(Person.class);
|
||||
List<Person> result = query.executeList();
|
||||
System.out.println("name: " + result.get(0).getFirstName());
|
||||
}
|
||||
|
||||
public static void queryAnnotatedPersonsInXML() {
|
||||
|
||||
Query<AnnotadedPerson> query = pm.newQuery(AnnotadedPerson.class);
|
||||
List<AnnotadedPerson> result = query.executeList();
|
||||
System.out.println("name: " + result.get(0).getFirstName());
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.baeldung.jdo.xml;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.jdo.annotations.PersistenceCapable;
|
||||
import javax.jdo.annotations.PrimaryKey;
|
||||
|
||||
@PersistenceCapable
|
||||
public class Person {
|
||||
private long personNum;
|
||||
|
||||
@PrimaryKey
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
private List phoneNumbers = new ArrayList();
|
||||
|
||||
public Person(long personNum, String firstName, String lastName) {
|
||||
super();
|
||||
this.personNum = personNum;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public long getPersonNum() {
|
||||
return personNum;
|
||||
}
|
||||
|
||||
public void setPersonNum(long personNum) {
|
||||
this.personNum = personNum;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public List getPhoneNumbers() {
|
||||
return phoneNumbers;
|
||||
}
|
||||
|
||||
public void setPhoneNumbers(List phoneNumbers) {
|
||||
this.phoneNumbers = phoneNumbers;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.baeldung.jdo.xml;
|
||||
|
||||
import javax.jdo.annotations.PersistenceCapable;
|
||||
import javax.jdo.annotations.PrimaryKey;
|
||||
|
||||
@PersistenceCapable
|
||||
public class Product {
|
||||
|
||||
@PrimaryKey
|
||||
String id;
|
||||
String name;
|
||||
String description;
|
||||
double price;
|
||||
|
||||
public Product() {
|
||||
|
||||
}
|
||||
|
||||
public Product(String id, String name, String description, double price) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<jdoconfig xmlns="http://xmlns.jcp.org/xml/ns/jdo/jdoconfig"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/jdo/jdoconfig
|
||||
http://xmlns.jcp.org/xml/ns/jdo/jdoconfig_3_2.xsd"
|
||||
version="3.2">
|
||||
|
||||
<!-- Datastore Txn PMF -->
|
||||
<persistence-manager-factory name="XmlDatastore">
|
||||
<property name="javax.jdo.PersistenceManagerFactoryClass"
|
||||
value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" />
|
||||
<property name="javax.jdo.option.ConnectionURL" value="xml:file:namedPMF-ds.xml" />
|
||||
<property name="datanucleus.xml.indentSize" value="6" />
|
||||
<property name="datanucleus.schema.autoCreateAll"
|
||||
value="true" />
|
||||
</persistence-manager-factory>
|
||||
</jdoconfig>
|
||||
@@ -1,29 +0,0 @@
|
||||
<jdo xmlns="http://xmlns.jcp.org/xml/ns/jdo/jdo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/jdo/jdo http://xmlns.jcp.org/xml/ns/jdo/jdo_3_1.xsd"
|
||||
version="3.1">
|
||||
<package name="com.baeldung.jdo.xml">
|
||||
<class name="Person" detachable="true" schema="/myproduct/people"
|
||||
table="person">
|
||||
<field name="personNum">
|
||||
<extension vendor-name="datanucleus" key="XmlAttribute"
|
||||
value="true" />
|
||||
</field>
|
||||
<field name="firstName" primary-key="true" /> <!-- PK since JAXB requires String -->
|
||||
<field name="lastName" />
|
||||
<field name="phoneNumbers">
|
||||
<collection element-type="java.lang.String" />
|
||||
<element column="phoneNumber" />
|
||||
</field>
|
||||
</class>
|
||||
</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>
|
||||
@@ -1,47 +0,0 @@
|
||||
create table dept(
|
||||
deptno numeric,
|
||||
dname varchar(14),
|
||||
loc varchar(13),
|
||||
constraint pk_dept primary key (deptno)
|
||||
);
|
||||
|
||||
create table emp(
|
||||
empno numeric,
|
||||
ename varchar(10),
|
||||
job varchar(9),
|
||||
mgr numeric,
|
||||
hiredate date,
|
||||
sal numeric,
|
||||
comm numeric,
|
||||
deptno numeric,
|
||||
constraint pk_emp primary key (empno),
|
||||
constraint fk_deptno foreign key (deptno) references dept (deptno)
|
||||
);
|
||||
|
||||
insert into dept values(10, 'ACCOUNTING', 'NEW YORK');
|
||||
insert into dept values(20, 'RESEARCH', 'DALLAS');
|
||||
insert into dept values(30, 'SALES', 'CHICAGO');
|
||||
insert into dept values(40, 'OPERATIONS', 'BOSTON');
|
||||
|
||||
insert into emp values(
|
||||
7839, 'KING', 'PRESIDENT', null,
|
||||
to_date('17-11-1981','dd-mm-yyyy'),
|
||||
7698, null, 10
|
||||
);
|
||||
insert into emp values(
|
||||
7698, 'BLAKE', 'MANAGER', 7839,
|
||||
to_date('1-5-1981','dd-mm-yyyy'),
|
||||
7782, null, 20
|
||||
);
|
||||
insert into emp values(
|
||||
7782, 'CLARK', 'MANAGER', 7839,
|
||||
to_date('9-6-1981','dd-mm-yyyy'),
|
||||
7566, null, 30
|
||||
);
|
||||
insert into emp values(
|
||||
7566, 'JONES', 'MANAGER', 7839,
|
||||
to_date('2-4-1981','dd-mm-yyyy'),
|
||||
7839, null, 40
|
||||
);
|
||||
|
||||
commit;
|
||||
Reference in New Issue
Block a user