Reputation: 49
I have a problem with object criteria.
I have 5 fields : SN(string), IP address(string), Site(string), Install Date(date), End Date(date).
I want to be able to combine fields to reduce the result.
For example if i have a device with serial number 1234 and ip 10.4.5
If I search serial number 1234, it return the device
If I search serial number 1234 and ip 10.4.5 it return the device
If I search serial number 1234 and ip 12.2.3 it doesn't return anything
I was able to make this filter but when there is 5 fields it's not working.
Knowing that some fields can be empty.
public List<IDevice> findByObject(Device device) {
Criteria criteria = getSessionFactory().getCurrentSession()
.createCriteria(Device.class);
if (device.getSerialNumber() != null &&
device.getSerialNumber().trim().length() > 0 &&
device.getIpAdress() != null &&
device.getIpAdress().trim().length() > 0 &&
device.getOwner() != null &&
device.getOwner().getSiteName().trim().length() > 0) {
criteria.add(Restrictions.and(
Restrictions.like("serialNumber", device.getSerialNumber()),
Restrictions.like("installDate", device.getInstallDate()),
Restrictions.like("ipAdress", device.getIpAdress())));
criteria.createCriteria("owner")
.add(Restrictions.and(Restrictions
.like("siteName", device.getOwner().getSiteName())));
}
else if (device.getIpAdress() != null &&
device.getIpAdress().trim().length() > 0 ) {
criteria.add(Restrictions.like("ipAdress", device.getIpAdress()));
}
else if (device.getSerialNumber() != null &&
device.getSerialNumber().trim().length() > 0 ) {
criteria.add(Restrictions.like("serialNumber", device.getSerialNumber()));
}
else if (device.getOwner() != null &&
device.getOwner().getSiteName() != null &&
device.getOwner().getSiteName().trim().length() > 0) {
criteria.createCriteria("owner")
.add(Restrictions.like("siteName", device.getOwner().getSiteName()));
}
else if (device.getInstallDate() != null) {
criteria.add(Restrictions.like("installDate", device.getInstallDate()));
}
}
Upvotes: 0
Views: 111
Reputation: 802
It looks that in your first if condition you have not checked condition for installDate null.
Upvotes: 0
Reputation: 1567
you almost had it correct. Take out the else
in your else if
public List<IDevice> findByObject(Device device) {
Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(Device.class);
if (device.getIpAdress() != null && device.getIpAdress().trim().length() > 0 ) {
criteria.add(Restrictions.like("ipAdress", device.getIpAdress()));
}
if (device.getSerialNumber() != null && device.getSerialNumber().trim().length() > 0 ) {
criteria.add(Restrictions.like("serialNumber", device.getSerialNumber()));
}
if (device.getOwner() != null && device.getOwner().getSiteName() != null && device.getOwner().getSiteName().trim().length() > 0) {
criteria.createCriteria("owner").add(Restrictions.like("siteName", device.getOwner().getSiteName()));
}
if (device.getInstallDate() != null) {
criteria.add(Restrictions.like("installDate", device.getInstallDate()));
}
that should work
Upvotes: 2