Using CriteriaDefinition Utility in Hibernate (#15128)
* Using CriteriaDefinition Utility in Hibernate * Using CriteriaDefinition Utility in Hibernate * Using CriteriaDefinition Utility in Hibernate * Using CriteriaDefinition Utility in Hibernate * Using CriteriaDefinition Utility in Hibernate
This commit is contained in:
parent
7b6ba0e030
commit
33fceb65a6
|
@ -99,7 +99,7 @@
|
||||||
<mysql.version>8.0.32</mysql.version>
|
<mysql.version>8.0.32</mysql.version>
|
||||||
<mariaDB4j.version>2.6.0</mariaDB4j.version>
|
<mariaDB4j.version>2.6.0</mariaDB4j.version>
|
||||||
<h2.version>2.1.214</h2.version>
|
<h2.version>2.1.214</h2.version>
|
||||||
<hibernate.version>6.1.7.Final</hibernate.version>
|
<hibernate.version>6.3.1.Final</hibernate.version>
|
||||||
<testcontainers.mysql.version>1.17.6</testcontainers.mysql.version>
|
<testcontainers.mysql.version>1.17.6</testcontainers.mysql.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
|
@ -16,4 +16,11 @@ public class HibernateUtil {
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static SessionFactory getHibernateSessionFactory() {
|
||||||
|
|
||||||
|
final SessionFactory sessionFactory = new Configuration().configure("criteria.cfg.xml").buildSessionFactory();
|
||||||
|
|
||||||
|
return sessionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,126 @@
|
||||||
|
package com.baeldung.hibernate.criteria.view;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.query.criteria.CriteriaDefinition;
|
||||||
|
import org.hibernate.query.criteria.JpaRoot;
|
||||||
|
|
||||||
|
import com.baeldung.hibernate.criteria.model.Item;
|
||||||
|
import com.baeldung.hibernate.criteria.util.HibernateUtil;
|
||||||
|
|
||||||
|
public class CriteriaDefinitionApplicationView {
|
||||||
|
|
||||||
|
public CriteriaDefinitionApplicationView() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] greaterThanCriteria() {
|
||||||
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
|
final SessionFactory sessionFactory = HibernateUtil.getHibernateSessionFactory();
|
||||||
|
|
||||||
|
CriteriaDefinition<Item> query = new CriteriaDefinition<>(sessionFactory, Item.class) {
|
||||||
|
{
|
||||||
|
JpaRoot<Item> message = from(Item.class);
|
||||||
|
where(gt(message.get("itemPrice"), 1000));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
List<Item> items = session.createSelectionQuery(query).list();
|
||||||
|
|
||||||
|
final String greaterThanItems[] = new String[items.size()];
|
||||||
|
for (int i = 0; i < items.size(); i++) {
|
||||||
|
greaterThanItems[i] = items.get(i).getItemName();
|
||||||
|
}
|
||||||
|
|
||||||
|
session.close();
|
||||||
|
|
||||||
|
return greaterThanItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] lessThanCriteria() {
|
||||||
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
|
final SessionFactory sessionFactory = HibernateUtil.getHibernateSessionFactory();
|
||||||
|
|
||||||
|
CriteriaDefinition<Item> query = new CriteriaDefinition<>(sessionFactory, Item.class) {
|
||||||
|
{
|
||||||
|
JpaRoot<Item> message = from(Item.class);
|
||||||
|
where(lt(message.get("itemPrice"), 1000));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
List<Item> items = session.createSelectionQuery(query).list();
|
||||||
|
final String lessThanItems[] = new String[items.size()];
|
||||||
|
for (int i = 0; i < items.size(); i++) {
|
||||||
|
lessThanItems[i] = items.get(i).getItemName();
|
||||||
|
}
|
||||||
|
|
||||||
|
session.close();
|
||||||
|
|
||||||
|
return lessThanItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] likeCriteria() {
|
||||||
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
|
final SessionFactory sessionFactory = HibernateUtil.getHibernateSessionFactory();
|
||||||
|
|
||||||
|
CriteriaDefinition<Item> query = new CriteriaDefinition<>(sessionFactory, Item.class) {
|
||||||
|
{
|
||||||
|
JpaRoot<Item> item = from(Item.class);
|
||||||
|
where(like(item.get("itemName"), "%chair%"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
List<Item> items = session.createSelectionQuery(query).list();
|
||||||
|
|
||||||
|
final String likeItems[] = new String[items.size()];
|
||||||
|
for (int i = 0; i < items.size(); i++) {
|
||||||
|
likeItems[i] = items.get(i).getItemName();
|
||||||
|
}
|
||||||
|
session.close();
|
||||||
|
return likeItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] likeCaseCriteria() {
|
||||||
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
|
final SessionFactory sessionFactory = HibernateUtil.getHibernateSessionFactory();
|
||||||
|
|
||||||
|
CriteriaDefinition<Item> query = new CriteriaDefinition<>(sessionFactory, Item.class) {
|
||||||
|
{
|
||||||
|
JpaRoot<Item> item = from(Item.class);
|
||||||
|
where(like(lower(item.get("itemName")), "%chair%"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
List<Item> items = session.createSelectionQuery(query).list();
|
||||||
|
|
||||||
|
final String likeItems[] = new String[items.size()];
|
||||||
|
for (int i = 0; i < items.size(); i++) {
|
||||||
|
likeItems[i] = items.get(i).getItemName();
|
||||||
|
}
|
||||||
|
session.close();
|
||||||
|
return likeItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] betweenCriteria() {
|
||||||
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
|
final SessionFactory sessionFactory = HibernateUtil.getHibernateSessionFactory();
|
||||||
|
|
||||||
|
CriteriaDefinition<Item> query = new CriteriaDefinition<>(sessionFactory, Item.class) {
|
||||||
|
{
|
||||||
|
JpaRoot<Item> item = from(Item.class);
|
||||||
|
where(between(item.get("itemPrice"), 100, 200));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
List<Item> items = session.createSelectionQuery(query).list();
|
||||||
|
|
||||||
|
final String betweenItems[] = new String[items.size()];
|
||||||
|
for (int i = 0; i < items.size(); i++) {
|
||||||
|
betweenItems[i] = items.get(i).getItemName();
|
||||||
|
}
|
||||||
|
session.close();
|
||||||
|
return betweenItems;
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,8 +4,13 @@ package com.baeldung.hibernate.customtypes;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
import org.hibernate.dialect.Dialect;
|
||||||
|
import org.hibernate.tool.schema.extract.spi.ColumnTypeInformation;
|
||||||
|
import org.hibernate.type.BasicType;
|
||||||
import org.hibernate.type.descriptor.WrapperOptions;
|
import org.hibernate.type.descriptor.WrapperOptions;
|
||||||
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
|
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
|
||||||
|
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
|
||||||
|
import org.hibernate.type.spi.TypeConfiguration;
|
||||||
|
|
||||||
import io.hypersistence.utils.hibernate.type.array.internal.AbstractArrayTypeDescriptor;
|
import io.hypersistence.utils.hibernate.type.array.internal.AbstractArrayTypeDescriptor;
|
||||||
|
|
||||||
|
@ -49,4 +54,10 @@ public class LocalDateStringJavaDescriptor extends AbstractArrayTypeDescriptor<L
|
||||||
|
|
||||||
throw unknownWrap(value.getClass());
|
throw unknownWrap(value.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BasicType resolveType(TypeConfiguration typeConfiguration, Dialect dialect, BasicType elementType, ColumnTypeInformation columnTypeInformation, JdbcTypeIndicators stdIndicators) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
package com.baeldung.hibernate.criteria;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.junit.Test;
|
||||||
|
import com.baeldung.hibernate.criteria.model.Item;
|
||||||
|
import com.baeldung.hibernate.criteria.util.HibernateUtil;
|
||||||
|
import com.baeldung.hibernate.criteria.view.CriteriaDefinitionApplicationView;
|
||||||
|
|
||||||
|
public class HibernateCriteriaDefinitionIntegrationTest {
|
||||||
|
|
||||||
|
final private CriteriaDefinitionApplicationView criteriaDefinition = new CriteriaDefinitionApplicationView();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHibernateSession_whenExecutingLikeCriteriaQuery_thenExpectMatchingItems() {
|
||||||
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
|
final List<Item> expectedLikeList = session.createQuery("From Item where itemName like '%chair%'").list();
|
||||||
|
final String expectedLikeItems[] = new String[expectedLikeList.size()];
|
||||||
|
for (int i = 0; i < expectedLikeList.size(); i++) {
|
||||||
|
expectedLikeItems[i] = expectedLikeList.get(i).getItemName();
|
||||||
|
}
|
||||||
|
session.close();
|
||||||
|
assertArrayEquals(expectedLikeItems, criteriaDefinition.likeCriteria());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHibernateSession_whenExecutingCaseSensitiveLikeCriteriaQuery_thenExpectMatchingItems() {
|
||||||
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
|
final List<Item> expectedChairCaseList = session.createQuery("From Item where itemName like '%Chair%'").list();
|
||||||
|
final String expectedChairCaseItems[] = new String[expectedChairCaseList.size()];
|
||||||
|
for (int i = 0; i < expectedChairCaseList.size(); i++) {
|
||||||
|
expectedChairCaseItems[i] = expectedChairCaseList.get(i).getItemName();
|
||||||
|
}
|
||||||
|
session.close();
|
||||||
|
assertArrayEquals(expectedChairCaseItems, criteriaDefinition.likeCaseCriteria());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHibernateSession_whenExecutingGreaterThanCriteriaQuery_thenExpectMatchingItems() {
|
||||||
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
|
final List<Item> expectedGreaterThanList = session.createQuery("From Item where itemPrice>1000").list();
|
||||||
|
final String expectedGreaterThanItems[] = new String[expectedGreaterThanList.size()];
|
||||||
|
for (int i = 0; i < expectedGreaterThanList.size(); i++) {
|
||||||
|
expectedGreaterThanItems[i] = expectedGreaterThanList.get(i).getItemName();
|
||||||
|
}
|
||||||
|
session.close();
|
||||||
|
assertArrayEquals(expectedGreaterThanItems, criteriaDefinition.greaterThanCriteria());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHibernateSession_whenExecutingLessThanCriteriaQuery_thenExpectMatchingItems() {
|
||||||
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
|
final List<Item> expectedLessList = session.createQuery("From Item where itemPrice<1000").list();
|
||||||
|
final String expectedLessThanItems[] = new String[expectedLessList.size()];
|
||||||
|
for (int i = 0; i < expectedLessList.size(); i++) {
|
||||||
|
expectedLessThanItems[i] = expectedLessList.get(i).getItemName();
|
||||||
|
}
|
||||||
|
session.close();
|
||||||
|
assertArrayEquals(expectedLessThanItems, criteriaDefinition.lessThanCriteria());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHibernateSession_whenExecutingBetweenCriteriaQuery_thenExpectMatchingItems() {
|
||||||
|
final Session session = HibernateUtil.getHibernateSession();
|
||||||
|
final List<Item> expectedBetweenList = session.createQuery("From Item where itemPrice between 100 and 200").list();
|
||||||
|
final String expectedPriceBetweenItems[] = new String[expectedBetweenList.size()];
|
||||||
|
for (int i = 0; i < expectedBetweenList.size(); i++) {
|
||||||
|
expectedPriceBetweenItems[i] = expectedBetweenList.get(i).getItemName();
|
||||||
|
}
|
||||||
|
session.close();
|
||||||
|
assertArrayEquals(expectedPriceBetweenItems, criteriaDefinition.betweenCriteria());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue