我正在尝试启用Spring Data MongoDB审核。但每当我试图启动我的服务器时,我都会遇到错误。你能告诉我这里有什么问题吗?我使用的是Spring 4.1.6和Spring Data MongoDB 1.7.0。
SEVERE: Exception sending context initialized event to listener instance of class
org.springframework.web.context.ContextLoaderListener
java.lang.IllegalArgumentException: Unsupported entity
com.idearealty.product.shopchat.persistence.model.IdeaRealtyUser!
Could not determine IsNewStrategy. at
org.springframework.data.support.IsNewStrategyFactorySupport.getIsNewStrategy(IsNewStrategyFactorySupport.java:48)
at
org.springframework.data.auditing.IsNewAwareAuditingHandler.markAudited(IsNewAwareAuditingHandler.java:80)
at
org.springframework.data.mongodb.core.mapping.event.AuditingEventListener.onApplicationEvent(AuditingEventListener.java:54)
at
org.springframework.data.mongodb.core.mapping.event.AuditingEventListener.onApplicationEvent(AuditingEventListener.java:31)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
at
org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331)
at
org.springframework.data.mongodb.core.MongoTemplate.maybeEmitEvent(MongoTemplate.java:1609)
at
org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:792)
at
org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:742)
at
org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606) at
org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:434)
at
org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:419)
at
org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:391)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy62.save(Unknown Source) at
com.idearealty.product.shopchat.config.SetupDataLoader.onApplicationEvent(SetupDataLoader.java:52)
at
com.idearealty.product.shopchat.config.SetupDataLoader.onApplicationEvent(SetupDataLoader.java:1)
at
org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
以下是实体代码
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Id;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.annotation.Transient;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
/**
* @author Debopam
*
*/
@Document(collection="IdeaRealtyUser")
@TypeAlias("user")
public class IdeaRealtyUser extends User implements UserDetails,Serializable {
@Id
private String id;
private String email;
private String phoneNumber;
private String role;
private String firstName;
private String lastName;
private Address address;
//private String password;
@Transient
private String matchingPassword;
@CreatedDate
protected Date createDate;
@LastModifiedDate
protected Date lastModifiedDate;
@CreatedBy
protected String createdBy;
@LastModifiedBy
protected String lastModifiedBy;
//getter and setter
}
配置
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="localhost" />
</bean>
<mongo:mongo host="127.0.0.1" port="27017">
<mongo:options connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1000"
max-wait-time="1500"
auto-connect-retry="true"
socket-keep-alive="true"
socket-timeout="1500"
slave-ok="true"
write-number="1"
write-timeout="0"
write-fsync="true"/>
</mongo:mongo>
<mongo:db-factory dbname="shopchatdb" mongo-ref="mongo"/>
<!--
<mongo:auditing mapping-context-ref="customMappingContext" auditor-aware-ref="com.idearealty.product.util.MongoAuditAware"/>
-->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="mongo"/>
</bean>
<mongo:auditing auditor-aware-ref="ideaRealtyAudit"/>
<context:component-scan base-package="com.idearealty.product.shopchat" />
答案 0 :(得分:0)
问题在于Spring如何确定实体或模型的基础包。重写getMappingBasePackage()方法可以解决此问题。下面是Spring Mongodb配置文件
@Configuration
//@EnableTransactionManagement
@PropertySource({ "classpath:persistence.properties" })
@ComponentScan(basePackages={ "com.idearealty.product.persistence.model" })
@EnableMongoRepositories(basePackages = "com.idearealty.product.repository.mongodb")
@EnableMongoAuditing
public class PersistenceMongoConfig extends AbstractMongoConfiguration{
@Autowired
private Environment env;
public PersistenceMongoConfig() {
super();
}
@Bean(name="mongoAudit")
public AuditorAware<String> mongoAuditProvider() {
return new MongoAuditAware();
}
@Override
protected String getDatabaseName() {
return "shopdb";
}
@Override
public Mongo mongo() throws Exception {
return new MongoClient(new ServerAddress("localhost", 27017),mongoClientOptions());
}
@Bean
public MongoClientOptions mongoClientOptions() {
// override to more reasonable connection timeout (default is 10 seconds)
return MongoClientOptions.builder().connectTimeout(1500)
.connectionsPerHost(8)
.maxWaitTime(1500)
.socketKeepAlive(true)
.build();
}
@Override
protected String getMappingBasePackage() {
return "com.product.persistence.model";
}
}
答案 1 :(得分:-1)
要解决此错误,请声明<mongo:mapping-converter />
并将base-package
属性配置为指向包含您的域类的包。确保使用带有MongoTemplate
和MongoDbFactory
的{{1}}构造函数并连接相应的bean定义。
除此之外,你的配置看起来很可疑。您声明了两个导致MongoConverter
实例的bean(一个通过工厂,一个通过命名空间)。此外,建议最近使用Mongo
实例。
最简化的配置减少到最低限度应该如下所示:
MongoClient