gpt4 book ai didi

java - 搜索条件不起作用

转载 作者:行者123 更新时间:2023-12-02 05:01:48 26 4
gpt4 key购买 nike

我对对象标准有疑问。

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

我能够制作这个过滤器,但是当有 5 个字段时它不起作用。知道某些字段可以为空。

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()));
}
}

最佳答案

你几乎猜对了。取出 else if 中的 else

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()));
}

应该可以

关于java - 搜索条件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28211571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com