Write not equal Restriction with Hibernate Criteria if the particular column is not null

Hii,
I am using Hibernate 4.3 version.

I need to write restriction on a particular column(which is Many to one Mapped) if that particular column is not null, I am unable to write this condition in the criteria query.

Thanks.

Hibernate Criteria is no longer supported. You need to migrate to JPA Criteria API and a newer version of Hibernate as well (e.g 5.2).

is there any suggestion to solve in this version, because I have to change the Queries which is no longer supported in 5.2 version

Hibernate 4.3 also supports JPA, so you can use Criteria API just as fine.

Hii I have Written Criteria Query like this.

Criterion allergen1 = Restrictions.neOrIsNotNull("allergen1",
	  staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen2 = Restrictions.neOrIsNotNull("allergen2",
	  staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen3 = Restrictions.neOrIsNotNull("allergen3",
	  staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen4 = Restrictions.neOrIsNotNull("allergen4",
	  staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen5 = Restrictions.neOrIsNotNull("allergen5",
	  staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen6 = Restrictions.neOrIsNotNull("allergen6",
	  staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen7 = Restrictions.neOrIsNotNull("allergen7",
	  staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen8 = Restrictions.neOrIsNotNull("allergen8",
	  staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Disjunction orExp = Restrictions.or(allergen1, allergen2, allergen3, allergen4,
	  allergen5, allergen6, allergen7, allergen8);
criteria.add(orExp);

in this scenario, if allergen1.allergen2,allergen3,…allergen8 are not null then its work fine, but if any field is null then it not worked perfectly.

First, you need to add the SQL statement you want to be generated.

Hii

Thanks for your support.
I resolve my issue.

the code is like this:

Criterion allergen1Condition1=Restrictions.ne("allergen1",staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen1Condition2=Restrictions.isNull("allergen1");
LogicalExpression allergen1Condition=Restrictions.or(allergen1Condition1,allergen1Condition2);

Criterion allergen2Condition1=Restrictions.ne("allergen2",staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen2Condition2=Restrictions.isNull("allergen2");
LogicalExpression allergen2Condition=Restrictions.or(allergen2Condition1,allergen2Condition2);

Criterion allergen3Condition1=Restrictions.ne("allergen3",staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen3Condition2=Restrictions.isNull("allergen3");
LogicalExpression allergen3Condition=Restrictions.or(allergen3Condition1,allergen3Condition2);

Criterion allergen4Condition1=Restrictions.ne("allergen4",staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen4Condition2=Restrictions.isNull("allergen4");
LogicalExpression allergen4Condition=Restrictions.or(allergen4Condition1,allergen4Condition2);

Criterion allergen5Condition1=Restrictions.ne("allergen5",staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen5Condition2=Restrictions.isNull("allergen5");
LogicalExpression allergen5Condition=Restrictions.or(allergen5Condition1,allergen5Condition2);

Criterion allergen6Condition1=Restrictions.ne("allergen6",staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen6Condition2=Restrictions.isNull("allergen6");
LogicalExpression allergen6Condition=Restrictions.or(allergen6Condition1,allergen6Condition2);

Criterion allergen7Condition1=Restrictions.ne("allergen7",staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen7Condition2=Restrictions.isNull("allergen7");
LogicalExpression allergen7Condition=Restrictions.or(allergen7Condition1,allergen7Condition2);

Criterion allergen8Condition1=Restrictions.ne("allergen8",staticAppDataDao.getAllergenById(Integer.valueOf(allergenList[i])));
Criterion allergen8Condition2=Restrictions.isNull("allergen8");
LogicalExpression allergen8Condition=Restrictions.or(allergen8Condition1,allergen8Condition2);

Conjunction allergenCondition = Restrictions.and(allergen1Condition, allergen2Condition, allergen3Condition,
	  allergen4Condition,allergen5Condition,allergen6Condition,allergen7Condition,allergen8Condition);
criteria.add(allergenCondition);