Like spring-data-jpa have @NotNull annotation what can be used for this in spring-data-mongodb.?
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.
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.
Yes, DataNucleus JPA allows it, as well as to many other databases.
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.
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; ... }
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.
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:
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.
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")
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With