代码之家  ›  专栏  ›  技术社区  ›  Scyla101

EclipseLink:以MongoDB作为持久层的继承

  •  0
  • Scyla101  · 技术社区  · 6 年前
    实体 @字段(name=“_id”) 实体 @XML根元素 公共类扩展类扩展AbstractClass{ 私有字符串名称; } this.name=名称;

    以下是我正在使用的文件: persistence.xml

    <persistence version=“2.1”xml ns=“http://xmlns.jcp.org/xml/ns/persistence”xmlns:xsi=“http://www.w3.org/2001/xmlschema-instance”xsi:schemalocation=“http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence\u 2_1.xsd”> <class>org.example.webapp.resources.test.extendedClass</class> <property name=“eclipselink.target database”value=“org.eclipse.persistence.nosql.adapters.mongo.mongoplatform”/>gt; <property name=“eclipselink.nosql.property.mongo.host”value=“IP地址”/>
    
    @路径(“持久/扩展对象”)
    公共字符串addobject(@queryparam(“name”)final string name){
    
    
    extendedClass theObject=new extendedClass(id,name);
    
    
    
    
    
    em.close();
    
    }
    
    
    
    
    
    @生成(mediatype.application)
    
    
    entityManager em=emf.createntityManager();
    
    
    list<extendedClass>extendedClassList=em.createQuery(“从extendedClass A中选择a”,extendedClass.class).getResultList();
    
    em.close();
    
    }
    
    

    这将产生以下异常:

    调用:执行mappedinteraction()。 properties=>mongo.operation=find,mongo.collection=abstractClass_ $and=>[数据库记录( 在Org.Eclipse中.持久性.eIS.EISExtExtReaveExtExebug(EISExtExist.java:65) 在Org.Eclipse中.持久性.eIS.EISExtExtReaveExtExebug(EISExtExist.java:78) 在Org.Eclipse中。 在Org.Eclipse中。持久性。查询。ObjutalRealQueQuest.ExcExtDeabaseAdQuy(ObjutalRealQualQuest.java:1155) 在Org.Eclipse中。持久性.Qual.RealAlqQue.Excel(RealAlqQue.java:402) 在Org.Eclipse中.Primult.Neal.Studio.UnWorkFiffiL.IntualExcExtQueQuy(UNITOFWorkIMP.java:2894) 在Org.Eclipse中.Primest.jp.QueIIMP.GETReultList.(QuyIMP.java:468) 在Sun.Real.nTyvEndoDealOrthIMP.LoCKE(NATEVEMEDAccess ORIL .java:62) 在Java方法调用(方法:Java 498) 在Org.gassFiels.Sersi.Melo.My.ActudioJavaRealCysMeDeDeCorter 1美元.运行(抽象JavaRealCyMeDoDebug). Java:144) 在Org.GasasFISH.Serv.Mulk.My.javaRealCyMeDeDebug Services $ Type OutoPo.Ko.DoDebug(JavaRealthMyDoDebug Services提供程序. Java:247) 在Org.GasasFiels.Sersi.Serv.MultReMyDoDeCork.Apple(ReavigyDoDeCork.java:102) 在GrasFISH。泽西。内部。错误1美元。调用(错误。Java:271) 在GrasFISH。泽西。内部。错误。过程(错误。Java:315) 在GrasFISH服务器上,Server RunTime.进程(ServRunTime.java:316) 在Org.GalasFISH.JeSe.servlet .WebCypultEclipse(WebCyp.java:418) 在Org.GalasFiels.Jelsi.servlet .Servlet容器. Service(Servlet容器. Java:389) 在Org.Apache .Tomcat .WebSoC.Server .WS滤镜.DoFulter(WSField.java:52) 在Org.Apache .Calalina .Cype .FieldFieldCork.DoFulter(Apple)FieldStork.java:206) 在Org.Acache .Calalina .Cort.Stand AdExtCort.Cube(Stand AdvutoTork.java:106) 在Org.Acache .Calalina .Corn.StaldHoistPort.CuoKe(StaldHistort.java:142) 在Org.Apache .Calalina .Cudio.Access Access阀门.调用(ActudiAccess LogPort.java:610) 在Org.Apache .Calalina .Cornor.CyoToAddio.Service(CyoToAddio.java:537) 在Org.Apache .Tomcat .UTL.NET.NooEndotSoCoCompultor.Dorun(NooEndoPo.java:1566) 由于:javax.resource.resourceexception:org.bson.codecs.configuration.codecconfigurationexception:找不到类org.eclipse.persistence.internal.helper.databasefield的编解码器。 在COM.MangGDB .DBOBJCTCODEC .RealValueUE(DBOBJCTCODEC . Java:231) 在COM.MangGDB .DBOBJCTCODEC .RealValueUE(DBOBJCTCODEC . Java:219) 在COM.MangGDB .DBOBJCTCODEC .EnCODE(DBOBJCTCODEC,Java:65) 在Org.BSO.CODES. BSONDCONTRON WRAPPARCODEC .EnCODE(BSONCONTRON WRAPPARCODEC . Java:29) 在Org.BSO.CODES. BSONDCONTCONDECODEC .RealValueE(BSONDCONTROCONDECOD.JAVA:136) 在B.SON.BSON.编解码器. BSONDCONTENCODEC .EnCODE(BSONDCONMENTCONDEC . Java:41) 在COM.MangGDB连接.RealStestMulk.AddioDebug(ReavestMeava. Java:188) 在COM.MangGDB .CONTION.CurdPrimuloIMP.Exctudio(CurdPrimuloIMP.java:70) 在COM.MangGDB。 在COM.MangGDB .Client .No.MGOcliclient委托$CuffATEORATION执行器.执行(Mungclient委托. Java:140) 在COM.MangGDB .Client .No.MangoTeAbabl IMPL迭代器(MangoTracabliIMP.java:86) ……71个以上

    有人知道为什么会发生这个错误吗?根据EclipseLink的文档,当使用NoSQL数据库时,它应该能够很好地处理继承:https://wiki.eclipse.org/eclipseLink/faq/nosql

    我还测试了一个没有继承的版本,在那里提取WebService工作得很好。

    一如既往地,我们感谢您的帮助。

    实体 @nosql(dataformat=dataformattype.mapped) @xmlAccessorType(xmlAccessType.Field) @身份证 受保护的字符串ID; }

    @Entity
    @NoSql(dataFormat=DataFormatType.MAPPED)
    public abstract class AbstractClass {
    
        @Id
        @Field(name="_id")
        protected String id;
    
        public String getId() {
    
            return this.id;
        }
    }
    

    @Entity
    @NoSql(dataFormat=DataFormatType.MAPPED)
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class ExtendedClass extends AbstractClass {
    
        @Basic
        private String name;
    
        public ExtendedClass() {
    
        }
    
        public ExtendedClass(String id, String name) {
    
            this.id = id;
            this.name = name;
        }
    
        public String getName() {
    
            return this.name;
        }
    }
    

    persistence.xml我使用的文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
        <persistence-unit name="persistence-unit" transaction-type="RESOURCE_LOCAL">
            <class>org.example.webapp.resources.test.AbstractClass</class>
            <class>org.example.webapp.resources.test.ExtendedClass</class>
            <properties>
                <property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/>                         
                <property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/>
                <property name="eclipselink.nosql.property.mongo.port" value="27017"/>
                <property name="eclipselink.nosql.property.mongo.host" value="ip-address"/>
                <property name="eclipselink.nosql.property.mongo.db" value="database-name"/>
                <property name="eclipselink.logging.level" value="FINEST"/>
            </properties>
        </persistence-unit>
    </persistence>
    

    ExtendedClass以下内容:

    @PUT
    @Path("persist/extended-object")
    @Consumes(MediaType.APPLICATION_JSON)
    public String addObject(@QueryParam("name") final String name) {
    
        final String id = UUID.randomUUID().toString();
    
        ExtendedClass theObject = new ExtendedClass(id, name);
    
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence-unit");
    
        EntityManager em = emf.createEntityManager();
    
        em.getTransaction().begin();
    
        em.persist(theObject);
    
        em.getTransaction().commit();
        em.close();
    
        return "Persisted object with name: " + name;
    }
    

    http://localhost:8080/webapp/webapi/test/persist/extended-object?name=test-abstraction

    persisted object

    到现在为止,一直都还不错。但是,如果我试图获取包含所有持久化对象的列表,我会遇到一个问题。下面是WebService示例:

    @GET
    @Path("get/all")
    @Produces(MediaType.APPLICATION_JSON)
    public List<ExtendedClass> getAllExtendedClasses() {
    
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence-unit");
    
        EntityManager em = emf.createEntityManager();
    
        em.getTransaction().begin();
    
        List<ExtendedClass> extendedClassList = em.createQuery("SELECT a FROM ExtendedClass a", ExtendedClass.class).getResultList();
    
        em.getTransaction().commit();
        em.close();
    
        return extendedClassList;
    }   
    

    javax.resource.ResourceException: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.eclipse.persistence.internal.helper.DatabaseField.

    完整堆栈跟踪:

    Exception [EclipseLink-90000] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.eis.EISException
    Internal Exception: javax.resource.ResourceException: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.eclipse.persistence.internal.helper.DatabaseField.
    Error Code: 000
    Call: Executing MappedInteraction()
        spec => null
        properties => {mongo.operation=FIND, mongo.collection=ABSTRACTCLASS}
        input => [DatabaseRecord(
        $and => [DatabaseRecord(
        EXTENDEDCLASS._id => ABSTRACTCLASS._id), DatabaseRecord(
        ABSTRACTCLASS.DTYPE => ExtendedClass)])]
    Query: ReadAllQuery(referenceClass=ExtendedClass jpql="SELECT a FROM ExtendedClass a")
        at org.eclipse.persistence.eis.EISException.resourceException(EISException.java:65)
        at org.eclipse.persistence.eis.EISException.resourceException(EISException.java:74)
        at org.eclipse.persistence.eis.EISException.resourceException(EISException.java:78)
        at org.eclipse.persistence.eis.EISAccessor.basicExecuteCall(EISAccessor.java:212)
        at org.eclipse.persistence.eis.EISAccessor.executeCall(EISAccessor.java:112)
        at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1995)
        at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570)
        at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
        at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
        at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:299)
        at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:694)
        at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2714)
        at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2667)
        at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:477)
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1155)
        at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1114)
        at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:402)
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1202)
        at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2894)
        at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1797)
        at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1779)
        at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1744)
        at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
        at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:468)
        at org.example.webapp.resources.test.ObjectWebServices.getAllExtendedClasses(ObjectWebServices.java:54)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:74)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:247)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:388)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:346)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
        at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:337)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:280)
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:316)
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1084)
        at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:418)
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
        at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: javax.resource.ResourceException: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.eclipse.persistence.internal.helper.DatabaseField.
        at org.eclipse.persistence.internal.nosql.adapters.mongo.MongoDatabaseInteraction.execute(MongoDatabaseInteraction.java:201)
        at org.eclipse.persistence.eis.EISAccessor.basicExecuteCall(EISAccessor.java:163)
        ... 70 more
    Caused by: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.eclipse.persistence.internal.helper.DatabaseField.
        at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
        at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
        at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51)
        at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:231)
        at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:241)
        at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:217)
        at com.mongodb.DBObjectCodec.encodeIterable(DBObjectCodec.java:292)
        at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:219)
        at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:149)
        at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:65)
        at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
        at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
        at org.bson.codecs.EncoderContext.encodeWithChildContext(EncoderContext.java:91)
        at org.bson.codecs.BsonDocumentCodec.writeValue(BsonDocumentCodec.java:136)
        at org.bson.codecs.BsonDocumentCodec.encode(BsonDocumentCodec.java:115)
        at org.bson.codecs.BsonDocumentCodec.encode(BsonDocumentCodec.java:41)
        at com.mongodb.connection.RequestMessage.addDocument(RequestMessage.java:238)
        at com.mongodb.connection.RequestMessage.addDocument(RequestMessage.java:188)
        at com.mongodb.connection.CommandMessage.encodeMessageBodyWithMetadata(CommandMessage.java:154)
        at com.mongodb.connection.RequestMessage.encode(RequestMessage.java:138)
        at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:236)
        at com.mongodb.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:98)
        at com.mongodb.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:441)
        at com.mongodb.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:70)
        at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:192)
        at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:264)
        at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:126)
        at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:118)
        at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:226)
        at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:217)
        at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:120)
        at com.mongodb.operation.FindOperation$1.call(FindOperation.java:717)
        at com.mongodb.operation.FindOperation$1.call(FindOperation.java:711)
        at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:471)
        at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:415)
        at com.mongodb.operation.FindOperation.execute(FindOperation.java:711)
        at com.mongodb.operation.FindOperation.execute(FindOperation.java:83)
        at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:140)
        at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132)
        at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86)
        at org.eclipse.persistence.internal.nosql.adapters.mongo.MongoDatabaseInteraction.execute(MongoDatabaseInteraction.java:173)
        ... 71 more
    

    https://wiki.eclipse.org/EclipseLink/FAQ/NoSQL

    此外,我测试了一个实现,其中AbstractClass

    ABSTRACTCLASSEXTENDEDCLASS

    Frank Caputos@Inheritance属性。这就是我的抽象类目前的情况,它似乎在为我的目的工作:

    @Entity
    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    @NoSql(dataFormat=DataFormatType.MAPPED)
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public abstract class AbstractClass {
    
        @Id
        @Field(name="_id")
        protected String id;
    
        public String getId() {
    
            return this.id;
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Frank Caputo    6 年前

    @Entity
    @NoSql(dataFormat=DataFormatType.MAPPED)
    @DiscriminatorColumn(name = "type")
    public abstract class AbstractClass {
    
        @Id
        @Field(name="_id")
        protected String id;
    
        public String getId() {
    
            return this.id;
        }
    }
    
    @Entity
    @NoSql(dataFormat=DataFormatType.MAPPED)
    @DiscriminatorValue("extended")
    public class ExtendedClass extends AbstractClass {
    
        @Basic
        private String name;
    
        public ExtendedClass() {
    
        }
    
        public ExtendedClass(String id, String name) {
    
            this.id = id;
            this.name = name;
        }
    
        public String getName() {
    
            return this.name;
        }
    }
    

    您可能还需要在@nosql中设置数据类型为的集合名称。

        2
  •  0
  •   Scyla101    6 年前

    @Inheritace 使其工作的属性。这就是抽象类看起来知道哪一个对我来说是可行的解决方案的方式:

    @Entity
    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    @NoSql(dataFormat=DataFormatType.MAPPED)
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public abstract class AbstractClass {
    
        @Id
        @Field(name="_id")
        protected String id;
    
        public String getId() {
    
            return this.id;
        }
    }