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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:52:20+00:00 2026-06-03T20:52:20+00:00

I have a category entity that has a category as parent and a list

  • 0

I have a category entity that has a category as parent and a list of category as child. here’s it’s declaration:

@Entity
@Table(name="CATEGORYENTITY")
public class CategoryEntity extends BaseEntity<Long> {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name="ID")
    private Long id;

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

    @Column(name="VIEWSCOUNT")
    private Integer viewsCount;

    @OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @JoinColumn(name="CATEGORY_ID")
    private List<CategoryEntity> childCategories;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="CATEGORY_ID")
    private CategoryEntity parent;

setters and getters...

It works fine when saving and updating but when try to get all of the categories… for each child returns a parent too!!! I mean if i have a mainCat and this mainCat has 4 child for example, when getting all categories from db, it returns all of the four child and surprisingly 4 mainCat… the weird part is that returned objects are correct and their properties are set correct and 4 mainCat are same!!! what should I do?

I use following code to retrieve results:

public List<E> find(E example, final PagingObject paging, Map<?, ?> filter) throws Exception {
    // create the criteria
    final DetachedCriteria detachedCriteria = createCriteria(filter);
    // add restrictions to the criteria based on example entity
    pruneCriteria(detachedCriteria, example);
    List<E> result = getHibernateTemplate().execute(new HibernateCallback<List<E>>() {
        @SuppressWarnings("unchecked")
        @Override
        public List<E> doInHibernate(Session session) throws HibernateException, SQLException {
            // attach criteria to the session
            Criteria criteria = detachedCriteria.getExecutableCriteria(session);
            // add paging and sort to the criteria
            paginate(criteria, paging);
            List<E> list = criteria.list();
            return list;
        }
    });
    return result;
}
  • 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-03T20:52:22+00:00Added an answer on June 3, 2026 at 8:52 pm

    A bidirectional association always has an owner side, and an inverse side. The owner side is the one where you define how the association is mapped (using @JoinColumn). On the inverse side, you must not define how the association is mapped (it would be redundant). But you must define that it’s the inverse side by using the mappedBy attribute.

    Instead of defining a parent-child bidirectional association, you have defined two different associations.

    The mapping of the collection should be:

    @OneToMany(mappedBy = "parent",
               fetch=FetchType.EAGER, 
               cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    private List<CategoryEntity> childCategories;
    

    Note that setting both sides of such an association as EAGER will force Hibernate to load the whole tree each time you load one single node of the tree. Prepare yourself for a very large number of queries to be executed, and abysmal performance.

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

Sidebar

Related Questions

I have an Item entity that has a ManyToOne relationship to a Category entity.
Let's say I have a product class that has a Category property on it.
I have an entity Product which has relationship category, which is optional, and use
I have category model that has a tree structure. In my database I have
Lets suppose that I have a Category table with a column that holds the
I have a Product admin class. The Product entity has a many-to-one relationship with
I have an entity that maps to a table called Rule. The table for
Say I have two Entities. public class Category{ public virtual int Id{get;set;} public virtual
I have category entity and subcategory entity. I need to get subcategory data to
I have a Product and Category entity in many-to-many association. I am trying to

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.