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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:32:45+00:00 2026-06-04T05:32:45+00:00

Even after reading the documentation , I seem to have a fundamental misunderstanding about

  • 0

Even after reading the documentation, I seem to have a fundamental misunderstanding about Google App Engine’s entity groups. My goal is a trivial example of ORM: I’ve got some Employees assigned to Departments. An employee can only be assigned to one department, but a department can have many employees. It’s your standard one-to-many relationship.

Given the employee’s key (email) and a department name, I want to look up both the employee and department objects, and if they don’t exist, to create them.

What follows is pseudocode, not meant to compile. If producing code that will compile would help you help me, I’d be happy to do so, but I think my problem is conceptual.

Data Objects:

@Entity
public class Department {
  private Key key;
  private String name;
  // getters and setters
}

@Entity
@NamedQuery(name="getEmployeesInDept", query="SELECT a from Employee a WHERE a.dept=:dept")
public class Employee {
  private Key key;
  private String firstName;
  @ManyToOne
  private Department dept;
  // getters and setters
}

Look Up or Create

Key employeeKey = KeyFactory.createKey("Employee", email);
Employee employee = entityManager.find(Employee.class, employeeKey);
if(employee == null)
{
  Key deptKey = KeyFactory.createKey("Department", deptName);
  Department dept = entityManager.find(Department.class, deptKey);
  if(dept == null)
  {
    dept = new Department();
    dept.setKey(deptKey);
    dept.setName(deptName);
    entityManager.persist(dept);
  }
  employee = new Employee();
  employee.setKey(employeeKey);
  employee.setFirstName(firstName);
  employee.setDept(dept);
  entityManager.persist(employee);
}
entityManager.close();
print("Found employee " + employee.getFirstName() + " from " + dept.getName() + " department!");

That’s the logic that worked perfectly when I was using ye olde generic ORM before I tried migrating to Google App Engine.

However, on GAE, I get an exception like:

javax.persistence.PersistenceException: Detected attempt to establish
Employee(“bob@mycompany.com”) as the parent of Department(14) but the
entity identified by Department(14) has already been persisted without
a parent. A parent cannot be established or changed once an object has
been persisted.

While I understand that in order to get Employee and Department into the same entity group (which I would prefer), I have to make one of them the parent of the other, their relationship isn’t really one that fits into the parent-child paradigm in my mind.

I have tried wrapping various parts between entityManager.getTransaction().begin() and entityManager.getTransaction().end(), but to no avail.

I can get around this by including Department’s key as part of Employee’s key (thus making Department the parent of Employee), but then I have no idea how to look up an Employee based on their email and figure out what department they’re in, or, conversely, how to look up all the employees in a given department.

Does this make sense? How should I structure this relationship in GAE? Surely this is a very common pattern that has a simple solution that is just eluding me.

I’m convinced that there’s some fundamental piece of this puzzle that I’m missing, because it seems rather ridiculous that a simple many-to-one foreign key cannot be easily represented in GAE’s ORM.

Cheers!

  • 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-04T05:32:46+00:00Added an answer on June 4, 2026 at 5:32 am

    So if it doesn’t fit owned relationships then make it unowned, which is supported in v2.x of the GAE JPA plugin.

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

Sidebar

Related Questions

I am having difficulties understanding AsyncTask, even after reading everything about it on Developer.Android.
I'm new to Apache Solr. Even after reading the documentation part, I'm finding it
I have a feeling the answer is no after reading through documentation. However, I
even after reading quite a bit about the strict-aliasing rules I am still confused.
I know next to nothing about RegEx, even after reading a few tutorials :\
I'm not quite sure even after reading the documentation how to do this with
I heard that C# is a rapid application development (RAD) language. Even after reading
I have been stuck at this point for a long time and even after
I'm having a tough time integrating moodle with sqlserver through FREEDTS even after reading
I know there is array_key_exists() but after reading the documentation I'm not really sure

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.