Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data mongoDb not null annotation like Spring data Jpa

Like spring-data-jpa have @NotNull annotation what can be used for this in spring-data-mongodb.?

like image 294
Mehrish Yousuf Avatar asked Feb 17 '17 07:02

Mehrish Yousuf


People also ask

Which one is not a spring data MongoDB annotation?

The Id annotation Id annotation does not belong to spring-data-mongodb module, it belongs to spring data for widely used both for spring-data-jpa and spring-data-mongodb. In the term of MongoDB, this annotation indicates a field should be used as an identifier for each document.

Which is better MongoTemplate or MongoRepository?

So I'd say that MongoTemplate is a better option, unless you have a very elaborated POJO model or need the custom queries capabilities of MongoRepository for some reason. Good points/examples. However your race condition example and undesired result can be avoided using @Version to prevent that very scenario.

Can we use spring data JPA with MongoDB?

Yes, DataNucleus JPA allows it, as well as to many other databases.

IS NOT NULL JPA?

Null constraints in JPA reflect the nullability of a column as defined in the database schema. As such in all database definitions a column is nullable by default. I.e. if a column is not defined as primary key or unique then it is by default nullable.

What are the core features of spring data MongoDB?

In this tutorial we'll explore some of the core features of Spring Data MongoDB – indexing, common annotations and converters. 2. Indexes 2.1. @Indexed This annotation marks the field as indexed in MongoDB: @QueryEntity @Document public class User { @Indexed private String name; ... }

What is Idid annotation in spring data MongoDB?

Id annotation does not belong to spring-data-mongodb module, it belongs to spring data for widely used both for spring-data-jpa and spring-data-mongodb. In the term of MongoDB, this annotation indicates a field should be used as an identifier for each document.

Why can't I find the name field in spring data MongoDB?

We may be surprised there's no sign of the name field anywhere! This is because, as of Spring Data MongoDB 3.0, automatic index creation is turned off by default. We can, however, change that behavior by explicitly overriding autoIndexCreation () method in our MongoConfig:

What is field annotation in MongoDB?

The Field annotation used to represent a field in a collection. It is similar to the Column annotation in java persistence API but it does not have much property like java.persistence.Column. name → to specify the name of the field stored in MongoDB. It is useful when the class field differents from the collection field.


1 Answers

javax.validation.constraints.NotNull itself could be used with spring-data-mongodb. For this you need to have following in place.

JSR-303 dependencies added in your pom.xml

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.3.4.Final</version>
</dependency>

Declare appropriate validators and validator event listeners

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Configuration
public class Configuration {
    @Bean
    public ValidatingMongoEventListener validatingMongoEventListener() {
        return new ValidatingMongoEventListener(validator());
    }

    @Bean
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }
}

Add @NotNull annotation in your MongoDB POJO

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import javax.validation.constraints.NotNull;

@Document(collection = "user_account")
public class User {
    @Id
    private String userId;

    @NotNull(message = "User's first name must not be null")
    private String firstName;

    @NotNull(message = "User's last name must not be null")
    private String lastName;
}

With this configuration and implementation, if you persist User object with null values, then you will see failure with javax.validation.ConstraintViolationException

Remember: if using @SpringBootApplication, make sure that Spring can scan your Configuration file. Otherwise, you may use @ComponentScan("com.mypackage").

like image 132
Naveen Kumar Avatar answered Sep 30 '22 04:09

Naveen Kumar