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 7911277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:10:54+00:00 2026-06-03T13:10:54+00:00

I will explain the problem with an example. I have a Vehicle object which

  • 0

I will explain the problem with an example.
I have a Vehicle object which has a model name and a make. When the make is given, relevant vehicle should be listed. when both make and model is given,then suitable vehicles should be listed.

For example:
There is an entity called ‘Vehicle’ .It has a proxy called makeModel whose Entity name is MakeModel . MakeModel has proxies which are ‘make’ and ‘model’ whose entity names are Model and Make.Both Entititiyes ‘Model’ and ‘Make’ have String variable ‘name’.Here are classes

class Vehicle{
 private Integer vehicleId;  
 private MakeModel makeMOdel;
 //getters and setters  
}

class MakeModel {
 private Make make;
 private Model model;  
 //getters and setters  
}  

class Make{
 private Integer mid;
 private String make;
}  
class Model{  
 private Integer mdlid;
 private String modelName;
}

Here is how I tried to get vehicle list. Here, session is Hibernate Session.

Criteria cr= session.createCriteria(Vehicle.class);  

MakeModel mkmd=new MakeModel();  
if( /* if only the make is give */){  
 Make mk=new Make();  
 mk.setMake("Toyota");  
 mkmd.setMake(mk);
}  
else if(if both make and and model given){  
 Make mk=new Make();  
 mk.setMake("Toyota");  
 mkmd.setMake(mk);  
 Model md=new Model("Venus");  
 mkmd.setModel(md);
 }  

cr.createCriteria("makeModel").add(Example.create(mkmd));List l=cr.list();  

But this returns all vehicles.
I want to get all vehicles using hibernate criteria not HQL.

A same question can be found here and referred this also. Any one let me know where is wrong and how to retrieve vehicle list according to given criteria.

  • 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-06-03T13:10:55+00:00Added an answer on June 3, 2026 at 1:10 pm

    Supposing that you are passing to your function makeValue and modelValue, which are the make and the model you want, you would have:

    Criteria cr = session.createCriteria(Vehicle.class);  // Create the criteria on desired class
    cr.createAlis("makeModel", "makeModel")
      .createAlias("makeModel.make", "make")
      .createAlias("makeModel.model", "model");  // Creating aliases makes a join on those tables 
    if (makeValue != null){
        cr.add(Restrictions.eq("make.make", makeValue))  // If you have a makeValue passed, filter by it
    }
    if (makeModel != null){
        cr.add(Restrictions.eq("model.modelName", modelValue));  // If you have a makeModel passed, filter by it
    }
    
    return cr.list();    // Return the list
    

    The result will be a List<Vehicle>. You can add a @SuppressWarnings("unchecked") to your method to avoid seeing unchecked cast warnings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem adding arraylist to list view, will explain about my problem here..
I have a problem and I will try to explain the issue: I have
I have a problem with JPA I will explain in detail: I created a
my problem is easy to explain with an example: I have a 'common' schema
I have a model Client which has many :tours, association in it. I am
I will explain problem with an example: There is two table in my database,
I'll explain my problem with an example. I have an AnimalService, allowing me to
This problem is going to be hard for me to explain but I will
I am trying to reduce some code here. I will explain how I have
My problem: For example: I have a link, when I click on it a

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.