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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:51:41+00:00 2026-05-11T21:51:41+00:00

I need some help figuring out what I’m doing wrong here. I am trying

  • 0

I need some help figuring out what I’m doing wrong here. I am trying
to master one to many relationships and running into a roadblock.
I tried to modify the Employee and ContactInfo example to do one to
many mappings:

Everything works if I create both the parent (employee) and child
(Contact) and then call makePersistent.
But if I try to add a child object to an already persistent parent, I
get a java.lang.ClassCast exception. The full stack trace is at the
bottom of the post.

Here is the code that breaks (If I move the makePersistent() call to
after the add(), everything works fine:

public void testOneToMany(){
      pm = newPM();
      Employee e = new Employee("peter");
      pm.makePersistent(e);
      Contact c = new Contact("123 main");
      List<Contact> contacts = e.getContacts();
      contacts.add(c); // here I get java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
}

Here is the parent class

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {
 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 private Long id;

 @Persistent
 private String name;

 @Persistent(mappedBy="employee")
 private List<Contact> contacts;

 public Employee(String e){
         contacts = new ArrayList<Contact>();
         name = e;
 }

 List<Contact> getContacts(){
         return contacts;
 }

 Long getId(){
         return id;
 }

 public String getName(){
         return name;
 }

}

Here is the child class

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Contact {
 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 private Key key;

 @Persistent
 private String streetAddress;

 @Persistent
 private Employee employee;

 public Contact(String s){
         streetAddress = s;
 }
 public String getAddress(){
         return streetAddress;
 }
 public Employee getEmployee(){
         return employee;
 }

}

Here’s the full stacktrace:

java.lang.ClassCastException: java.lang.Long cannot be cast to
java.lang.String
        at org.datanucleus.store.appengine.DatastoreRelationFieldManager
$1.setObjectViaMapping(DatastoreRelationFieldManager.java:148)
        at org.datanucleus.store.appengine.DatastoreRelationFieldManager
$1.apply(DatastoreRelationFieldManager.java:108)
        at
org.datanucleus.store.appengine.DatastoreRelationFieldManager.storeRelations
(DatastoreRelationFieldManager.java:80)
        at
org.datanucleus.store.appengine.DatastoreFieldManager.storeRelations
(DatastoreFieldManager.java:770)
        at
org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObject
(DatastorePersistenceHandler.java:231)
        at org.datanucleus.state.JDOStateManagerImpl.internalMakePersistent
(JDOStateManagerImpl.java:3067)
        at org.datanucleus.state.JDOStateManagerImpl.makePersistent
(JDOStateManagerImpl.java:3043)
        at org.datanucleus.ObjectManagerImpl.persistObjectInternal
(ObjectManagerImpl.java:1258)
        at org.datanucleus.sco.SCOUtils.validateObjectForWriting
(SCOUtils.java:1365)
        at
org.datanucleus.store.mapped.scostore.ElementContainerStore.validateElementForWriting
(ElementContainerStore.java:401)
        at
org.datanucleus.store.mapped.scostore.FKListStore.validateElementForWriting
(FKListStore.java:764)
        at org.datanucleus.store.mapped.scostore.FKListStore.internalAdd
(FKListStore.java:503)
        at org.datanucleus.store.mapped.scostore.AbstractListStore.add
(AbstractListStore.java:123)
        at org.datanucleus.sco.backed.List.add(List.java:752)
        at com.btg.plyus.scratch.JDOTest.testOneToMany(JDOTest.java:33)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at junit.framework.TestCase.runTest(TestCase.java:168)
        at com.btg.plyus.test.BaseTest.runTest(BaseTest.java:79)
        at junit.framework.TestCase.runBare(TestCase.java:134)
        at junit.framework.TestResult$1.protect(TestResult.java:110)
        at junit.framework.TestResult.runProtected(TestResult.java:128)
        at junit.framework.TestResult.run(TestResult.java:113)
        at junit.framework.TestCase.run(TestCase.java:124)
        at junit.framework.TestSuite.runTest(TestSuite.java:232)
        at junit.framework.TestSuite.run(TestSuite.java:227)
        at org.junit.internal.runners.OldTestClassRunner.run
(OldTestClassRunner.java:76)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
(JUnit4TestReference.java:45)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:460)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:673)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:386)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:196) 
  • 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-11T21:51:42+00:00Added an answer on May 11, 2026 at 9:51 pm

    i dont think you are doing anything wrong. this seems to be a bug with the vendor code. see this link. there isnt a whole lot of info, but they intimate that the PK on your type Long Employee.id is problematic.

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

Sidebar

Ask A Question

Stats

  • Questions 212k
  • Answers 213k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you would need to write a custom method… May 12, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer Yes, you don't need an instance of DataContext, only the… May 12, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer The easiest way is probably to wrap it in another… May 12, 2026 at 10:27 pm

Related Questions

I need some help figuring out what I'm doing wrong here. I am trying
I have this odd problem with a third party script. On our site a
I'm looking to implement the Shunting-yard Algorithm , but I need some help figuring
What I'm trying to do is pass parameters to a user control view which
I have a site that I am currently working on in ASP.NET 2.0 using

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.