move and upgrade hibernate criteria
This commit is contained in:
parent
489a26d429
commit
1e11dea206
|
@ -0,0 +1,81 @@
|
|||
package com.baeldung.hibernate.criteria.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Item implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Integer itemId;
|
||||
private String itemName;
|
||||
private String itemDescription;
|
||||
private Integer itemPrice;
|
||||
|
||||
// constructors
|
||||
public Item() {
|
||||
|
||||
}
|
||||
|
||||
public Item(final Integer itemId, final String itemName, final String itemDescription) {
|
||||
super();
|
||||
this.itemId = itemId;
|
||||
this.itemName = itemName;
|
||||
this.itemDescription = itemDescription;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((itemId == null) ? 0 : itemId.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final Item other = (Item) obj;
|
||||
if (itemId == null) {
|
||||
if (other.itemId != null)
|
||||
return false;
|
||||
} else if (!itemId.equals(other.itemId))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public Integer getItemId() {
|
||||
return itemId;
|
||||
}
|
||||
|
||||
public void setItemId(final Integer itemId) {
|
||||
this.itemId = itemId;
|
||||
}
|
||||
|
||||
public String getItemName() {
|
||||
return itemName;
|
||||
}
|
||||
|
||||
public void setItemName(final String itemName) {
|
||||
this.itemName = itemName;
|
||||
}
|
||||
|
||||
public String getItemDescription() {
|
||||
return itemDescription;
|
||||
}
|
||||
|
||||
public Integer getItemPrice() {
|
||||
return itemPrice;
|
||||
}
|
||||
|
||||
public void setItemPrice(final Integer itemPrice) {
|
||||
this.itemPrice = itemPrice;
|
||||
}
|
||||
|
||||
public void setItemDescription(final String itemDescription) {
|
||||
this.itemDescription = itemDescription;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,335 @@
|
|||
/**
|
||||
* ApplicationViewer is the class that starts the application
|
||||
* First it creates the session object and then creates the
|
||||
* criteria query.
|
||||
*
|
||||
* @author Pritam Banerjee
|
||||
* @version 1.0
|
||||
* @since 07/20/2016
|
||||
*/
|
||||
|
||||
package com.baeldung.hibernate.criteria.view;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.query.Query;
|
||||
|
||||
import com.baeldung.hibernate.criteria.model.Item;
|
||||
import com.baeldung.hibernate.criteria.util.HibernateUtil;
|
||||
|
||||
public class ApplicationView {
|
||||
|
||||
// default Constructor
|
||||
public ApplicationView() {
|
||||
|
||||
}
|
||||
|
||||
public boolean checkIfCriteriaTimeLower() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
|
||||
// calculate the time taken by criteria
|
||||
final long startTimeCriteria = System.nanoTime();
|
||||
cr.select(root)
|
||||
.where(cb.like(root.get("itemName"), "%item One%"));
|
||||
// .add(Restrictions.like("itemName", "%item One%"));
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
|
||||
final List<Item> results = query.getResultList();
|
||||
final long endTimeCriteria = System.nanoTime();
|
||||
final long durationCriteria = (endTimeCriteria - startTimeCriteria) / 1000;
|
||||
|
||||
// calculate the time taken by HQL
|
||||
final long startTimeHQL = System.nanoTime();
|
||||
session.beginTransaction();
|
||||
final List<Item> items = session.createQuery("FROM Item where itemName like '%item One%'")
|
||||
.list();
|
||||
final long endTimeHQL = System.nanoTime();
|
||||
final long durationHQL = (endTimeHQL - startTimeHQL) / 1000;
|
||||
|
||||
if (durationCriteria > durationHQL) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// To get items having price more than 1000
|
||||
public String[] greaterThanCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
cr.select(root)
|
||||
.where(cb.gt(root.get("itemPrice"), 1000));
|
||||
// cr.add(Restrictions.gt("itemPrice", 1000));
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
final List<Item> greaterThanItemsList = query.getResultList();
|
||||
final String greaterThanItems[] = new String[greaterThanItemsList.size()];
|
||||
for (int i = 0; i < greaterThanItemsList.size(); i++) {
|
||||
greaterThanItems[i] = greaterThanItemsList.get(i)
|
||||
.getItemName();
|
||||
}
|
||||
session.close();
|
||||
return greaterThanItems;
|
||||
}
|
||||
|
||||
// To get items having price less than 1000
|
||||
public String[] lessThanCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
cr.select(root)
|
||||
.where(cb.lt(root.get("itemPrice"), 1000));
|
||||
// cr.add(Restrictions.lt("itemPrice", 1000));
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
final List<Item> lessThanItemsList = query.getResultList();
|
||||
final String lessThanItems[] = new String[lessThanItemsList.size()];
|
||||
for (int i = 0; i < lessThanItemsList.size(); i++) {
|
||||
lessThanItems[i] = lessThanItemsList.get(i)
|
||||
.getItemName();
|
||||
}
|
||||
session.close();
|
||||
return lessThanItems;
|
||||
}
|
||||
|
||||
// To get items whose Name start with Chair
|
||||
public String[] likeCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
cr.select(root)
|
||||
.where(cb.like(root.get("itemName"), "%chair%"));
|
||||
// cr.add(Restrictions.like("itemName", "%chair%"));
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
final List<Item> likeItemsList = query.getResultList();
|
||||
final String likeItems[] = new String[likeItemsList.size()];
|
||||
for (int i = 0; i < likeItemsList.size(); i++) {
|
||||
likeItems[i] = likeItemsList.get(i)
|
||||
.getItemName();
|
||||
}
|
||||
session.close();
|
||||
return likeItems;
|
||||
}
|
||||
|
||||
// Case sensitive search
|
||||
public String[] likeCaseCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
cr.select(root)
|
||||
.where(cb.like(cb.lower(root.get("itemName")), "%chair%"));
|
||||
// cr.add(Restrictions.ilike("itemName", "%Chair%"));
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
final List<Item> ilikeItemsList = query.getResultList();
|
||||
final String ilikeItems[] = new String[ilikeItemsList.size()];
|
||||
for (int i = 0; i < ilikeItemsList.size(); i++) {
|
||||
ilikeItems[i] = ilikeItemsList.get(i)
|
||||
.getItemName();
|
||||
}
|
||||
session.close();
|
||||
return ilikeItems;
|
||||
}
|
||||
|
||||
// To get records having itemPrice in between 100 and 200
|
||||
public String[] betweenCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
cr.select(root)
|
||||
.where(cb.between(root.get("itemPrice"), 100, 200));
|
||||
// cr.add(Restrictions.between("itemPrice", 100, 200));
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
final List<Item> betweenItemsList = query.getResultList();
|
||||
final String betweenItems[] = new String[betweenItemsList.size()];
|
||||
for (int i = 0; i < betweenItemsList.size(); i++) {
|
||||
betweenItems[i] = betweenItemsList.get(i)
|
||||
.getItemName();
|
||||
}
|
||||
session.close();
|
||||
return betweenItems;
|
||||
}
|
||||
|
||||
// To check if the given property is null
|
||||
public String[] nullCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
cr.select(root)
|
||||
.where(cb.isNull(root.get("itemDescription")));
|
||||
// cr.add(Restrictions.isNull("itemDescription"));
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
final List<Item> nullItemsList = query.getResultList();
|
||||
final String nullDescItems[] = new String[nullItemsList.size()];
|
||||
for (int i = 0; i < nullItemsList.size(); i++) {
|
||||
nullDescItems[i] = nullItemsList.get(i)
|
||||
.getItemName();
|
||||
}
|
||||
session.close();
|
||||
return nullDescItems;
|
||||
}
|
||||
|
||||
// To check if the given property is not null
|
||||
public String[] notNullCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
cr.select(root)
|
||||
.where(cb.isNotNull(root.get("itemDescription")));
|
||||
// cr.add(Restrictions.isNotNull("itemDescription"));
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
final List<Item> notNullItemsList = query.getResultList();
|
||||
final String notNullDescItems[] = new String[notNullItemsList.size()];
|
||||
for (int i = 0; i < notNullItemsList.size(); i++) {
|
||||
notNullDescItems[i] = notNullItemsList.get(i)
|
||||
.getItemName();
|
||||
}
|
||||
session.close();
|
||||
return notNullDescItems;
|
||||
}
|
||||
|
||||
// Adding more than one expression in one cr
|
||||
public String[] twoCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
Predicate[] predicates = new Predicate[2];
|
||||
predicates[0] = cb.isNull(root.get("itemDescription"));
|
||||
predicates[1] = cb.like(root.get("itemName"), "chair%");
|
||||
cr.select(root)
|
||||
.where(predicates);
|
||||
// cr.add(Restrictions.isNull("itemDescription"));
|
||||
// cr.add(Restrictions.like("itemName", "chair%"));
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
final List<Item> notNullItemsList = query.getResultList();
|
||||
final String notNullDescItems[] = new String[notNullItemsList.size()];
|
||||
for (int i = 0; i < notNullItemsList.size(); i++) {
|
||||
notNullDescItems[i] = notNullItemsList.get(i)
|
||||
.getItemName();
|
||||
}
|
||||
session.close();
|
||||
return notNullDescItems;
|
||||
}
|
||||
|
||||
// To get items matching with the above defined conditions joined
|
||||
// with Logical AND
|
||||
public String[] andLogicalCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
Predicate greaterThanPrice = cb.gt(root.get("itemPrice"), 1000);
|
||||
Predicate chairItems = cb.like(root.get("itemName"), "Chair%");
|
||||
cr.select(root)
|
||||
.where(cb.and(greaterThanPrice, chairItems));
|
||||
// final Criterion greaterThanPrice = Restrictions.gt("itemPrice", 1000);
|
||||
// final Criterion chairItems = Restrictions.like("itemName", "Chair%");
|
||||
// final LogicalExpression andExample = Restrictions.and(greaterThanPrice, chairItems);
|
||||
// cr.add(andExample);
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
final List<Item> andItemsList = query.getResultList();
|
||||
final String andItems[] = new String[andItemsList.size()];
|
||||
for (int i = 0; i < andItemsList.size(); i++) {
|
||||
andItems[i] = andItemsList.get(i)
|
||||
.getItemName();
|
||||
}
|
||||
session.close();
|
||||
return andItems;
|
||||
}
|
||||
|
||||
// To get items matching with the above defined conditions joined
|
||||
// with Logical OR
|
||||
public String[] orLogicalCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
Predicate greaterThanPrice = cb.gt(root.get("itemPrice"), 1000);
|
||||
Predicate chairItems = cb.like(root.get("itemName"), "Chair%");
|
||||
cr.select(root)
|
||||
.where(cb.or(greaterThanPrice, chairItems));
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
final List<Item> orItemsList = query.getResultList();
|
||||
final String orItems[] = new String[orItemsList.size()];
|
||||
for (int i = 0; i < orItemsList.size(); i++) {
|
||||
orItems[i] = orItemsList.get(i)
|
||||
.getItemName();
|
||||
}
|
||||
session.close();
|
||||
return orItems;
|
||||
}
|
||||
|
||||
// Sorting example
|
||||
public String[] sortingCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Item> cr = cb.createQuery(Item.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
cr.select(root);
|
||||
cr.orderBy(cb.asc(root.get("itemName")), cb.desc(root.get("itemPrice")));
|
||||
// cr.addOrder(Order.asc("itemName"));
|
||||
// cr.addOrder(Order.desc("itemPrice")).list();
|
||||
Query<Item> query = session.createQuery(cr);
|
||||
final List<Item> sortedItemsList = query.getResultList();
|
||||
final String sortedItems[] = new String[sortedItemsList.size()];
|
||||
for (int i = 0; i < sortedItemsList.size(); i++) {
|
||||
sortedItems[i] = sortedItemsList.get(i)
|
||||
.getItemName();
|
||||
}
|
||||
session.close();
|
||||
return sortedItems;
|
||||
}
|
||||
|
||||
// Set projections Row Count
|
||||
public Long[] projectionRowCount() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Long> cr = cb.createQuery(Long.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
cr.select(cb.count(root));
|
||||
Query<Long> query = session.createQuery(cr);
|
||||
final List<Long> itemProjected = query.getResultList();
|
||||
// session.createCriteria(Item.class).setProjection(Projections.rowCount()).list();
|
||||
final Long projectedRowCount[] = new Long[itemProjected.size()];
|
||||
for (int i = 0; i < itemProjected.size(); i++) {
|
||||
projectedRowCount[i] = itemProjected.get(i);
|
||||
}
|
||||
session.close();
|
||||
return projectedRowCount;
|
||||
}
|
||||
|
||||
// Set projections average of itemPrice
|
||||
public Double[] projectionAverage() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
final CriteriaQuery<Double> cr = cb.createQuery(Double.class);
|
||||
final Root<Item> root = cr.from(Item.class);
|
||||
cr.select(cb.avg(root.get("itemPrice")));
|
||||
Query<Double> query = session.createQuery(cr);
|
||||
final List avgItemPriceList = query.getResultList();
|
||||
// session.createCriteria(Item.class).setProjection(Projections.projectionList().add(Projections.avg("itemPrice"))).list();
|
||||
|
||||
final Double avgItemPrice[] = new Double[avgItemPriceList.size()];
|
||||
for (int i = 0; i < avgItemPriceList.size(); i++) {
|
||||
avgItemPrice[i] = (Double) avgItemPriceList.get(i);
|
||||
}
|
||||
session.close();
|
||||
return avgItemPrice;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
insert into item (item_id, item_name, item_desc, item_price)
|
||||
values(1,'item One', 'test 1', 35.12);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price)
|
||||
values(2,'Pogo stick', 'Pogo stick', 466.12);
|
||||
insert into item (item_id, item_name, item_desc, item_price)
|
||||
values(3,'Raft', 'Raft', 345.12);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price)
|
||||
values(4,'Skate Board', 'Skating', 135.71);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price)
|
||||
values(5,'Umbrella', 'Umbrella for Rain', 619.25);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price)
|
||||
values(6,'Glue', 'Glue for home', 432.73);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price)
|
||||
values(7,'Paint', 'Paint for Room', 1311.40);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price)
|
||||
values(8,'Red paint', 'Red paint for room', 1135.71);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price)
|
||||
values(9,'Household Chairs', 'Chairs for house', 25.71);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price)
|
||||
values(10,'Office Chairs', 'Chairs for office', 395.98);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price)
|
||||
values(11,'Outdoor Chairs', 'Chairs for outdoor activities', 1234.36);
|
|
@ -43,7 +43,6 @@ public class HibernateCriteriaIntegrationTest {
|
|||
}
|
||||
session.close();
|
||||
assertArrayEquals(expectedChairCaseItems, av.likeCaseCriteria());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
|
@ -10,8 +10,8 @@
|
|||
<property name="hibernate.connection.username">sa</property>
|
||||
<property name="hibernate.connection.password"></property>
|
||||
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
|
||||
<property name="hibernate.hbm2ddl.auto">update</property>
|
||||
<property name="show_sql">true</property>
|
||||
<property name="hibernate.hbm2ddl.auto">create-drop</property>
|
||||
<property name="show_sql">false</property>
|
||||
<mapping resource="com/baeldung/hibernate/criteria/model/Item.hbm.xml" />
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
|
@ -0,0 +1,21 @@
|
|||
insert into item (item_id, item_name, item_desc, item_price) values(1,'item One', 'test 1', 35.12);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price) values(2,'Pogo stick', 'Pogo stick', 466.12);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price) values(3,'Raft', 'Raft', 345.12);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price) values(4,'Skate Board', 'Skating', 135.71);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price) values(5,'Umbrella', 'Umbrella for Rain', 619.25);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price) values(6,'Glue', 'Glue for home', 432.73);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price) values(7,'Paint', 'Paint for Room', 1311.40);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price) values(8,'Red paint', 'Red paint for room', 1135.71);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price) values(9,'Household Chairs', 'Chairs for house', 25.71);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price) values(10,'Office Chairs', 'Chairs for office', 395.98);
|
||||
|
||||
insert into item (item_id, item_name, item_desc, item_price) values(11,'Outdoor Chairs', 'Chairs for outdoor activities', 1234.36);
|
|
@ -1,251 +0,0 @@
|
|||
/**
|
||||
* ApplicationViewer is the class that starts the application
|
||||
* First it creates the session object and then creates the
|
||||
* criteria query.
|
||||
*
|
||||
* @author Pritam Banerjee
|
||||
* @version 1.0
|
||||
* @since 07/20/2016
|
||||
*/
|
||||
|
||||
package com.baeldung.hibernate.criteria.view;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Criterion;
|
||||
import org.hibernate.criterion.LogicalExpression;
|
||||
import org.hibernate.criterion.Order;
|
||||
import org.hibernate.criterion.Projections;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
|
||||
import com.baeldung.hibernate.criteria.model.Item;
|
||||
import com.baeldung.hibernate.criteria.util.HibernateUtil;
|
||||
|
||||
public class ApplicationView {
|
||||
|
||||
// default Constructor
|
||||
public ApplicationView() {
|
||||
|
||||
}
|
||||
|
||||
public boolean checkIfCriteriaTimeLower() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
Transaction tx = null;
|
||||
|
||||
// calculate the time taken by criteria
|
||||
final long startTimeCriteria = System.nanoTime();
|
||||
cr.add(Restrictions.like("itemName", "%item One%"));
|
||||
final List results = cr.list();
|
||||
final long endTimeCriteria = System.nanoTime();
|
||||
final long durationCriteria = (endTimeCriteria - startTimeCriteria) / 1000;
|
||||
|
||||
// calculate the time taken by HQL
|
||||
final long startTimeHQL = System.nanoTime();
|
||||
tx = session.beginTransaction();
|
||||
final List items = session.createQuery("FROM Item where itemName like '%item One%'").list();
|
||||
final long endTimeHQL = System.nanoTime();
|
||||
final long durationHQL = (endTimeHQL - startTimeHQL) / 1000;
|
||||
|
||||
if (durationCriteria > durationHQL) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// To get items having price more than 1000
|
||||
public String[] greaterThanCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
cr.add(Restrictions.gt("itemPrice", 1000));
|
||||
final List<Item> greaterThanItemsList = cr.list();
|
||||
final String greaterThanItems[] = new String[greaterThanItemsList.size()];
|
||||
for (int i = 0; i < greaterThanItemsList.size(); i++) {
|
||||
greaterThanItems[i] = greaterThanItemsList.get(i).getItemName();
|
||||
}
|
||||
session.close();
|
||||
return greaterThanItems;
|
||||
}
|
||||
|
||||
// To get items having price less than 1000
|
||||
public String[] lessThanCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
cr.add(Restrictions.lt("itemPrice", 1000));
|
||||
final List<Item> lessThanItemsList = cr.list();
|
||||
final String lessThanItems[] = new String[lessThanItemsList.size()];
|
||||
for (int i = 0; i < lessThanItemsList.size(); i++) {
|
||||
lessThanItems[i] = lessThanItemsList.get(i).getItemName();
|
||||
}
|
||||
session.close();
|
||||
return lessThanItems;
|
||||
}
|
||||
|
||||
// To get items whose Name start with Chair
|
||||
public String[] likeCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
cr.add(Restrictions.like("itemName", "%chair%"));
|
||||
final List<Item> likeItemsList = cr.list();
|
||||
final String likeItems[] = new String[likeItemsList.size()];
|
||||
for (int i = 0; i < likeItemsList.size(); i++) {
|
||||
likeItems[i] = likeItemsList.get(i).getItemName();
|
||||
}
|
||||
session.close();
|
||||
return likeItems;
|
||||
}
|
||||
|
||||
// Case sensitive search
|
||||
public String[] likeCaseCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
cr.add(Restrictions.ilike("itemName", "%Chair%"));
|
||||
final List<Item> ilikeItemsList = cr.list();
|
||||
final String ilikeItems[] = new String[ilikeItemsList.size()];
|
||||
for (int i = 0; i < ilikeItemsList.size(); i++) {
|
||||
ilikeItems[i] = ilikeItemsList.get(i).getItemName();
|
||||
}
|
||||
session.close();
|
||||
return ilikeItems;
|
||||
}
|
||||
|
||||
// To get records having itemPrice in between 100 and 200
|
||||
public String[] betweenCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
// To get items having price more than 1000
|
||||
cr.add(Restrictions.between("itemPrice", 100, 200));
|
||||
final List<Item> betweenItemsList = cr.list();
|
||||
final String betweenItems[] = new String[betweenItemsList.size()];
|
||||
for (int i = 0; i < betweenItemsList.size(); i++) {
|
||||
betweenItems[i] = betweenItemsList.get(i).getItemName();
|
||||
}
|
||||
session.close();
|
||||
return betweenItems;
|
||||
}
|
||||
|
||||
// To check if the given property is null
|
||||
public String[] nullCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
cr.add(Restrictions.isNull("itemDescription"));
|
||||
final List<Item> nullItemsList = cr.list();
|
||||
final String nullDescItems[] = new String[nullItemsList.size()];
|
||||
for (int i = 0; i < nullItemsList.size(); i++) {
|
||||
nullDescItems[i] = nullItemsList.get(i).getItemName();
|
||||
}
|
||||
session.close();
|
||||
return nullDescItems;
|
||||
}
|
||||
|
||||
// To check if the given property is not null
|
||||
public String[] notNullCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
cr.add(Restrictions.isNotNull("itemDescription"));
|
||||
final List<Item> notNullItemsList = cr.list();
|
||||
final String notNullDescItems[] = new String[notNullItemsList.size()];
|
||||
for (int i = 0; i < notNullItemsList.size(); i++) {
|
||||
notNullDescItems[i] = notNullItemsList.get(i).getItemName();
|
||||
}
|
||||
session.close();
|
||||
return notNullDescItems;
|
||||
}
|
||||
|
||||
// Adding more than one expression in one cr
|
||||
public String[] twoCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
cr.add(Restrictions.isNull("itemDescription"));
|
||||
cr.add(Restrictions.like("itemName", "chair%"));
|
||||
final List<Item> notNullItemsList = cr.list();
|
||||
final String notNullDescItems[] = new String[notNullItemsList.size()];
|
||||
for (int i = 0; i < notNullItemsList.size(); i++) {
|
||||
notNullDescItems[i] = notNullItemsList.get(i).getItemName();
|
||||
}
|
||||
session.close();
|
||||
return notNullDescItems;
|
||||
}
|
||||
|
||||
// To get items matching with the above defined conditions joined
|
||||
// with Logical AND
|
||||
public String[] andLogicalCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
final Criterion greaterThanPrice = Restrictions.gt("itemPrice", 1000);
|
||||
final Criterion chairItems = Restrictions.like("itemName", "Chair%");
|
||||
final LogicalExpression andExample = Restrictions.and(greaterThanPrice, chairItems);
|
||||
cr.add(andExample);
|
||||
final List<Item> andItemsList = cr.list();
|
||||
final String andItems[] = new String[andItemsList.size()];
|
||||
for (int i = 0; i < andItemsList.size(); i++) {
|
||||
andItems[i] = andItemsList.get(i).getItemName();
|
||||
}
|
||||
session.close();
|
||||
return andItems;
|
||||
}
|
||||
|
||||
// To get items matching with the above defined conditions joined
|
||||
// with Logical OR
|
||||
public String[] orLogicalCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
final Criterion greaterThanPrice = Restrictions.gt("itemPrice", 1000);
|
||||
final Criterion chairItems = Restrictions.like("itemName", "Chair%");
|
||||
final LogicalExpression orExample = Restrictions.or(greaterThanPrice, chairItems);
|
||||
cr.add(orExample);
|
||||
final List<Item> orItemsList = cr.list();
|
||||
final String orItems[] = new String[orItemsList.size()];
|
||||
for (int i = 0; i < orItemsList.size(); i++) {
|
||||
orItems[i] = orItemsList.get(i).getItemName();
|
||||
}
|
||||
session.close();
|
||||
return orItems;
|
||||
}
|
||||
|
||||
// Sorting example
|
||||
public String[] sortingCriteria() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final Criteria cr = session.createCriteria(Item.class);
|
||||
cr.addOrder(Order.asc("itemName"));
|
||||
cr.addOrder(Order.desc("itemPrice")).list();
|
||||
final List<Item> sortedItemsList = cr.list();
|
||||
final String sortedItems[] = new String[sortedItemsList.size()];
|
||||
for (int i = 0; i < sortedItemsList.size(); i++) {
|
||||
sortedItems[i] = sortedItemsList.get(i).getItemName();
|
||||
}
|
||||
session.close();
|
||||
return sortedItems;
|
||||
}
|
||||
|
||||
// Set projections Row Count
|
||||
public Long[] projectionRowCount() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List<Long> itemProjected = session.createCriteria(Item.class).setProjection(Projections.rowCount()).list();
|
||||
final Long projectedRowCount[] = new Long[itemProjected.size()];
|
||||
for (int i = 0; i < itemProjected.size(); i++) {
|
||||
projectedRowCount[i] = itemProjected.get(i);
|
||||
}
|
||||
session.close();
|
||||
return projectedRowCount;
|
||||
}
|
||||
|
||||
// Set projections average of itemPrice
|
||||
public Double[] projectionAverage() {
|
||||
final Session session = HibernateUtil.getHibernateSession();
|
||||
final List avgItemPriceList = session.createCriteria(Item.class).setProjection(Projections.projectionList().add(Projections.avg("itemPrice"))).list();
|
||||
|
||||
final Double avgItemPrice[] = new Double[avgItemPriceList.size()];
|
||||
for (int i = 0; i < avgItemPriceList.size(); i++) {
|
||||
avgItemPrice[i] = (Double) avgItemPriceList.get(i);
|
||||
}
|
||||
session.close();
|
||||
return avgItemPrice;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
|
||||
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
|
||||
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
|
||||
<property name="hibernate.connection.username">root</property>
|
||||
<property name="hibernate.connection.password">iamtheking</property>
|
||||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||
<property name="show_sql">true</property>
|
||||
<mapping resource="com/baeldung/hibernate/criteria/model/Item.hbm.xml" />
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
|
@ -1,7 +0,0 @@
|
|||
CREATE TABLE `item` (
|
||||
`ITEM_ID` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`ITEM_DESC` varchar(100) DEFAULT NULL,
|
||||
`ITEM_PRICE` int(11) NOT NULL,
|
||||
`ITEM_NAME` varchar(255) NOT NULL,
|
||||
PRIMARY KEY (`ITEM_ID`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;
|
Loading…
Reference in New Issue