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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:52:30+00:00 2026-06-09T22:52:30+00:00

I have a database table containing departments. I have another table containing people. As

  • 0

I have a database table containing departments. I have another table containing people. As you’d expect a department contains many people, and a person is in a single department.

When I want to persist a new person to the database, I create a Person object and try to set it’s Department property to an existing Department object which is managed by the Entity Manager. However, when I try to persist my new Person, I get an exception:

A new entity was found through the relationship
‘Entities\Person#department’ that was not configured to cascade
persist operations for entity:
Entities\Department@0000000016abe202000000000d29dd37. To solve this
issue: Either explicitly call EntityManager#persist() on this unknown
entity or configure cascade persist this association in the mapping
for example @ManyToOne(..,cascade={“persist”}).

I don’t fully understand the part of the exception which says the department is an “unknown entity”, since I extracted it via the Entity manager.

As the exception suggests, I inserted a cascade into the yml metadata (cascade: ["persist"]). My person then gets saved, but I end up with a duplicated Department in the departments table, with a new id.

This must be a very common use case. I’ve included my code and metadata below. What changes should I make?

Metadata:

Entities\Person
  type: entity
  table: people
  fields:
    ...
    departmentId:
      type: integer
      unsigned: false
      nullable: false
      column: department_id
    ...
  manyToOne:
    department:
      targetEntity: Entities\Department
      joinColumn: department_id
      referenceColumnName: id

Code:

$department = $em->getRepository('Department')->findOneBy(array('name' => $departmentName);

$person = new Person();
$person->setName('Joe Bloggs');
$person->setDepartment($department);

$em->persist($person);
$em->flush();
  • 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-09T22:52:32+00:00Added an answer on June 9, 2026 at 10:52 pm

    The problem was caused by using different instances of the Entity Manager to first fetch the department, and then persist the Person.

    My entity manager is now a singleton, so whichever class requests an entity manager gets the same instance.

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

Sidebar

Related Questions

I have a database containing a single huge table. At the moment a query
I have a table containing persons information (one row per person) and another table
I have a table containing access logs. I want to know how many accesses
I have a SQL database containging a Contants table containing people contacts records. Im
I have a database containing one table, i want to generate CSV file with
I have a database table containing dates (`date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00').
I have a database table containing the news on my site. It's divided into
In my database I have table AnimalDetails containing animalNumber(primary key),name,breed dateofbirth and an image
I have a java class containing all the columns of a database table as
Suppose I have an Oracle 11.2 database containing the following table: TABLE: SURVEY PARAMETER

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.