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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:23:32+00:00 2026-06-05T05:23:32+00:00

I have an object CategoryBean where are defined a list of other objects CategoryBean

  • 0

I have an object CategoryBean where are defined a list of other objects CategoryBean (its children) and another object CategoryBean (its parent).

Cloning its children is not a problem, but cloning its parent isn’t working. Indeed, when I have my object A and its parent P, when I go through P and get the list of its children (where I should fine A), A has not the same reference than the child of P. The objects equal (“attributly” speaking) but are not the same.

Here is my class and its attributes :

public class CategoryBean implements Externalizable, Cloneable {
    public static final boolean ACTIVE = true;
    public static final boolean INACTIVE = false;

    public static final int VALIDATED = 1;
    public static final int NON_OBSERVED = 2;
    public static final int IN_PROGRESS = 3;
    public static final int PARTIEL = 4;

    private String perimetre;
    private CategoryBean parentCategory;
    private List<CategoryBean> categoryList = new LinkedList<CategoryBean>();
    protected String title;
    /**
     * Category validée ou non
     */
    protected int state = NON_OBSERVED;
    /**
     * Category active ou inactive
     */
    protected boolean activated = ACTIVE;

    /**
     * @return the parentCategory
     */
    public CategoryBean getParentCategory() {
        return parentCategory;
    }

    /**
     * @param parentCategory
     *            the parentCategory to set
     */
    public void setParentCategory(CategoryBean parentCategory) {
        this.parentCategory = parentCategory;
    }

    /**
     * @return the perimetre
     */
    public String getPerimetre() {
        return perimetre != null ? perimetre.trim() : null;
    }

    /**
     * @param perimetre
     *            the perimetre to set
     */
    public void setPerimetre(String perimetre) {
        this.perimetre = perimetre != null ? perimetre.trim() : null;
    }

    /**
     * @return the category
     */
    public List<CategoryBean> getCategoryList() {
        return categoryList;
    }

    /**
     * @param category
     *            the category to set
     */
    public void setCategoryList(List<CategoryBean> categoryList) {
        this.categoryList = categoryList;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title != null ? title.trim() : null;
    }

    /**
     * @param title
     *            the title to set
     */
    public void setTitle(String title) {
        this.title = title != null ? title.trim() : null;
    }

    /**
     * @return the state
     */
    public int getState() {
        return state;
    }

    /**
     * @param state
     *            the state to set
     */
    public void setState(int state) {
        this.state = state;
    }

    /**
     * @return the activated
     */
    public boolean isActivated() {
        return activated;
    }

    /**
     * @param activated
     *            the activated to set
     */
    public void setActivated(boolean activated) {
        this.activated = activated;
    }

    @Override
    public int hashCode() {
        if (parentCategory != null && categoryList != null && title != null) {
            return parentCategory.hashCode() + categoryList.hashCode() + title.hashCode();
        } else if (categoryList != null && title != null) {
            return parentCategory.hashCode() + categoryList.hashCode() + title.hashCode();
        } else {
            return super.hashCode();
        }
    }

    @Override
    public boolean equals(Object o) {
        if (o != null && o instanceof CategoryBean) {
            CategoryBean o2 = (CategoryBean) o;
            if (getPerimetre() != null && getTitle() != null && getParentCategory() != null) {
                return getPerimetre().equals(o2.getPerimetre()) && getTitle().equals(o2.getTitle())
                        && getParentCategory().equals(o2.getParentCategory());
            } else if (getPerimetre() != null && getTitle() != null && getPerimetre().equals(getTitle())) {
                return getPerimetre().equals(o2.getPerimetre()) && getTitle().equals(o2.getTitle());
            }
        }
        return super.equals(o);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException {
        setPerimetre((String) input.readObject());
        setParentCategory((CategoryBean) input.readObject());
        setCategoryList((List<CategoryBean>) input.readObject());
        setTitle((String) input.readObject());
        setState((Integer) input.readObject());
        setActivated((Boolean) input.readObject());
    }

    @Override
    public void writeExternal(ObjectOutput output) throws IOException {
        output.writeObject(getPerimetre());
        output.writeObject(getParentCategory());
        output.writeObject(getCategoryList());
        output.writeObject(getTitle());
        output.writeObject(getState());
        output.writeObject(isActivated());
    }

    @Override
    public CategoryBean clone() throws CloneNotSupportedException {
        try {
            CategoryBean clone = (CategoryBean) super.clone();
            clone.setPerimetre(getPerimetre());
            clone.setParentCategory(getParentCategory());

            List<CategoryBean> categoryListClone = null;
            if (getCategoryList() != null) {
                categoryListClone = new LinkedList<CategoryBean>();
                for (int i = 0; i < getCategoryList().size(); i++) {
                    categoryListClone.add(getCategoryList().get(i).clone());
                }
            }
            clone.setCategoryList(categoryListClone);

            clone.setTitle(getTitle());
            clone.setState(getState());
            clone.setActivated(isActivated());
            return clone;
        } catch (CloneNotSupportedException e) {
            throw new InternalError();
        }
    }
}

Do you know what I do wrong in the clone method ?

Thanks for you help 🙂

(Edit) Solution :

public CategoryBean clone() throws CloneNotSupportedException {
    try {
        CategoryBean clone = (CategoryBean) super.clone();
        clone.setPerimetre(getPerimetre());

        List<CategoryBean> categoryListClone = null;
        if (getCategoryList() != null) {
            categoryListClone = new LinkedList<CategoryBean>();
            for (int i = 0; i < getCategoryList().size(); i++) {
                CategoryBean childClone = getCategoryList().get(i).clone();
                childClone.setParentCategory(clone);
                categoryListClone.add(childClone);
            }
        }
        clone.setCategoryList(categoryListClone);

        clone.setTitle(getTitle());
        clone.setState(getState());
        clone.setActivated(isActivated());
        return clone;
    } catch (CloneNotSupportedException e) {
        throw new InternalError();
    }
}
  • 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-05T05:23:33+00:00Added an answer on June 5, 2026 at 5:23 am

    I think you need to cache the parent beans already cloned during the clone operation, so that when clone.setParentCategory is called in the child, this is the cloned parent that is injected in the property…

    Not sure it’s crystal clear :)… so… something like this (careful, no tested !) :

    @Override
    public CategoryBean clone() throws CloneNotSupportedException {
        return cloneRecursive(new HashMap<CategoryBean, CategoryBean>());    
    }
    
    private CategoryBean cloneRecursive(Map<CategoryBean, CategoryBean> categoryBeans) throws CloneNotSupportedException {
        try {
            CategoryBean clone = (CategoryBean) super.clone();
            categoryBeans.put(this, clone);
            clone.setPerimetre(getPerimetre());
            clone.setParentCategory(categoryBeans.get(getParentCategory()));
    
            List<CategoryBean> categoryListClone = null;
            if (getCategoryList() != null) {
                categoryListClone = new LinkedList<CategoryBean>();
                for (int i = 0; i < getCategoryList().size(); i++) {
                    categoryListClone.add(getCategoryList().get(i).cloneRecursive(categoryBeans));
                }
            }
            clone.setCategoryList(categoryListClone);
    
            clone.setTitle(getTitle());
            clone.setState(getState());
            clone.setActivated(isActivated());
            return clone;
        } catch (CloneNotSupportedException e) {
            throw new InternalError();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that holds several value types and other objects. I want
Assuming I have object_list list which contains objects. I want to check if my
I have this before the process: protected void onPostExecute(SortedSet<RatedMessage> result) { List<Object> list=Arrays.asList(result.toArray()); lancon.putExtra(results,
I have object of type WebClient which successfully connects to web server. But when
Application use NHibernate. I Have object A that contains set of objects B. I
But I'm wondering what the naming conventions are for this? Like.. I have object
I have been looking at the ruby-docs but have not been able to make
I have object with relation, and want save it, but I have only id
Suppose I have a design like this: Object GUI has two objects: object aManager
I have defined book to be an open graph object in my Facebook app.

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.