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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:25:14+00:00 2026-05-15T05:25:14+00:00

I have a one-to-many relationship between Part and Params (a Part has many Params).

  • 0

I have a one-to-many relationship between Part and Params (a “Part” has many “Params).

I’m trying to do something naive like this:

Part sourcePart = em.find(Part.class, partIdSource);
Part destPart = em.find(Part.class, partIdDest);

Collection<Param> paramListSource = sourcePart.getParamList();

destPart.setParamList(paramListSource);

Basically I want to copy all the parameters from sourcePart to destPart. Hopefully the persistence provider will automatically set the right foreign keys in the Param table/entity.

The above code will obviously not work.

Is there any easy way of doing this, or do I have to do create a new collection, then add each Param (creating new Param, setting attributes, etc) ?

Edit

I tried detaching the entity first like someone recommended in another thread but I get an exception (org.hibernate.PersistentObjectException: detached entity passed to persist: shared.entity.Param).

Part sourcePart = em.find(Part.class, partIdSource);
// force eager loading...
((List)sourcePart.getParamList()).get(0);

Part destPart = em.find(Part.class, partIdDest);

// detach entity
org.hibernate.Session session = ((org.hibernate.ejb.EntityManagerImpl) em.getDelegate()).getSession();
session.evict(sourcePart);

//causes exception "detached entity passed to persist"
destPart.setParamList(sourcePart.getParamList());
  • 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-05-15T05:25:15+00:00Added an answer on May 15, 2026 at 5:25 am

    Basically I want to copy all the parameters from sourcePart to destPart. Hopefully the persistence provider will automatically set the right foreign keys in the Param table/entity.

    If you want to go the “evict way”, making your entities Detached is NOT enough, you need to “nullify” the Id properties so that your entities become New again (persisting them must result in SQL INSERT). In pseudo code:

    MyObject ob = session.get(idOfExisting);
    hibernate.evict(ob);
    ob.setId(null);
    hibernate.save(ob);
    

    But I find this dirty. I would iterate over the items of the list and use a copy constructor (without copying the Id properties) to create a new list and then set the copy on the destination object (and this would be provider agnostic).

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

Sidebar

Related Questions

I have a one-to-many relationship between two classes for this situation. I have a
Please see this pic: I have a one to many relationship between eg. Person
I have a one-to-many relationship between Foo and Bar and I cannot perform an
android noob... I have two tables, with a one to many relationship between country_tbl
I have a Hash Map (many-to-one relationship between texts and boolean values): name flag
In this test Core Data project, I have a one to many relationship from
I have a simple one-to-many relationship. I would like to select rows from the
I have an NSManagedObject who has a one to many relationship with the parent
I have a Type MenuItem that has one-to-many relationship with itself through Children property.
I'm using Entity Framework 4 and have a one-to-many relationship between a parent and

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.