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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:37:59+00:00 2026-06-18T04:37:59+00:00

I’m trying to create simple select query with HQL. There is an Entity that

  • 0

I’m trying to create simple select query with HQL.
There is an Entity that will be used in query. It Looks like the following:

@Entity
@Table(name = "my_table")
public class MyTable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "test_id")
    private Long testId;

    @Column(name = "parent_id")
    private Long parentId;

    @OneToMany(mappedBy = "parentId", 
               fetch = FetchType.EAGER, 
               cascade = CascadeType.ALL)
    private Set<MyTable> children = new HashSet<MyTable>();

    //getters and setters
}

Hierarchy is simple. There are Parents (which have null parent_id value) and their children. So two levels only.

I would like to create query that selects all parents and their children, but there is a condition for children: it should equal to specific test_id. E.g. it is required to have children only with test_id = 1. Table consists of Parent1 with Child1 (test_id = 2) and Parent2 with Child2 (test_id = 1). The query result should be Parent1 without children and Parent2 with Child2.

Query:

from MyTable as myTable left fetch join myTable.children as child 
where child.testId = 1

As a result – I’m getting only those parents which have children with “1” test_id. But I need all parents to see even if there no needed children.
What is wrong here: mapping or query? And how should it actually be?

Thanks in advance.

  • 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-18T04:38:01+00:00Added an answer on June 18, 2026 at 4:38 am

    Your mapping is wrong. You have a unidirectional OneToMany association. SO you obviously must tell Hibernate how this association is mapped. And what you tell is:

    @OneToMany(mappedBy = "parentId", ...)
    

    This means: “Go look at the other side of this bidirectional association to find how this association is mapped. I’m only the inverse side”.

    But there is no other side. Your association is unidirectional.

    The mapping should be:

    @Entity
    @Table(name = "my_table")
    public class MyTable {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id")
        private Long id;
    
        @Column(name = "name")
        private String name;
    
        @Column(name = "test_id")
        private Long testId;
    
        @OneToMany(fetch = FetchType.EAGER, 
                   cascade = CascadeType.ALL)
        @JoinColumn(name = "parentId")
        private Set<MyTable> children = new HashSet<MyTable>();
    
        //getters and setters
    }
    

    Note that the parentId field has been removed, since it’s already used to map the children association.

    Another problem is your expectations. A query returns columns, or it returns entities. If it returns a parent entity and you ask this entity for its children, all the children will be returned. AN entity represents what is in the database. It doesn’t represent the result of a specific query.

    What you could do, if you only want to have some of the children, is to search for children, and get their parent. To do that, you would need to make the association bidirectional:

    @Entity
    @Table(name = "my_table")
    public class MyTable {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id")
        private Long id;
    
        @Column(name = "name")
        private String name;
    
        @Column(name = "test_id")
        private Long testId;
    
        @ManyToOne
        @JoinColumn(name="parentId")
        private MyTable parent;
    
        @OneToMany(mappedBy="parent",
                   fetch = FetchType.EAGER, 
                   cascade = CascadeType.ALL)
        @JoinColumn(name = "parentId")
        private Set<MyTable> children = new HashSet<MyTable>();
    
        //getters and setters
    }
    

    And the query would be:

    select child from MyTable child 
    left join fetch child.parent
    where child.testId = 1
    

    This would return all the children with testId = 1, along with their parent. You would need a second query to get all the other parents. Something like

    select parent from MyTable parent 
    where parent.id not in (
        select parent2.id from MyTable child   
        left join child.parent parent2
        where child.testId = 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.