Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring data mongo No property b found on entity class when retrieving entity by Id

I have a parent class that has some shared fields and a child class that extends it.

@SuperBuilder(toBuilder = true)
@Data
public abstract class MultiTenantAuthoredDocument {

    @Indexed
    private String tenantId;

    @CreatedDate
    private LocalDateTime createdDate;

    @LastModifiedDate
    private LocalDateTime lastModifiedDate;
}


@Document(collection = "users")
@SuperBuilder(toBuilder = true)
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
 public class User extends MultiTenantAuthoredDocument {

    @Id
    private String id;

    private String firstName;

    private String lastName;

    @Indexed
    private String password;

    @Indexed(unique = true)
    private String userName;

    @Indexed(unique = true)
    private String email;

    @Indexed
    private List<UserRole> roles;

    @Builder.Default
    private boolean enabled = false;

}

However when running my unit tests, I get an unexpected exception when I do a findById and there's a result found namely: No property b found on entity class be.moesmedia.erp.users.domain.User to bind constructor parameter to! As I have no clue where property b is coming from it's pretty difficult to see what I'm doing wrong.
If anyone can help me point out what I'm doing wrong.

like image 866
J.Pip Avatar asked Nov 08 '18 11:11

J.Pip


People also ask

What is @document annotation in spring boot?

@Document is an annotation provided by Spring data project. It is used to identify a domain object, which is persisted to MongoDB. So you can use it to map a Java class into a collection inside MongoDB. If you don't use Spring Data, you don't need this annotation.

What is the use of @document in spring boot?

Annotation @Document The annotations @Document applied to a class marks this class as a candidate for mapping to the database. The most relevant parameter is value to specify the collection name in the database. The annotation @Document specifies the collection type to DOCUMENT .

What is spring boot MongoRepository?

MongoRepository extends the PagingAndSortingRepository and QueryByExampleExecutor interfaces that further extend the CrudRepository interface. MongoRepository provides all the necessary methods which help to create a CRUD application and it also supports the custom derived query methods.

Which annotation is used on domain class is used by Spring data MongoDB?

The @Id annotation tells the mapper which property you want to use for the MongoDB _id property and the @Indexed annotation tells the mapping framework to call ensureIndex on that property of your document, making searches faster.


1 Answers

So I've figured out what was going wrong, Lombok generated a constructor that accepted an Object with the properties for the SuperBuilder class. Once I added @NoArgsConstructorto both the child and parent class, it works like a charm.

like image 142
J.Pip Avatar answered Sep 18 '22 22:09

J.Pip