Java Tutorial/Hibernate/Query by Example
QBE Advanced Criteria
File: Main.java
<source lang="java">
import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Example; public class Main {
public static void main(String[] args) throws Exception { HibernateUtil hibernateUtil = new HibernateUtil(); hibernateUtil .executeSQLCommand("create table Product(id int, name varchar, description varchar, price decimal(6,2), supplierid int)"); hibernateUtil .executeSQLCommand("create table Supplier (id int , name varchar)"); hibernateUtil .executeSQLCommand("create table Software(id int, name varchar, description varchar, price decimal(6,2), supplierid int, version varchar)"); Session session = hibernateUtil.getSession(); Supplier superCorp = new Supplier(); superCorp.setName("Supplier1"); session.save(superCorp); Supplier megaInc = new Supplier(); megaInc.setName("Supplier2"); session.save(megaInc); Product mouse = new Product("Product1","first product", 20.0); mouse.setSupplier(superCorp); superCorp.getProducts().add(mouse); session.flush(); Product mouse2 = new Product("Product2","second product", 22.0); mouse2.setSupplier(superCorp); superCorp.getProducts().add(mouse2); Product keyboard = new Product("Product3", "third product", 30.0); keyboard.setSupplier(megaInc); megaInc.getProducts().add(keyboard); Software webBrowser = new Software("Web Browser","new browser", 75.0, "2.0"); webBrowser.setSupplier(superCorp); superCorp.getProducts().add(webBrowser); Software email = new Software("Email","email client", 49.99, "4.1 Edition"); email.setSupplier(megaInc); megaInc.getProducts().add(email); session.flush(); Criteria prdCrit = session.createCriteria(Product.class); Product product = new Product(); product.setName("P%"); Example prdExample = Example.create(product); prdExample.excludeProperty("price"); prdExample.enableLike(); Criteria suppCrit = prdCrit.createCriteria("supplier"); Supplier supplier = new Supplier(); supplier.setName("Supplier2"); suppCrit.add(Example.create(supplier)); prdCrit.add(prdExample); List results = prdCrit.list();
System.out.println(results); session.close(); hibernateUtil.checkData("select * from Product"); hibernateUtil.checkData("select * from Software"); hibernateUtil.checkData("select * from Supplier"); }
}</source>
QBE Criteria: Not Null Or Zero
File: Main.java
<source lang="java">
import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Example; public class Main {
public static void main(String[] args) throws Exception { HibernateUtil hibernateUtil = new HibernateUtil(); hibernateUtil .executeSQLCommand("create table Product(id int, name varchar, description varchar, price decimal(6,2), supplierid int)"); hibernateUtil .executeSQLCommand("create table Supplier (id int , name varchar)"); hibernateUtil .executeSQLCommand("create table Software(id int, name varchar, description varchar, price decimal(6,2), supplierid int, version varchar)"); Session session = hibernateUtil.getSession(); Supplier superCorp = new Supplier(); superCorp.setName("Supplier1"); session.save(superCorp); Supplier megaInc = new Supplier(); megaInc.setName("Supplier2"); session.save(megaInc); Product mouse = new Product("Product1","first product", 20.0); mouse.setSupplier(superCorp); superCorp.getProducts().add(mouse); session.flush(); Product mouse2 = new Product("Product2","second product", 22.0); mouse2.setSupplier(superCorp); superCorp.getProducts().add(mouse2); Product keyboard = new Product("Product3", "third product", 30.0); keyboard.setSupplier(megaInc); megaInc.getProducts().add(keyboard); Software webBrowser = new Software("Web Browser","new browser", 75.0, "2.0"); webBrowser.setSupplier(superCorp); superCorp.getProducts().add(webBrowser); Software email = new Software("Email","email client", 49.99, "4.1 Edition"); email.setSupplier(megaInc); megaInc.getProducts().add(email); session.flush(); Criteria crit = session.createCriteria(Product.class); Product exampleProduct = new Product(); exampleProduct.setName("Product3"); Example example = Example.create(exampleProduct); example.excludeZeroes(); crit.add(example); List results = crit.list();
System.out.println(results); session.close(); hibernateUtil.checkData("select * from Product"); hibernateUtil.checkData("select * from Software"); hibernateUtil.checkData("select * from Supplier"); }
}</source>
Query By Example
File: Main.java
<source lang="java">
import java.util.Date; import java.util.Iterator; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Example; public class Main {
public static void main(String[] args) throws Exception { HibernateUtil hibernateUtil = new HibernateUtil(); hibernateUtil .executeSQLCommand("create table survey (id int,name varchar, purchasedate date);"); Session session = hibernateUtil.getSession(); Survey survey = new Survey(); survey.setName("Survey"); survey.setPurchaseDate(new Date()); session.save(survey); survey = new Survey(); survey.setName("Survey1"); survey.setPurchaseDate(new Date()); session.save(survey); session.flush(); Criteria crit = session.createCriteria(Survey.class); Survey supplier = new Survey(); supplier.setName("Survey"); crit.add(Example.create(supplier)); List results = crit.list(); Iterator iter = results.iterator(); while (iter.hasNext()) { Survey cd = (Survey) iter.next(); System.out.println("Result Name:"+cd.getName()); } session.close(); hibernateUtil.checkData("select * from survey"); }
}</source>