開発中Spring Boot MongoDB
例です。この例では、spring-data-mongoリポジトリクエリでTerritoryIDを取得しようとしていますが、エラーが発生しています。 EmployeeTerritory.java原因:org.springframework.data.mapping.model.MappingException:com.common.model.EmployeeTerritoryにプロパティterritoryIDが見つかりません! - スプリングブート
@Document(collection="employee-territories")
public class EmployeeTerritory {
@Id
private ObjectId id;
@Field
private String EmployeeID;
@Field(value="TerritoryID")
private Integer TerritoryID;
// setters and getters
}
EmployeeTerritoryRepository.java
public interface EmployeeTerritoryRepository extends CrudRepository<EmployeeTerritory, String>{
EmployeeTerritory findByTerritoryID(Integer territoryID);
}
EmployeeTerritoryService.java
public interface EmployeeTerritoryService {
EmployeeTerritory findByTerritoryID(Integer territoryID);
}
:
Caused by: org.springframework.data.mapping.model.MappingException: No property territoryID found on com.common.model.EmployeeTerritory!
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentPropertyPath(AbstractMappingContext.java:236) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentPropertyPath(AbstractMappingContext.java:214) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentPropertyPath(AbstractMappingContext.java:202) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
at org.springframework.data.mongodb.repository.query.MongoQueryCreator.create(MongoQueryCreator.java:109) ~[spring-data-mongodb-1.6.3.RELEASE.jar:na]
at org.springframework.data.mongodb.repository.query.MongoQueryCreator.create(MongoQueryCreator.java:55) ~[spring-data-mongodb-1.6.3.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
at org.springframework.data.mongodb.repository.query.PartTreeMongoQuery.createQuery(PartTreeMongoQuery.java:75) ~[spring-data-mongodb-1.6.3.RELEASE.jar:na]
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:86) ~[spring-data-mongodb-1.6.3.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:415) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:393) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$DefaultMethodInvokingMethodInterceptor.invoke(RepositoryFactorySupport.java:506) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[spring-aop-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at com.sun.proxy.$Proxy35.findByTerritoryID(Unknown Source) ~[na:na]
at com.common.service.EmployeeTerritoryServiceImpl.findByTerritoryID(EmployeeTerritoryServiceImpl.java:18) ~[classes/:na]
at com.common.main.MainController.run(MainController.java:51) ~[classes/:na]
at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:672) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
... 5 common frames omitted
16-03-22 22:33:38 [main] INFO o.s.c.a.AnnotationConfigApplicationContext - Closing org.spring[email protected]576d5deb: startup date [Tue Mar 22 22:33:26 IST 2016]; root of context hierarchy
16-03-22 22:33:38 [main] INFO o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown
Exception in thread "main" java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:675)
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.common.main.Main.main(Main.java:17)
私が開発したコードを案内してください。
EmployeeTerritoryServiceImpl.java
@Component
public class EmployeeTerritoryServiceImpl implements EmployeeTerritoryService{
@Autowired
private EmployeeTerritoryRepository employeeTerritoryRepository;
@Override
public EmployeeTerritory findByTerritoryID(Integer territoryID) {
EmployeeTerritory et = employeeTerritoryRepository.findByTerritoryID(territoryID);
return et;
}
}
MainController.java
@Controller
public class MainController implements CommandLineRunner {
private Logger LOGGER = Logger.getLogger(MainController.class);
@Autowired
private EmployeeTerritoryService etService;
@Override
public void run(String... args) throws Exception {
LOGGER.info("~~ STARTED ~~");
// Find employee-territories
EmployeeTerritory et = etService.findByTerritoryID(19713);
LOGGER.info("~~~~~~~~~~~~~~~~~~~~~~~~");
LOGGER.info("EmployeeID : "+et.getEmployeeID());
LOGGER.info("TerritoryID : "+et.getTerritoryID());
}
}
Main.java
@Configuration
@EnableAutoConfiguration
@EnableMongoRepositories("com.common.repository")
@ComponentScan({"com.common.main", "com.common.service"})
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
application.properties
# MONGODB Config
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.uri=mongodb://localhost/boot
spring.data.mongodb.repositories.enabled=true
logging.level.com.altruista.guidingsigns=INFO
logging.level.org.springframework=INFO
"territoryId"クラスのフィールドに名前を付けてみましたか?それは愚かな質問ではありません。 – RubioRic
'territoryId'を使用してgetter/setterを追加してください(春はそれらを使用するかもしれません) –
春にデバッグすると、@ RRに –