NPE 来自 MappingCassandraConverter

huangapple go评论124阅读模式
英文:

NPE from MappingCassandraConverter

问题

我在尝试将对象插入数据库时,由于Cassandra转换器抛出了一个NPE(NullPointerException)异常。在日志中显示了POJO的JSON表示。在代码中,它使用了 @Table@PrimaryKey@Column 字段进行了注释。

我不确定是否会影响,但是POJO具有一个用户定义的类型,我已经根据注释进行了适当的标注,如 @CassandraType(type = CassandraType.Name.UDT, userTypeName = "announcement")。对于该字段(announcement字段)的POJO,我还使用了 @UserDefinedType("announcement") 标签。

根据这里的示例,我使用以下代码设置了Cassandra会话:

public void initialize() {
    session = CqlSession.builder().addContactPoint(new InetSocketAddress(cassandraHost, cassandraPort))
            .withAuthCredentials(csUsername, csPassword)
            .withKeyspace(keyspace)
            .withLocalDatacenter("datacenter1")
            .build();
    template = new CassandraTemplate(session);

    logger.info("Cassandra client connection created");
}

public MetaDatatype saveMeta(MetaDatatype metaDatatype) {
    logger.info("Adding new {} row for id: {}", metaDatatype.getReferenceType().name(), metaDatatype.getReferenceId());
    logger.info(new Gson().toJson(metaDatatype, MetaDatatype.class));
    return template.insert(metaDatatype);
}

但在运行插入代码后,我得到了以下错误:

java.lang.NullPointerException: null
	at org.springframework.data.cassandra.core.convert.MappingCassandraConverter.lambda$new$1(MappingCassandraConverter.java:142) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
	...

这是我的pom文件中的一部分:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4.RELEASE</version>
    <relativePath></relativePath>
</parent>
...
<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-cassandra</artifactId>
   <version>3.0.4.RELEASE</version>
</dependency>

POJO定义如下:

@Table(value = "metatable")
public class MetaDatatype {
    @Column @PrimaryKey
    private String id;
    @Column
    private Set<Payment> payments;
    ...
    @Column
    @CassandraType(type = CassandraType.Name.UDT, userTypeName = "announcement")
    private BusinessAnnouncement announcement;
    ...
}

@UserDefinedType("announcement")
public class BusinessAnnouncement {
    @CassandraType(type = CassandraType.Name.BIGINT)
    private long timestamp;
    @CassandraType(type = CassandraType.Name.TEXT)
    private String message;
}
英文:

I get an NPE thrown by the cassandra converters, when I attempt to insert an object into the database. A json representation of the POJO is shown in the logs.In the code it is annotated with the @Table, @PrimaryKey and @Column fields.
Not sure if this affects it but the POJO has one user-defined type that I have annotated accordingly as @CassandraType(type = CassandraType.Name.UDT, userTypeName = "announcement"). The POJO for that(announcement field) is also noted with the @UserDefinedType("announcement") tags.

Following the examples here I set up my cassandra session with the following code

 public void initialize() {
    session = CqlSession.builder().addContactPoint(new InetSocketAddress(cassandraHost, cassandraPort))
            .withAuthCredentials(csUsername, csPassword)
            .withKeyspace(keyspace)
            .withLocalDatacenter(&quot;datacenter1&quot;)
            .build() ;
    template = new CassandraTemplate(session);

    logger.info(&quot;Cassandra client connection created&quot;);
}

public MetaDatatype saveMeta(MetaDatatype metaDatatype) {
    logger.info(&quot;Adding new {} row for id: {}&quot; , metaDatatype.getReferenceType().name(), metaDatatype.getReferenceId());
    logger.info(new Gson().toJson(metaDatatype, MetaDatatype.class) );
    return template.insert(metaDatatype);
}

But after running the insert code, this is what I get

2020-09-29T09:40:34.683 INFO  [http-nio-7891-exec-1] MetaRepository:21 - Adding metadatatype for ENTITY with id: ENTITY_d02f614f-2856-417e-a130-4a2aecd4817f
2020-09-29T09:40:34.684 INFO  [http-nio-7891-exec-1] PersistenceClient:80 - Adding new ENTITY row for id: d02f614f-2856-417e-a130-4a2aecd4817f
2020-09-29T09:40:34.710 INFO  [http-nio-7891-exec-1] PersistenceClient:81 - {&quot;id&quot;:&quot;ENTITY_d02f614f-2856-417e-a130-4a2aecd4817f&quot;,&quot;payments&quot;:[&quot;MOBILE_MONEY&quot;,&quot;CASH&quot;],&quot;priceRating&quot;:0,&quot;announcement&quot;:{&quot;timestamp&quot;:0,&quot;message&quot;:&quot;Shut for COVID-19&quot;},&quot;highlights&quot;:[&quot;LOCALLY_OWNED&quot;],&quot;deliveries&quot;:[&quot;VIRTUAL&quot;],&quot;wifiAvailable&quot;:true,&quot;audiences&quot;:[&quot;KIDS&quot;],&quot;referenceType&quot;:&quot;ENTITY&quot;,&quot;referenceId&quot;:&quot;d02f614f-2856-417e-a130-4a2aecd4817f&quot;,&quot;addedTimestamp&quot;:1601386834683,&quot;modifiedTimestamp&quot;:0,&quot;metatype&quot;:&quot;EATERY&quot;,&quot;appointment&quot;:true,&quot;seating&quot;:[&quot;INDOOR&quot;],&quot;accesses&quot;:[&quot;WHEELCHAIR&quot;],&quot;parking&quot;:[&quot;STREET&quot;],&quot;amenities&quot;:[&quot;Dishwasher&quot;],&quot;facilities&quot;:[&quot;POOL&quot;]}
2020-09-29T09:40:34.759 ERROR [http-nio-7891-exec-1] MetadataExchange:183 - Error occcured when adding metadata
java.lang.NullPointerException: null
at org.springframework.data.cassandra.core.convert.MappingCassandraConverter.lambda$new$1(MappingCassandraConverter.java:142) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
at org.springframework.data.cassandra.core.convert.DefaultColumnTypeResolver.getUserType(DefaultColumnTypeResolver.java:470) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
at org.springframework.data.cassandra.core.convert.DefaultColumnTypeResolver.resolve(DefaultColumnTypeResolver.java:260) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
at org.springframework.data.cassandra.core.convert.DefaultColumnTypeResolver.resolve(DefaultColumnTypeResolver.java:127) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
at org.springframework.data.cassandra.core.convert.MappingCassandraConverter.getWriteValue(MappingCassandraConverter.java:743) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
at org.springframework.data.cassandra.core.convert.MappingCassandraConverter.writeMapFromWrapper(MappingCassandraConverter.java:491) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
at org.springframework.data.cassandra.core.convert.MappingCassandraConverter.write(MappingCassandraConverter.java:457) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
at org.springframework.data.cassandra.core.StatementFactory.insert(StatementFactory.java:307) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
at org.springframework.data.cassandra.core.CassandraTemplate.doInsert(CassandraTemplate.java:639) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:629) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:617) ~[spring-data-cassandra-3.0.4.RELEASE.jar!/:3.0.4.RELEASE]
at com.ghyrate.dataservice.clients.PersistenceClient.saveMeta(PersistenceClient.java:82) ~[classes!/:1.0-SNAPSHOT]
at com.ghyrate.dataservice.repository.MetaRepository.addMeta(MetaRepository.java:22) ~[classes!/:1.0-SNAPSHOT]
at com.ghyrate.dataservice.processor.MetadataExchange.postMetadata(MetadataExchange.java:179) [classes!/:1.0-SNAPSHOT]
at com.ghyrate.dataservice.controllers.MetaDataController.postEntity(MetaDataController.java:56) [classes!/:1.0-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_131]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) [spring-web-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) [spring-web-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) [spring-webmvc-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) [spring-webmvc-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) [spring-webmvc-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) [spring-webmvc-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) [spring-webmvc-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) [spring-webmvc-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) [spring-webmvc-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) [tomcat-embed-core-9.0.38.jar!/:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) [tomcat-embed-core-9.0.38.jar!/:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) [tomcat-embed-websocket-9.0.38.jar!/:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) [spring-web-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) [spring-web-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:93) [spring-boot-actuator-2.3.4.RELEASE.jar!/:2.3.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.9.RELEASE.jar!/:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_131]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_131]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.38.jar!/:9.0.38]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]

This is from my pom

&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
&lt;relativePath&gt;&lt;/relativePath&gt;
&lt;/parent&gt;
...
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
&lt;artifactId&gt;spring-data-cassandra&lt;/artifactId&gt;
&lt;version&gt;3.0.4.RELEASE&lt;/version&gt;
&lt;/dependency&gt;

EDIT: Including the POJO definitions

@Table(value = &quot;metatable&quot;)
public class MetaDatatype {
@Column @PrimaryKey
private String id;
@Column
private Set&lt;Payment&gt; payments;
@Column(value = &quot;pricerating&quot;)
private short priceRating;
@Column
@CassandraType(type = CassandraType.Name.UDT, userTypeName = &quot;announcement&quot;)
private BusinessAnnouncement announcement; //This is the only UDT, all others are enums
@Column
private Set&lt;Highlight&gt; highlights;
@Column
private Set&lt;Delivery&gt; deliveries;
@Column(value = &quot;wifiavailable&quot;)
private boolean wifiAvailable;
@Column
private Set&lt;Audience&gt; audiences;
@Column(value = &quot;referencetype&quot;)
private Config.ReferenceType referenceType;
@Column(value = &quot;referenceid&quot;)
private String referenceId;
@Column(value = &quot;addedTimestamp&quot;)
private long addedTimestamp;
@Column(value = &quot;pricerating&quot;)
private long modifiedTimestamp;
@Column
private MetaType metatype;
@Column
private boolean appointment;
@Column
private List&lt;Seating&gt; seating;
@Column
private List&lt;DisabilityAccess&gt; accesses;
@Column
private List&lt;Parking&gt; parking;
@Column
private Set&lt;Amenities&gt; amenities;
@Column
private Set&lt;Facilities&gt; facilities;
}

User-defined datatype class definition

@UserDefinedType(&quot;announcement&quot;)
public class BusinessAnnouncement {
@CassandraType(type = CassandraType.Name.BIGINT)
private long timestamp;
@CassandraType(type = CassandraType.Name.TEXT)
private String message;
}

答案1

得分: 1

实体定义/用户自定义类型(UDT)定义的代码未包含在内,这可能会很有用,但我怀疑在这里使用 @CassandraType 的方式不太正确。驱动程序映射器文档:https://docs.datastax.com/en/developer/java-driver/4.9/manual/mapper/entities/#inheritance - 并未使用 @CassandraType 注解。

假设您使用较新的 Cassandra 统一驱动程序,但在pom文件中并未看到相关包含。

页面上有一个示例 - 简而言之,您对实体进行注解,如果一个实体包含另一个实体,则它会自动处理UDT方面的内容。

英文:

The code for the entity definition / UDT definition is not included which would of been useful, but I suspect that the use of @CassandraType here is not quite correct. The driver mapper documentation : https://docs.datastax.com/en/developer/java-driver/4.9/manual/mapper/entities/#inheritance - does not use a @CassandraType annotation.

This assumes you would use the newer unified drivers for Cassandra, but there is no inclusion in the pom file visible.

There is an example on the page - simply put you annotate the entities, and it 1 entity contains another, it automatically handles the UDT aspect.

huangapple
  • 本文由 发表于 2020年9月29日 22:16:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64121489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定