Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7084085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:15:29+00:00 2026-05-28T07:15:29+00:00

I have a Java class which has some field annotated with @SearchCriteria(criteria = class1.class2.field)

  • 0

I have a Java class which has some field annotated with @SearchCriteria(criteria = "class1.class2.field"). criteria parameter inside annotation means for with class this field should be set as hibernate criteria, it mean that, if field marked for example:

@SearchCriteria(criteria = "class1.class2.field")
private String value;

I want dynamicly create hibernate criteria with looks like

DetachedCriteria hibernateCriteria = forClass(Class.class);
hibernateCriteria.createCriteria("class1").createCriteria("class2").add(eq("field", value));

Problem is than I can not set another criteria to already added, it mean I should check annotation criteria option.

      switch (annotationCriteria.length - 1) {
case 0:             
hibernateCriteria.add(isNull(annotationCriteria[0]));               
case 1:
hibernateCriteria.createCriteria(annotationCriteria[0]).add(                Restrictions.isNull(annotationCriteria[annotationCriteria.length - 1]));
case 2:   hibernateCriteria.createCriteria(annotationCriteria[0]).createCriteria(annotationCriteria[1]).add(                Restrictions.isNull(annotationCriteria[annotationCriteria.length - 1]));
    }

I want to remove this “swith”. It is possible to get already added criteria by, I dont now, for example by “name” and that add new subcriteria for it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:15:29+00:00Added an answer on May 28, 2026 at 7:15 am

    I find out more “nice” solution for this issue.
    First of all create some searchCriteria class, for example:

    public class SearchCriteria {
    
        @SearchCriteria(path = "someclass.someclass.id")
        private Integer someId;
    
        public Integer getSomeId() {
            return someId;
        }
    
        public void setSomeId(Integer someId) {
            this.someId= someId;
        }
    }
    

    And custom annotation:

    @Target(value = { ElementType.FIELD })
    @Retention(RUNTIME)
    public @interface SearchCriteria {
        String path() default "";
    
        boolean optional() default true;
    }
    

    And in DAO class:

    @Override
        public Collection<SomeClass> find(SearchCriteria criteria) {
            DetachedCriteria hibernateCriteria = forClass(SomeClass.class);
    
            for (Field field : criteria.getClass().getDeclaredFields()) {
                Annotation annotation = field.getAnnotation(SearchCriteria.class);
    
                if (annotation == null) {
                    continue;
                }
    
                List<String> elements = Arrays.asList(StringUtils.split(((SearchCriteria) annotation).path(), "."));
    
                field.setAccessible(true);
                Object fieldValue = null;
                try {
                    fieldValue = field.get(criteria);
                } catch (IllegalArgumentException e) {
                    LOG.error("while trying to get private field value of entity " + SearchCriteria.class, e);
                } catch (IllegalAccessException e) {
                    LOG.error("while trying to get private field value of entity " + SearchCriteria.class, e);
                }
    
                for (String element : elements) {
                    if (elements.indexOf(element) == elements.size() - 1) {
                        hibernateCriteria = hibernateCriteria.add(eq(element, fieldValue));
                    } else {
                        hibernateCriteria = hibernateCriteria.createCriteria(element);
                    }
                }
            }
    
            return getHibernateTemplate().findByCriteria(hibernateCriteria);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which resides in ROOT. This application has a java class(in
I have a class User with one field called birthDate which is a java.sql.Date
I have some python module, which has a class ModuleClass and I can't modify
I have a java class, for some field (not all field), I will put
Java Code In Java code I have class called IdentificationResult which has 3 members:
I have a java class which fires custom java events. The structure of the
If I have a java class which is package-private (declared with class, not public
I have a MATLAB class which contains a reference to a java object classdef
I have an abstract Class Monitor.java which is subclassed by a Class EmailMonitor.java .
I have two Java classes: B, which extends another class A, as follows :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.