package com.hcll.sail.demo.mongo.entity;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.io.Serializable;
@Document(collection = Student.STUDENT)
@Data
public class Student implements Serializable {
public static final String STUDENT = "student";
public static final String ABC = "abc_hash";
@Field(ABC)
private String abc;
}
Criteria c1 = new Criteria();
c1.and(Student.ABC).is("query_xxxxxxx");
Query q = new Query(c1);
Student stu = mongoTemplate.findOne(q, Student.class);
System.out.println(stu);
出现异常:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) ~[spring-boot-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) ~[spring-boot-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) ~[spring-boot-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at com.hcll.sail.demo.mongo.DemoMongoApplication.main(DemoMongoApplication.java:21) ~[classes/:na]
Caused by: org.springframework.data.mapping.MappingException: Couldn’t find PersistentEntity for type java.lang.String!
at org.springframework.data.mapping.context.MappingContext.getRequiredPersistentEntity(MappingContext.java:119) ~[spring-data-commons-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mapping.context.PersistentPropertyPathFactory.getPair(PersistentPropertyPathFactory.java:237) ~[spring-data-commons-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mapping.context.PersistentPropertyPathFactory.createPersistentPropertyPath(PersistentPropertyPathFactory.java:210) ~[spring-data-commons-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mapping.context.PersistentPropertyPathFactory.lambda$getPersistentPropertyPath2(PersistentPropertyPathFactory.java:183) [spring−data−commons−2.2.12.RELEASE.jar:2.2.12.RELEASE]atjava.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) [na:na]atorg.springframework.data.mapping.context.PersistentPropertyPathFactory.getPersistentPropertyPath(PersistentPropertyPathFactory.java:182) [spring−data−commons−2.2.12.RELEASE.jar:2.2.12.RELEASE]atorg.springframework.data.mapping.context.PersistentPropertyPathFactory.from(PersistentPropertyPathFactory.java:97) [spring−data−commons−2.2.12.RELEASE.jar:2.2.12.RELEASE]atorg.springframework.data.mapping.context.PersistentPropertyPathFactory.from(PersistentPropertyPathFactory.java:110) [spring−data−commons−2.2.12.RELEASE.jar:2.2.12.RELEASE]atorg.springframework.data.mapping.context.AbstractMappingContext.getPersistentPropertyPath(AbstractMappingContext.java:275) [spring−data−commons−2.2.12.RELEASE.jar:2.2.12.RELEASE]atorg.springframework.data.mongodb.core.convert.QueryMapper2(PersistentPropertyPathFactory.java:183) ~[spring-data-commons-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) ~[na:na]
at org.springframework.data.mapping.context.PersistentPropertyPathFactory.getPersistentPropertyPath(PersistentPropertyPathFactory.java:182) ~[spring-data-commons-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mapping.context.PersistentPropertyPathFactory.from(PersistentPropertyPathFactory.java:97) ~[spring-data-commons-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mapping.context.PersistentPropertyPathFactory.from(PersistentPropertyPathFactory.java:110) ~[spring-data-commons-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentPropertyPath(AbstractMappingContext.java:275) ~[spring-data-commons-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mongodb.core.convert.QueryMapper2(PersistentPropertyPathFactory.java:183) [spring−data−commons−2.2.12.RELEASE.jar:2.2.12.RELEASE]atjava.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) [na:na]atorg.springframework.data.mapping.context.PersistentPropertyPathFactory.getPersistentPropertyPath(PersistentPropertyPathFactory.java:182) [spring−data−commons−2.2.12.RELEASE.jar:2.2.12.RELEASE]atorg.springframework.data.mapping.context.PersistentPropertyPathFactory.from(PersistentPropertyPathFactory.java:97) [spring−data−commons−2.2.12.RELEASE.jar:2.2.12.RELEASE]atorg.springframework.data.mapping.context.PersistentPropertyPathFactory.from(PersistentPropertyPathFactory.java:110) [spring−data−commons−2.2.12.RELEASE.jar:2.2.12.RELEASE]atorg.springframework.data.mapping.context.AbstractMappingContext.getPersistentPropertyPath(AbstractMappingContext.java:275) [spring−data−commons−2.2.12.RELEASE.jar:2.2.12.RELEASE]atorg.springframework.data.mongodb.core.convert.QueryMapperMetadataBackedField.getPath(QueryMapper.java:1100) ~[spring-data-mongodb-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mongodb.core.convert.QueryMapperMetadataBackedField.<init>(QueryMapper.java:968) [spring−data−mongodb−2.2.12.RELEASE.jar:2.2.12.RELEASE]atorg.springframework.data.mongodb.core.convert.QueryMapperMetadataBackedField.<init>(QueryMapper.java:968) ~[spring-data-mongodb-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mongodb.core.convert.QueryMapperMetadataBackedField.<init>(QueryMapper.java:968) [spring−data−mongodb−2.2.12.RELEASE.jar:2.2.12.RELEASE]atorg.springframework.data.mongodb.core.convert.QueryMapperMetadataBackedField.(QueryMapper.java:945) ~[spring-data-mongodb-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mongodb.core.convert.QueryMapper.createPropertyField(QueryMapper.java:263) ~[spring-data-mongodb-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:142) ~[spring-data-mongodb-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:2473) ~[spring-data-mongodb-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:820) ~[spring-data-mongodb-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:807) ~[spring-data-mongodb-2.2.12.RELEASE.jar:2.2.12.RELEASE]
at com.hcll.sail.demo.mongo.DemoMongoApplication.run(DemoMongoApplication.java:40) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) ~[spring-boot-2.2.12.RELEASE.jar:2.2.12.RELEASE]
… 5 common frames omitted
2021-01-09 09:57:53.575 INFO 27680 — [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService ‘applicationTaskExecutor’
Process finished with exit code 1
出现问题的条件:
1.字段名用xxx_hash,即以_分隔,后面有hash字符。
因为:String.class.getDeclaredFields(); 中包含"hash"属性
2.查询时,用改字段查询
c1.and(“abc_hash”).is(“query_xxxxxxx”);
这样就会出错。
1万+

被折叠的 条评论
为什么被折叠?



