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

  • Home
  • SEARCH
  • 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 8184207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:25:30+00:00 2026-06-07T01:25:30+00:00

So I have a Users table, Projects table and User_Roles table. I have one

  • 0

So I have a Users table, Projects table and User_Roles table. I have one users which is connected to 2 projects, but when I have just one role for him it returns those 2 projects, but if I have 2 roles for him it returns 4 roles (2×2 projects). How do I prevent this

This is my code for returning the list of projects for user

@Override
public List<Project> retrieve(User user) {
    Criteria criteria = super.createCriteria();
    criteria.addOrder(Order.desc("date"));
    criteria.createCriteria("users").add(Restrictions.eq("id", user.getId()));

    return (List<Project>) criteria.list();
}

mappings in User class

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "Users_Projects",
           joinColumns = @JoinColumn(name = "UserID"), inverseJoinColumns =   @JoinColumn(name = "ProjectID"))
public List<Project> getProjects() {
    return projects;
} 

@ElementCollection(fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
@Column(name = "RoleType")
@JoinTable(name = "User_Roles", joinColumns = @JoinColumn(name = "UserID"))
public Set<Role> getRoles() {
    return roles;
}

Any suggestions what is the problem here?

tnx

  • 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-07T01:25:32+00:00Added an answer on June 7, 2026 at 1:25 am

    The classic “fix” for issues of duplicate rows from criteria queries, which will likely work as long as you’re not trying to combine it with pagination, is to add

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    

    I believe this is happening in your situation because of the eager loading of the user roles. Changing that to be lazy might also work, and continue to work if you need pagination.

    Setting fetch = FetchType.EAGER instructs hibernate that you want to always load all the roles for the user whenever you retrieve a user, which it accomplishes by always doing a join to the roles table.

    Unfortunately, that sql leads to multiple records with the same user (one for each role), and it then has to be straightened out in Java after the sql is run, which is what the result transformer does.

    Much more unfortunately, if you want to use paging in the most natural way with simple hibernate criteria, using criteria.setFirstResult and criteria.setMaxResults, things happen in the wrong order, so these queries that need the result transformer to avoid duplicates just don’t page right.

    To understand all the hibernate sql generation a bit better, I’d advise turning on logging of the generated sql as described in this answer to another SO hibernate question.

    • 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 has a Projects table and a Users table. In
I have a Projects table and a Users table which are linked by a
I have tables: users {id, name} projects {id, name} roles {id, name} projects_users {id,
I have an application which has projects and users . I don't care about
In many projects I have to make a table containing users and implementing standard
I have a table of projects belonging to various users: project_id, owner_user_id, project_name I
I have two tables, users & projects users table: id username 1 sam 2
I have users table and want to SELECT some rows by bitmask criteria. I'll
I have users table. There are three other tables: developers, managers, testers. All of
i have users table and i have posts table i want select from users

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.