从Hibernate 5.x迁移到Hibernate 6.x – 如何迁移ComparisonPredicate类的使用

huangapple go评论97阅读模式
英文:

Migrated from Hibernate 5.x to Hibernate 6.x - How to migrate ComparisonPredicate class usage

问题

We are migrating from springboot 2.5 to the latest 3.1.0, which means hibernate migration 5.x to 6.x.

我們正在從 springboot 2.5 遷移到最新的 3.1.0 版本,這意味著要將 Hibernate 從 5.x 遷移到 6.x。

I have below code which use two classes 'CriteriaBuilderImpl' and 'ComparisonPredicate', which I think are removed in hibernate 6.x.

我有下面的代碼,使用了兩個類 'CriteriaBuilderImpl' 和 'ComparisonPredicate',我認為在 Hibernate 6.x 中已經刪除了這兩個類。

Can someone please suggest how the below code can be migrated to be hibernate 6.x comptatible ? As of now with springboot 3.1.0, the below code gives me 'cannot find symbol' for the above two classes.

有人能否提供建議,如何將下面的代碼遷移到 Hibernate 6.x 兼容?目前使用 springboot 3.1.0,下面的代碼對於上述兩個類給出了 'cannot find symbol' 錯誤。

Thanks in advance.

提前感謝。

英文:

We are migrating from springboot 2.5 to the latest 3.1.0, which means hibernate migration 5.x to 6.x.

I have below code which use two classes 'CriteriaBuilderImpl' and 'ComparisonPredicate', which I think are removed in hibernate 6.x. As I have read the migration guides (https://github.com/quarkusio/quarkus/wiki/Migration-Guide-3.0:-Hibernate-ORM-5-to-6-migration#legacy-criteria) which calls for an action => Migrate existing code that relies on legacy Hibernate Criteria API to the JPA Criteria API.

Can someone please suggest how the below code can be migrated to be hibernate 6.x comptatible ? As of now with springboot 3.1.0, the below code gives me 'cannot find symbol' for the above two classes.
Thanks in advance.

import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import org.hibernate.query.criteria.internal.predicate.ComparisonPredicate;       

    private Specification<ProductDAO> getSpecFromDatesAndExample(CriteriaBuilder builder, Date startDate, Date endDate) {
           final List<Predicate> predicates = new ArrayList<>();
           
           predicates.add(new ComparisonPredicate((CriteriaBuilderImpl) builder,
                ComparisonPredicate.ComparisonOperator.GREATER_THAN, root.get("startDate"), startDate));

           predicates.add(new ComparisonPredicate((CriteriaBuilderImpl) builder, 
                ComparisonPredicate.ComparisonOperator.LESS_THAN, root.get("endDate"), endDate));
    
                return builder.and(predicates.toArray(new Predicate[predicates.size()]));

    }

答案1

得分: 0

我在CriteriaBuilder中找到了greaterThan()和lessThan()方法,我已经使用过了。

例如,

predicates.add(new ComparisonPredicate((CriteriaBuilderImpl) builder, ComparisonPredicate.ComparisonOperator.GREATER_THAN, root.get("startDate"), startDate));

更新为,

predicates.add(builder.greaterThan(root.get("startDate"), startDate));

希望对某人有所帮助!

英文:

I found the methods greaterThan(), lessThan() in CriteriaBuilder that I have used.

For example,

predicates.add(new ComparisonPredicate((CriteriaBuilderImpl) builder,
            ComparisonPredicate.ComparisonOperator.GREATER_THAN, root.get("startDate"), startDate));

updated to,

predicates.add(builder.greaterThan(root.get("startDate"), startDate);

Hope this helps someone!

答案2

得分: 0

如果你想尝试这种方法也可以:

if(Optional.ofNullable(getStartDate()).isPresent()) {
    predicates.and(root.get("startDate").goe(startDate));
}
英文:

you can try this approach also

if(Optional.ofNullable(getStartDate()).isPresent()) {
            predicates.and(root.get("startDate").goe(startDate));
        }

huangapple
  • 本文由 发表于 2023年6月15日 12:42:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定