英文:
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("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);
}
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 - {"id":"ENTITY_d02f614f-2856-417e-a130-4a2aecd4817f","payments":["MOBILE_MONEY","CASH"],"priceRating":0,"announcement":{"timestamp":0,"message":"Shut for COVID-19"},"highlights":["LOCALLY_OWNED"],"deliveries":["VIRTUAL"],"wifiAvailable":true,"audiences":["KIDS"],"referenceType":"ENTITY","referenceId":"d02f614f-2856-417e-a130-4a2aecd4817f","addedTimestamp":1601386834683,"modifiedTimestamp":0,"metatype":"EATERY","appointment":true,"seating":["INDOOR"],"accesses":["WHEELCHAIR"],"parking":["STREET"],"amenities":["Dishwasher"],"facilities":["POOL"]}
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
<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>
EDIT: Including the POJO definitions
@Table(value = "metatable")
public class MetaDatatype {
@Column @PrimaryKey
private String id;
@Column
private Set<Payment> payments;
@Column(value = "pricerating")
private short priceRating;
@Column
@CassandraType(type = CassandraType.Name.UDT, userTypeName = "announcement")
private BusinessAnnouncement announcement; //This is the only UDT, all others are enums
@Column
private Set<Highlight> highlights;
@Column
private Set<Delivery> deliveries;
@Column(value = "wifiavailable")
private boolean wifiAvailable;
@Column
private Set<Audience> audiences;
@Column(value = "referencetype")
private Config.ReferenceType referenceType;
@Column(value = "referenceid")
private String referenceId;
@Column(value = "addedTimestamp")
private long addedTimestamp;
@Column(value = "pricerating")
private long modifiedTimestamp;
@Column
private MetaType metatype;
@Column
private boolean appointment;
@Column
private List<Seating> seating;
@Column
private List<DisabilityAccess> accesses;
@Column
private List<Parking> parking;
@Column
private Set<Amenities> amenities;
@Column
private Set<Facilities> facilities;
}
User-defined datatype class definition
@UserDefinedType("announcement")
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论