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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:10:50+00:00 2026-05-29T11:10:50+00:00

I am working on a domain model using Hibernate for a new project. I

  • 0

I am working on a domain model using Hibernate for a new project. I have an entity that is basically made up of a longName:String, shortName:String and otherNames:Collection<String>.

I have been able to set this up fine, but I now want to work on some DAO features that will allow a user to find an Entity by Example, i.e. I should be able to create an Entity object and set some of the fields, then when I pass this into the search it should return a list of results that match the fields that have been set. I know I can do this using Hibernate Examples.

public List<Entity> findByExample(EntityexampleObject) {
    try {
        Criteria criteria = getSession().createCriteria(type);
        Example example = Example.create(exampleObject).ignoreCase();
        criteria.add(example);

        @SuppressWarnings("unchecked")
        List<Entity> list = criteria.list();
        return list;
    } catch (HibernateException he) {
        return null;
    }
}

But according to my experience and the documentation, Associations are ignored when doing this. So my collection of Strings for otherNames is ignored because it is represented in the database as an association (one-to-many).

Ideally I would like to be able to create an Entity Object and add an otherName to it, then I would be able to find any Entities where one of their Collection of otherNames matches the argument.

I tried doing:

SortedSet<String> names = exampleObject.getNames();
if (names != null && !names.isEmpty()) {
    Criteria newCriteria = criteria.createCriteria("mappedNames");
    newCriteria.add(Restrictions.in("string", exampleObject.getNames()));
}

Where the String literals here represent the column names used in the DB. This works in that it does join the tables and return the results where one of the otherNames in the DB match the criteria. But I get duplicates because if an Entity has two other names and the search criteria is looking for both of them then they show up as two results because the join creates two rows representing the same Entity but with each of the two other names. e.g.:

Entity:
    longName:foo
    shortName:bar
    otherNames:
        a
        b

will result in two rows for the join of:

foo, bar, a
foo, bar, b

So when I do criteria.list it returns two Entity Objects, both of which are the same Object.

Does anyone know of a better way of doing a find by example using Hibernate, where the search actually looks at the association (which in this case is a simple association to a Collection of Strings)?

  • 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-29T11:10:51+00:00Added an answer on May 29, 2026 at 11:10 am

    I think I have found the answer myself, at least to the point that it works for me. I’m still interested in other input, but for now this is what I did:

    criteria.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
    

    this tells it to return distinct entities and in the case of any table joining, only return the root entity.

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

Sidebar

Related Questions

I am working on the domain model for a project. I have a class
I have been working on a new MVC application that utilizies EF4, POCO Domain
I have been working on test framework, which creates a new app domain to
I'm working on a project that uses ADO.NET entity framework as the ORM framework
In a project that I'm working on I have a decision problem and none
I have a domain model that has the concept of an Editor and a
Im new to working with Domain Models so forgive me for asking an elementary
I'm working on creating a domain layer in Zend Framework that is separate from
I'm using the new Silverlight 4 support for IDataErrorInfo. So I have a POCO
I'm working on an event-sourced CQRS implementation, using DDD in the application / domain

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.