Spring Data Mongo doesn't persist field value as null - This doesn't works - spring

I already went through the link: https://jira.spring.io/browse/DATAMONGO-1107 and also I dont see option to set spring.data.mongodb.persist-null-fields=true in application.properties file.
Spring Data Mongo doesn't even persist a field having value null. How to allow to save null value if I want to do so ?
Person.java
#Builder
#Data
#AllArgsConstructor
#NoArgsConstructor
#Document
public class Person {
private String id;
private String firstName;
private String lastName;
private String emailId;
#Field
private List<Hobbies> hobbies;
}
Hobbies.java
#Builder
#Data
#AllArgsConstructor
#NoArgsConstructor
public class Hobbies {
private String interest;
private String sports;
}
save() - This method doesn't persist data.
private void savePersons() {
Hobbies hobbies1 = Hobbies.builder().interest("Indoor").sports("Chess").build();
Hobbies hobbies2 = Hobbies.builder().interest("Loveoor").sports("Table Tennis").build();
Hobbies hobbies3 = Hobbies.builder().interest("Gamedoor").sports("Cricket").build();
Hobbies hobbies4 = Hobbies.builder().interest("Happydoor").sports("Lawn Tennis").build();
Person john = Person.builder().firstName("John")
.emailId("john.kerr#gmail.com").hobbies(Arrays.asList(hobbies1, hobbies2)).build();
Person neha = Person.builder().firstName("Neha").lastName("Parate")
.emailId("john.kerr#gmail.com").hobbies(Arrays.asList(hobbies1, hobbies2, hobbies4)).build();
repository.saveAll(Arrays.asList(john, neha));
}
I dont see lastName has been saved in the document. Also, how to save Date as null value?

One sollution is maybe to use defaults.
Your schemas can define default values for certain paths. If you create a new document without that path set, the default will kick in.
So if you want to "allow null", just make it the default value by adding to your schema definition lastName: { type: String, default: null }. When there is no lastName (aka null) you get the default value (null).
I would rethink allowing null because it would make querying complicated: Query on key: null will retrieve you all document where key is null or where key doesn't exist.

This is now possible starting with Spring Data MongoDB 3.3..
For example:
#Field(write = Field.Write.ALWAYS)
private String lastName;

Related

Document field as primary key not working

I have a "document" field that needs to be a primary key and must be unique, but every time I do a POST with the same document it updates the document and doesn't send a BAD_REQUEST
My entity:
#Entity
#Data
#NoArgsConstructor
#AllArgsConstructor
#Table(uniqueConstraints={#UniqueConstraint(columnNames={"document"})})
public class Cliente {
#Id
#Column(unique=true, updatable = false)
#NotBlank #NotNull
private String document;
#NotBlank
private String name;
#NotNull
private LocalDateTime date;
}
When I try to make a new POST with the same document it just updates what is saved in the database.
"Hibernate: update client set date=?, name=? where document=?"
The problem is that Spring Data JPA, when you call Repository#save, assumes that you want to update an existing entity when the passed in entity object has the id attribute set. You will have to inject a EntityManager in your code and instead call EntityManager#persist if you want to make sure that Hibernate tries to do an insert, in which case you'd get a constraint violation exception, just as you expect.

Elastic search with Spring Data for Reactive Repository - Deleting based on nested attributes

I am using Spring data for Elastic Search and am using the ReactiveCrudRepository for stuff like finding and deleting. I noticed that with attributes that are in root and are simple objects, the deletion works (deleteByAttributeName). However if I have nested objects then it does not work.
Here's my entities
Book
#Data
#TypeAlias("book")
#Document(indexName = "book")
public class EsBook{
#Field(type = FieldType.Long)
private Long id;
#Field(type = FieldType.Nested)
private EsStats stats;
#Field(type = FieldType.Date, format = DateFormat.date)
private LocalDate publishDate;
}
Stats
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
#EqualsAndHashCode
public class EsStats{
#Field(type = FieldType.Double)
private Double averageRating;
#Field(type = FieldType.Integer)
private Double totalRatings;
#Field(type = FieldType.Keyword)
private String category; //this can be null
}
Here is what I have tried and is working and not working
I used ReactiveCrudRepository to delete documents in index. For all the regular fields on Book Level like id or with id and publishDate deletion works perfectly. As soon as I use embedded object like Stats, it stops working. I see the documents and the stats that I am sending match atleast visually but never finds or deletes them.
I tried to use EqualsAndHashcode in the Stats assuming maybe iternally somehow does not consider equal for some reason. I also tried changing double data type to int, because on looking at the elastic search document, I see that average review if whole number like 3 is save as 3 but when we send it from Java, i see in the debug 3 being shown as 3.0, so I was doubting if that is the case, but does not seem so. Even changing the datatype to int deletion does not work.
public interface ReactiveBookRepository extends ReactiveCrudRepository<EsBook, String> {
Mono<Void> deleteById(long id); //working
Mono<Void> deleteByIdAndPublishDate(long id, LocalDate publishDate); //Nor working
Mono<Void> deleteByIdAndStats(long id, LocalDate startDate);
}
Any help will be appreciated
Have you verified that your Elasticsearch index mapping matches your Spring Data annotations?
Verify that the index mapping defines the stats field as a nested field type.
If not, then try changing your Spring annotation to:
#Field(type = FieldType.Object)
private EsStats stats;

Advantage of assigning the returned savedEntity in Spring Data

I see in most of the coders save data(using spring data) as:
savedEntity = repo.save(savedEntity);
Long id = savedEntity.getId();
I am confused about why most of them assign back the returned value to the saved Entity while the following code also works exact(I have tested myself):
repo.save(savedEntity);
Long id = savedEntity.getId();
Did I miss some benefit of assigning back?
for example, let the entity be:
#Entity
public class SavedEntity {
#Id
private int id;
private String name;
//getter, setters, all arg-constructor, and no-arg constructor
}
Consider the object of SavedEntity is
SavedEntity entity = new SavedEntity(1,"abcd");
now for your first question,
SavedUser entity1 = repo.save(entity);
Long id = entity1.getId();
this entity1 object is the return object getting from the database, which means the above entity is saved in the database succesfully.
for the Second Question,
repo.save(entity);
Long id = entity.getId();//which you got it from SavedEntity entity = new SavedEntity(1,"abcd");
here the value of id is the integer you mentioned in place of id(the raw value).
Most of the time the id (primary key) is generated automatically while storing the entity to the database using strategies like AUTO, Sequence etc. So as to fetch those id's or autogenerated primary key values we assign back the saved entity.
For example:
#Entity
public class Customer {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
}
In this case you'll not pass the id externally but it will create a value for it automatically while storing the data to DB.

Spring Boot + Webflux + Reactive MongoDB - get document by property Id

I'd like to find all Offer documents by Offer.ProductProperties.brand:
#Document(collection = "offers")
public class Offer {
#Id
private String id;
#NotNull
#DBRef
private ProductProperties properties;
ProductProperties:
#Document(collection = "product_properties")
public class ProductProperties {
#Id
private String id;
#NotNull
#NotEmpty
private String brand;
Service:
Flux<ProductProperties> all = productPropertiesRepository.findAllByBrand(brand);
List<String> productPropIds = all.toStream()
.map(ProductProperties::getId)
.collect(Collectors.toList());
Flux<Offer> byProperties = offerRepository.findAllByProperties_Id(productPropIds);
But unfortunately byProperties is empty. Why?
My repository:
public interface OfferRepository extends ReactiveMongoRepository<Offer, String> {
Flux<Offer> findAllByProperties_Id(List<String> productPropertiesIds);
}
How to find all Offers by ProductProperties.brand?
Thanks!
After reading some documentation found out that You cannot query with #DBRef. Hence the message
Invalid path reference properties.brand! Associations can only be
pointed to directly or via their id property
If you remove DBRef from the field, you should be able to query by findAllByProperties_BrandAndProperties_Capacity.
So the only ways is how you are doing. i.e. Fetch id's and query by id.
As I said in the comment, the reason it is not working is because return type of findAllByProperties_Id is a Flux. So unless u execute a terminal operation, you wont have any result. Try
byProperties.collectList().block()

Bulk data to find exists or not : Spring Data JPA

I get an Post request that would give me a List<PersonApi> Objects
class PersonApi {
private String name;
private String age;
private String pincode ;
}
And I have an Entity Object named Person
#Entity
#Table(name = "person_master")
public class Person{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
#Column(name = "name")
String name;
#Column(name = "age")
String age;
#Column(name = "pincode ")
String pincode ;
}
My record from Post request would look something like this (pseudocode representation of the data below)
[
"Arun","33","09876gh"
"James","34","8765468"
]
I need to do a bulk-validation using Spring JPA.. Give the List<PersonApi> and get a True or False based on the condition that all the entries in the PersonApi objects list should be there in the database.
How to do this ?
The selected answer is not a right one. (not always right)
You are selecting the whole database to check for existence. Unless your use case is very special, i.e. table is very small, this will kill the performance.
The proper way may start from issuing repository.existsById(id) for each Person, if you never delete the persons, you can even apply some caching on top of it.
exists
Pseudo Code:
List<PersonApi> personsApiList = ...; //from request
List<Person> result = personRepository.findAll();
in your service class you can access your repository to fetch all database entities and check if your list of personapi's is completeley available.
boolean allEntriesExist = result.stream().allMatch(person -> personsApiList.contains(createPersonApiFromPerson(person)));
public PersonApi createPersonApiFromPerson(Person person){
return new PersonApi(person.getName(), person.getAge(), person.getPincode());
}

Resources