2

I’m trying to avoid persisting some fields in my POJO in to MongoDB.

I tried

1) Adding @javax.persistence.Transient on field.

2) Adding @org.springframework.data.annotation.Transient on field even i do not use spring at all.

3) Make the field transient

I am using Jakson as the default ObjectMapper of mongodb. and i am not using Spring data to interact with database

But no luck.

user2486322
  • 847
  • 3
  • 13
  • 31
  • duplicate of http://stackoverflow.com/questions/8254856/mongo-ignore-property-from-being-persisted – Redlab Jan 30 '17 at 09:02
  • Possible duplicate of [Mongo - Ignore property from being persisted](http://stackoverflow.com/questions/8254856/mongo-ignore-property-from-being-persisted) – Marc Tarin Jan 30 '17 at 09:04
  • I have tried that spring @Transient , but it won't work. can't you see that i have mentioned it on my question???? – user2486322 Jan 30 '17 at 09:33

1 Answers1

0

The issue is with javax.persistence.Transient But this one, which is a JPA annotation, will not work for MongoDB:

If you want you can use Spring framework @Transient annotation. Spring Documentation

import org.springframework.data.annotation.Transient;

I tried with Spring framework which is working fine for me.

Sample example

@Transient
    private Integer age;

If above solution not working the there might be an issue with MappingMongoConverter Annotation based mapping only works if you're using the MappingMongoConverter (Documentation) as backing converter for MongoTemplate. If you're not configuring the converter a SimpleMongoConverter will be used by default that simply serializes objects into Mongo without taking a look at any meta information whatsoever.

Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59