MongoDB -Consider defining a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' in your configuration

Viewed 9454

My Student entity class

package com.example.entity;

import java.io.Serializable;

import javax.persistence.Id;

import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "student")
public class StudentMongo implements Serializable {

    private static final long serialVersionUID = 8764013757545132519L;

    @Id
    private Long id;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

}

My Repository

package com.example.repository;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
import org.springframework.stereotype.Repository;

import com.example.entity.StudentMongo;

@Repository
public class StudentMongoRepository extends SimpleMongoRepository<StudentMongo, Long> {

    @Resource
    MongoOperations mongoOperations;


    public StudentMongoRepository(MongoEntityInformation<StudentMongo, Long> metadata, MongoOperations mongoOperations) {
        super(metadata, mongoOperations);
    }

}

My configuration class

@Configuration
@EnableMongoRepositories(basePackageClasses = {com.example.repository.StudentMongoRepository.class})
public class MongoConfiguration {

}

Spring boot application

When i try to start the application i am getting following application

2017-11-20 09:04:48.937 ERROR 23220 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.example.repository.StudentMongoRepository required a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' in your configuration.

Consider defining a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' in your configuration How to create EntityInformation bean as said by spring framework Getting above issue while running my application . how to pass entity information

suggest me how to use SimpleMongorepository

4 Answers

First of all I would like to say that please DON'T USE IMPLEMENTATION CLASS INSTEAD USE AN INTERFACE.

declare an Interface and extend it from MongoRepository<T,ID>, spring will provide the implementation out-of-the-box.

@Repository
public interface StudentMongoRepository extends MongoRepository<T,ID>{

@Autowired
private MongoOperations mongoOperations;

This should work.

I had the same problem and I solved externalizating the MongoEntityInformationSupport of Spring Data.

Do this:

Create theese three classes:

public final class MongoEntityInformationSupport {

    private MongoEntityInformationSupport() {}

    /**
     * Factory method for creating {@link MongoEntityInformation}.
     *
     * @param entity must not be {@literal null}.
     * @param idType can be {@literal null}.
     * @return never {@literal null}.
     */
    @SuppressWarnings("unchecked")
    public static <T, ID> MongoEntityInformation<T, ID> entityInformationFor(MongoPersistentEntity<?> entity,
                                                                      @Nullable Class<?> idType) {

        Assert.notNull(entity, "Entity must not be null!");

        MappingMongoEntityInformation<T, ID> entityInformation = new MappingMongoEntityInformation<T, ID>(
                (MongoPersistentEntity<T>) entity, (Class<ID>) idType);

        return ClassUtils.isAssignable(Persistable.class, entity.getType())
                ? new PersistableMongoEntityInformation<T, ID>(entityInformation) : entityInformation;
    }
}

Ps.: Put PersistableMongoEntityInformation in the same package of MongoEntityInformationSupport

@RequiredArgsConstructor //<-- Lombok
class PersistableMongoEntityInformation<T, ID> implements MongoEntityInformation<T, ID> {

    private final @NonNull
    MongoEntityInformation<T, ID> delegate;

    /*
     * (non-Javadoc)
     * @see org.springframework.data.mongodb.repository.MongoEntityInformation#getCollectionName()
     */
    @Override
    public String getCollectionName() {
        return delegate.getCollectionName();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.mongodb.repository.MongoEntityInformation#getIdAttribute()
     */
    @Override
    public String getIdAttribute() {
        return delegate.getIdAttribute();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.repository.core.EntityInformation#isNew(java.lang.Object)
     */
    @Override
    @SuppressWarnings("unchecked")
    public boolean isNew(T t) {

        if (t instanceof Persistable) {
            return ((Persistable<ID>) t).isNew();
        }

        return delegate.isNew(t);
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
     */
    @Override
    @SuppressWarnings("unchecked")
    public ID getId(T t) {

        if (t instanceof Persistable) {
            return ((Persistable<ID>) t).getId();
        }

        return delegate.getId(t);
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.repository.core.support.PersistentEntityInformation#getIdType()
     */
    @Override
    public Class<ID> getIdType() {
        return delegate.getIdType();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.repository.core.support.EntityMetadata#getJavaType()
     */
    @Override
    public Class<T> getJavaType() {
        return delegate.getJavaType();
    }
}

I created this MongoHelper just for simplify

public class MongoHelper {
    public static MongoEntityInformation getEntityInformationFor(Class clazz, Class idClazz) {
        TypeInformation typeInformation = ClassTypeInformation.from(clazz);
        MongoPersistentEntity mongoPersistentEntity = new BasicMongoPersistentEntity(typeInformation);
        return MongoEntityInformationSupport.entityInformationFor(mongoPersistentEntity, idClazz);
    }
}

Finally

You can use like this(Sample with QuerydslMongoPredicateExecutor):

public class TicketRepositoryCustomImpl extends QuerydslMongoPredicateExecutor<Ticket> implements TicketRepositoryCustom {

    //The MongoOperations will be autowired
    public TicketRepositoryCustomImpl(MongoOperations mongoOperations) {
        super(MongoHelper.getEntityInformationFor(Ticket.class, String.class), mongoOperations);
    }

    @Override
    public Optional<Ticket> findWithFilter(TicketFilter filter) {
        BooleanBuilder builder = new BooleanBuilder();
        //populate the builder
        return findOne(builder.getValue());
    }
}

Try this :- don't make it autowired just make a variable like this . private MongoTemplate mongoTemplate;

XXService extends PagingAndSortingRepository<XX, String>{
    
}
----------------------------------------------------------------

@Service
public class XXXmpl extends SimpleMongoRepository<XX, String> implements XXService {

    public XXImpl(MongoOperations mongoOperations) {
        super(new MongoRepositoryFactory(mongoOperations).getEntityInformation(XXX.class), mongoOperations);
    }
}
Related