google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

datastore.merge() and datastore.delete(EntityObject) both fail #346

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have an object called Person, 

class Person {
  @Id String id;
  String firstName;
  String middleName;
  String lastName;
}

The only working way to perform a delete is by this
Person p = new Person("John", "Little", "Doe");
ObjectId objId = new ObjectId(p.getId());
datastore.delete(Person.class, objId);

// this does not work!!!!
datastore.delete(p);

now let's say I update the person
p.setMiddleName("Big");

datastore.merge(p); // --> Returns an exception

What version are you using? (Morphia/Driver/MongoDB)
Morphia 0.99
MongoDB Java Driver 2.7.2 
MongoDB Server version 2.0.1x64

Please include a stack trace below:
FAILED: testClinicPersistence
com.google.code.morphia.query.UpdateException: Not updated: { "serverUsed" : 
"127.0.0.1:27017" , "updatedExisting" : false , "n" : 0 , "connectionId" : 3 , 
"wtime" : 0 , "err" :  null  , "ok" : 1.0}
    at com.google.code.morphia.DatastoreImpl.merge(DatastoreImpl.java:890)
    at com.google.code.morphia.DatastoreImpl.merge(DatastoreImpl.java:870)
    at org.webpatterns.platform.nosql.mongogdb.core.MongoStore.merge(MongoStore.java:108)
    at org.webpatterns.platform.nosql.mongodb.test.DomainClassesTest.testPersistence(DomainClassesTest.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:691)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:883)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1208)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:758)
    at org.testng.TestRunner.run(TestRunner.java:613)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1141)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1066)
    at org.testng.TestNG.run(TestNG.java:978)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)

Can anybody assist?

Original issue reported on code.google.com by fady.ma...@gmail.com on 28 Nov 2011 at 12:42

GoogleCodeExporter commented 9 years ago
An ObjectId != String.

You should declare your class with @Id ObjectId id

Original comment by scotthernandez on 28 Nov 2011 at 1:41

GoogleCodeExporter commented 9 years ago
This is pure nonsence, why not abstract the ids then into a super class such as 
MorphiaEntity and all of them inherit the id from it? 

Original comment by fady.ma...@gmail.com on 28 Nov 2011 at 2:40

GoogleCodeExporter commented 9 years ago
You can do whatever you like. 

Not everyone uses the same type for @Id in every entity. You are making a lot 
of assumptions about use patterns here.

The @Id field is just a unique (and immutable) field which used by the system 
(mongodb). It really only is special because there is an index created for it 
by default and if you don't provide a value then an ObjectId will be assigned 
to it.

Original comment by scotthernandez on 28 Nov 2011 at 2:46

GoogleCodeExporter commented 9 years ago
The issue is not the id, the issue is with the broken API. Even with that 
approach:

datastore.delete(entity) // won't work,
datastore.merge(entity)  // won't work either

What's the usecase of those classes? 

Original comment by fady.ma...@gmail.com on 28 Nov 2011 at 2:54

GoogleCodeExporter commented 9 years ago
The issue is not the id, the issue is with the broken API. Even with that 
approach:

datastore.delete(entity) // won't work,
datastore.merge(entity)  // won't work either

What's the usecase of those classes? 

Original comment by fady.ma...@gmail.com on 28 Nov 2011 at 2:54