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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:51:06+00:00 2026-05-27T11:51:06+00:00

I have a person object, and a computer object, this person object has a

  • 0

I have a person object, and a computer object,
this person object has a field named computer,
one person can have one computer, one-to-one relation.

class Person{

    @OneToOne(cascade=CascadeType.ALL)//in which situations this is true?  
    @JoinColumn(name = "computer_id",nullable=false) 
    private Computer computer;

    getComputer(){...}
    setComputer(Computer c){...}

    @Id
    private String id;
}

class Computer{

    @Id
    private String id;

}

Let me assume I have a computer object on DB, and I get it with a load method.

Computer computer=entityManager.loadById({an id});
Person p=new Person();
p.setComputer(computer);

Now if I want to save both of computer and person what should I do?

entityManager.persist(p);

I can not generate same error but a while ago I got a “detached entity passed to persist” error.

what is the most appropriate way to save everything into DB without repeatings?

Also can you advise a resource (a book maybe) to understand what to do in different situations.

  • 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-27T11:51:07+00:00Added an answer on May 27, 2026 at 11:51 am

    You don’t need any cascade in the case you describe, and persisting the user is sufficient:

    • since the computer has been loaded using the EM, it’s attached to the EM. Any change done on the computer will be automatically saved to the database without any operation needed
    • the user is a new entity, which is not persistent yet. To make it persistent, you must call persist. After this call, it will be attached to the EM, and any change done on the user will also be automatically saved to the database without any operation needed

    The error “detached entity passed to persist” could happen if you created a new computer, then a new user, set the computer to the user, and persist the user. In this case, the EM complaions because you try to make persistent en entity (user) which refers to another entity (computer) which is not persistent. There are two solutions:

    • make the computer persistent before making the user persistent, by calling persist with the computer
    • annotate the person.computer association with cascade=PERSIST or cascade=ALL, so that the computer is made persistent autoamatically when the user is made persistent. Note that ALL also includes REMOVE. So if you remove a user, its computer will also be removed.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a model object 'Person' defined, which has a field called 'Name'.
I have an IEnumerable<Person> object. The Person class has a Designation property. I want
I have a Person object with two constructors - one takes an int (personId),
Let's say I have a WCF service which has a method returning object Person.
I have a computer vision set up with two cameras. One of this cameras
Say, I have a Person object list called people: public class Person { public
Think 2 entities OneToOne mapped. Person and Car. A Person can have a Car.
Okay here is the scenario! I have a Person object which has an Address
This is sorta' a two-part question. I have a person object with a char
I have a Person object with a lastName field. The lastName field cannot be

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.