开发者

java.lang.IllegalStateException, "Primary key for object of type Parent is null" except I can print it and its NOT null

I cant imagine any more methods to solve my problem. Here's a thread I made yesterday that describes the parent and child class in detail: https://stackoverflow.com/questions/3800450/many-to-one-unidirectional-gae-jdo-relationship-amongst-entities

I'm trying to add a Truck and persist it, but the persistence manager insists that the primary key of the parent item is NULL, even though I log the value prior to persisting the child, and the parent is NOT null, in fact its the valid key it should be set too. Why is the persistence manager seeing something that I'm not?

try{
        if (!findTruck(truck)){
                _logger.warning("get tweeter key " + truck.getTweeter().getEncodedKey());           
                pm.makePersistent(truck);   
                }
        }
        finally {
            pm.close();             
        }

from the logs:

09-27 06:3开发者_Go百科7PM 12.158
com.google.truxmap.server.TruckJdoDAO addTruck: get tweeter key agp0cnV4bWFwcGVych8LEgdUd2VldGVyIhJrZXltdW5jaGllbWFjaGluZTEM
W 09-27 06:37PM 12.160
com.google.truxmap.server.TruckJdoDAO addTruck: addTruck exception: class java.lang.IllegalStateException. Message:Primary key for object of type Tweeter is null.


perhaps the parent is not persistent state, but is detached ? or transient? why not print out the state of the object


Maybe the parent and the child are managed by different ObjectManager. If you use java, make sure the parent and the child are managed by the same manager (PersistenceManager). The code below would throw this exception.

PersistenceManager pm = JDOHelper.getPersistenceManagerFactory("transactions-optional");
Parent parent = (Parent)pm.getObjectById(Parent.class, parentKey);
pm.close();
// New object manager
pm = JDOHelper.getPersistenceManagerFactory("transactions-optional");
Child child = new Child();
child.setParent(parent);
pm.makePersistent(child); // Exception here
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜