gpt4 book ai didi

java - Objectify 的多态性

转载 作者:行者123 更新时间:2023-12-04 06:32:03 26 4
gpt4 key购买 nike

我正在尝试将 Objectify 与一组从抽象基类继承的类一起使用:

class Container {
@Id Long id;
@Embedded Set<MyAbstract> children = new HashSet<MyAbstract>();
}

@Entity
abstract class MyAbstract {
@Id Long id;
}

@Subclass class Derived1 extends MyAbstract {}
@Subclass class Derived2 extends MyAbstract {}

知道如何为这种情况编码吗?
我收到以下错误:
java.lang.RuntimeException: java.lang.InstantiationException
at com.googlecode.objectify.impl.TypeUtils.newInstance(TypeUtils.java:321)
at com.googlecode.objectify.impl.load.EmbeddedMultivalueSetter.safeSet(EmbeddedMultivalueSetter.java:89)
at com.googlecode.objectify.impl.load.CollisionDetectingSetter.set(CollisionDetectingSetter.java:37)
at com.googlecode.objectify.impl.Transmog.load(Transmog.java:326)
at com.googlecode.objectify.impl.ConcreteEntityMetadata.toObject(ConcreteEntityMetadata.java:212)
at com.googlecode.objectify.impl.QueryImpl$ToObjectIterator.translate(QueryImpl.java:611)
at com.googlecode.objectify.impl.QueryImpl$ToObjectIterator.translate(QueryImpl.java:1)
at com.googlecode.objectify.util.TranslatingIterator.next(TranslatingIterator.java:35)
at com.googlecode.objectify.impl.QueryImpl.get(QueryImpl.java:368)
at com.foodblox.server.model.ObjectifyTest.creation(ObjectifyTest.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.googlecode.objectify.impl.TypeUtils.newInstance(TypeUtils.java:319)
... 33 more

最佳答案

[编辑:此答案已过时。多态适用于 Objectify v5+ 中的嵌入式类]

Objectify 提供的多态性(从 3.0beta2 开始)允许您以与 JPA 类似(但不完全相同)的方式创建多态实体。但是,与 JPA 一样,这种多态性不会扩展到 @Embedded 类。

请求此行为并非完全不合理 - 在问题跟踪器中提交功能请求。

关于java - Objectify 的多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5278717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com