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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:08:32+00:00 2026-06-11T11:08:32+00:00

My domain has a Category entity which has a biderectional relationship on itself. Each

  • 0

My domain has a Category entity which has a biderectional relationship on itself. Each category can have a parent and children.

@Entity
public class Category implements DomainObject {

    private Long id;
    private Integer version;
    private String name;
    private Category parent;
    private Set<Category> children;

    @Override
    @Id
    @GeneratedValue
    public final Long getId() {
        return id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(unique=true, nullable=false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne
    public Category getParent() {
        return parent;
    }

    public void setParent(Category parent) {
        this.parent = parent;
    } 

    @OneToMany
    @JoinColumn(name = "parent_id")
    public Set<Category> getChildren() {
        return children;
    }

    public void setChildren(Set<Category> children) {
        this.children = children;
    }
}

I have created the following query to fetch the “root” categories with their direct (level 1) children.

select distinct c from Category c left join fetch c.children where c.parent is null order by c.name

This actually works. My question is: why do I need the “JoinColumn” annotation on getChildren() to make this work and why can’t I just make a “foin fetch” query, without “distinct”? If I remove “distinct” I get a multiplication. For each child of a parent, the entire parent is copied in the result set.

Is there a better way to do this? It just feels… a bit crappy.

  • 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-11T11:08:33+00:00Added an answer on June 11, 2026 at 11:08 am

    In JPA you need to set distinct when you join a OneToMany, otherwise it will return duplicates.
    This is required.

    The JPA spec requires this, but it is an odd default, but relates to what happens in database joins.

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

Sidebar

Related Questions

I have a Grails domain class that is a hierarchy of categories. Each Category
I have 10 top-level domains e.g. example1.com, example2.com, example3.com, etc, and each domain has
I have an ntier solution which has a Domain project for my POCO classes.
I have a url which has some get variables in it like: domain.com/recover?email=my@email.com&token=abc123 inside
I have an MVC3 webapplication which runs as a DOMAIN\USER1 account. This user has
I have a site which has pages like this: blabla.com/page/whatever blabla.com/category/whatever blabla.com/about ... How
I have a domain object that has been auto generated for me by MyGeneration.
I have a ViewModel which encapsulates a datasource which is a domain object. The
My persistence.xml has 2 persistence-units. Each of them has several <class> elements. I thought
Let's say I have two tables: Book and Category . Book has a foreign

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.