java.lang.IllegalStateException,“Parent 类型的对象的主键为 null”除了我可以打印它并且它不为空

发布于 2024-09-25 07:36:06 字数 1064 浏览 3 评论 0原文

我无法想象还有更多的方法来解决我的问题。这是我昨天制作的一个线程,详细描述了父类和子类: https://stackoverflow.com/questions/3800450/many-to-one-uni Direction-gae-jdo-relationship-amongst-entities

我正在尝试添加一辆卡车并保留它,但是持久性经理坚持认为父项的主键为 NULL,即使我在持久化子项之前记录该值,并且父项不为空,实际上它也是有效的键,也应该设置。为什么持久性管理器看到了我看不到的东西?

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

从日志中:

09-27 06:37PM 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.

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:37PM 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿透光 2024-10-02 07:36:06

也许父级不是持久状态,而是分离的?还是短暂的?为什么不打印出对象的状态

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

任性一次 2024-10-02 07:36:06

也许父级和子级由不同的 ObjectManager 管理。如果您使用 java,请确保父级和子级由同一管理器 (PersistenceManager) 管理。下面的代码会抛出这个异常。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文