None of the Transient annotations are working with mongoDB data persistence.

649 views
Skip to first unread message

Chiranjeevi Veerni

unread,
Mar 9, 2016, 3:37:30 PM3/9/16
to Morphia

None of the Transient annotations are working with mongoDB data persistence.

Here are the annotations we used for the fields which should be skipped from persisting in mongo DB.

org.mongodb.morphia.annotations.Transient

com.google.code.morphia.annotations.Transient

org.springframework.data.annotation.Transient

javax.persistence.Transient

we even tried by directly adding transient keyword. transient privateTaxId taxId.

None of the above annotations are working. It's not skipping the value from getting persisted in Db.

We are using com.google.code.morphia.dao for persisting the objects. Here is our code base.



Justin Lee

unread,
Mar 9, 2016, 4:19:15 PM3/9/16
to mor...@googlegroups.com
It looks like you're mixing versions.  The "com.google.code.morphia" package went away a couple of years ago.  Make sure you're using the latest.  If you're using maven you'd use this entry in your pom.xml

          <dependency>
              <groupId>org.mongodb.morphia</groupId>
              <artifactId>morphia</artifactId>
              <version>1.1.0</version>
          </dependency>

org.mongodb.morphia.annotations.Transient is the only annotation supported by Morphia.



--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "mongodb.com", "antwerkz.com" ],
  location : "New York, NY" }

--
You received this message because you are subscribed to the Google Groups "Morphia" group.
To unsubscribe from this group and stop receiving emails from it, send an email to morphia+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chiranjeevi Veerni

unread,
Mar 10, 2016, 1:27:54 AM3/10/16
to Morphia
But we are already using the below one in our POM.xml
 still i see this issue.

  <dependency>

        <groupId>org.mongodb.morphia</groupId>

        <artifactId>morphia</artifactId>

        <version>1.1.1</version>

</dependency>

Justin Lee

unread,
Mar 10, 2016, 7:06:08 AM3/10/16
to mor...@googlegroups.com
Can you post your code so we can take a look?

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "mongodb.com", "antwerkz.com" ],
  location : "New York, NY" }

Chiranjeevi Veerni

unread,
Mar 10, 2016, 7:06:49 AM3/10/16
to Morphia
Hi Justing,


I have changed all the code where we are using com.google.code.morphia to org.mongodb.morphia. Still the transient annotation is not working. Its saving the value in mongo DB. 

Could you please help me , i already wasted lot of time on this issue.

Regard,
Chiranjeevi.


On Thursday, March 10, 2016 at 2:49:15 AM UTC+5:30, Justin Lee wrote:

Chiranjeevi Veerni

unread,
Mar 10, 2016, 7:14:14 AM3/10/16
to Morphia
Actually it's a company code i should not share. I can give you the objects skeleton. Here taxid should not be persisted. Here is the code for UserInfo and TaxId objects.

import javax.validation.Valid;

import javax.validation.constraints.NotNull;

import javax.xml.bind.annotation.XmlElement;

import org.mongodb.morphia.annotations.Transient;



public class UserInfo {


    @XmlElement(name="name")

    @Valid

    @NotNull(message = "10003")

    private Name name;

    @XmlElement(name="address")

    @Valid

    @NotNull(message = "10004")

    private Address address;

    

@XmlElement(name="phone")

@Valid

    @NotNull(message = "10005")

    private Phone phone;

@Transient

private TaxId taxId;

private DOB dob;


public UserInfo() {

super();

}

public UserInfo(Name name, Address address, Phone phone,TaxId taxId,  DOB dob) {

this.name = name;

this.address = address;

this.phone = phone;

this.taxId = taxId;

this.dob =dob;

}

public Name getName() {

return name;

}


public void setName(Name name) {

this.name = name;

}


public Address getAddress() {

return address;

}


public void setAddress(Address address) {

this.address = address;

}


public Phone getPhone() {

return phone;

}


public void setPhone(Phone phone) {

this.phone = phone;

}


public DOB getDob() {

return dob;

}


public void setDob(DOB dob) {

this.dob = dob;

}


public TaxId getTaxId() {

return taxId;

}


public void setTaxId(TaxId taxId) {

this.taxId = taxId;

}


}


import org.mongodb.morphia.annotations.Transient;


public class TaxId {

@Transient

private String taxId;  

@Transient

private String taxIdType;

 

public String getTaxId() {

return taxId;

}

public void setTaxId(String taxId) {

this.taxId = taxId;

}

public String getTaxIdType() {

return taxIdType;

}

public void setTaxIdType(String taxIdType) {

this.taxIdType = taxIdType;

}

 


}


Justin Lee

unread,
Mar 10, 2016, 7:19:41 AM3/10/16
to mor...@googlegroups.com
Where is that image from? 

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "mongodb.com", "antwerkz.com" ],
  location : "New York, NY" }

Chiranjeevi Veerni

unread,
Mar 10, 2016, 8:50:33 AM3/10/16
to Morphia
Its from the mongoDB screen shot. Robomongo tool.

Justin Lee

unread,
Mar 10, 2016, 9:36:16 AM3/10/16
to mor...@googlegroups.com
I just confirmed that @Transient suppresses the field in MappedClass that Morphia uses to track entity mappings.  There has to be something else in your system at play here.  Can you create a test case that reproduces this problem independent from your production/proprietary code?

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "mongodb.com", "antwerkz.com" ],
  location : "New York, NY" }

Chiranjeevi Veerni

unread,
Mar 10, 2016, 9:45:09 AM3/10/16
to Morphia
Sure it might take some time. Is it possible to talk in hangouts. My gmail ID is chiranje...@gmail.com
Please share ur gmail id. Ill try to chat and explain you the problem in detail.

Chiranjeevi Veerni

unread,
Mar 10, 2016, 10:17:09 AM3/10/16
to Morphia
There is one more class above the UserInfo.  here is the class.

@Entity(ZootTransLogDo.COLLECTION_NAME)
@Indexes(@Index(name = "idx_userId_requestDigest", value = "userId, requestDigest", unique = false))
public class ZootTransLogDo {

public static final String COLLECTION_NAME = "TransLog";

public static final String FIELD_ID = "id";
public static final String FIELD_USER_ID = "userId";
public static final String FIELD_REQUEST_DIGEST = "requestDigest";
public static final String FIELD_VENDOR_TRANS_LOG = "vendorTransLogId";
public static final String FIELD_UPDATED_DATE = "-updated";

// unique id within the collection
@Id
ObjectId id;

String userId;

String publicUserId;

String requestDigest;

@Indexed(name = "idx_vendorTransLogId", unique = true)
String vendorTransLogId;

@Indexed(name = "idx_clientTransId", unique = true)
String clientTransId;

@Embedded
UserInfo userInfo;
@Embedded
BusinessInfo businessInfo;

@Embedded
ApplicationInfo applicationInfo;
@Embedded
ResponseData response;
@Embedded
Map<String,String> Attributes;

// date the doc was created: will be set automatically before insert
@Indexed(value = IndexDirection.DESC, name = "idx_created")
Date created;

// date the doc was last updated: will be set automatically before update
@Indexed(value = IndexDirection.DESC, name = "idx_updated")
Date updated;

@PrePersist
void prePersist() {
if (userInfo!=null){
requestDigest = UserInfoDigestHelper.getUserInfoDigest(userInfo);
} else {
requestDigest = BusinessInfoDigestHelper.getBusinessInfoDigest(businessInfo);
}
updated = new Date();
if (created == null) {
created = updated;
}
}

@PostPersist
void postPersist() {
}

/*
* @Override public int hashCode() { return 31 * (int) this.getId() +
* this.toString().hashCode(); }
*/

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null || obj.getClass() != this.getClass()) {
return false;
}

TransLogDo other = (TransLogDo) obj;
return this.getId() == other.getId()
&& areEqual(this.getUpdated(), other.getUpdated())
&& areEqual(this.getCreated(), other.getCreated());
}

protected static boolean areEqual(Object obj1, Object obj2) {
if (obj1 == obj2) {
return true;
}

if ((obj1 == null && obj2 != null) || (obj1 != null && obj2 == null)) {
return false;
}

return obj1.equals(obj2);
}

public static String getDateStr(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
String dateStr = (date == null ? "" : formatter.format(date));
return dateStr;
}

public ObjectId getId() {
return id;
}

public void setId(ObjectId id) {
this.id = id;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getPublicUserId() {
return publicUserId;
}

public void setPublicUserId(String publicUserId) {
this.publicUserId = publicUserId;
}

public String getRequestDigest() {
return requestDigest;
}

public void setRequestDigest(String requestDigest) {
this.requestDigest = requestDigest;
}

public String getVendorTransLogId() {
return vendorTransLogId;
}

public void setVendorTransLogId(String vendorTransLogId) {
this.vendorTransLogId = vendorTransLogId;
}

public String getClientTransId() {
return clientTransId;
}

public void setClientTransId(String clientTransId) {
this.clientTransId = clientTransId;
}

public UserInfo getUserInfo() {
return userInfo;
}

public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}

public BusinessInfo getBusinessInfo() {
return businessInfo;
}

public void setBusinessInfo(BusinessInfo businessInfo) {
this.businessInfo = businessInfo;
}

public ApplicationInfo getApplicationInfo() {
return applicationInfo;
}

public void setApplicationInfo(ApplicationInfo applicationInfo) {
this.applicationInfo = applicationInfo;
}
public ResponseData getResponse() {
return response;
}

public void setResponse(ResponseData response) {
this.response = response;
}

public Date getCreated() {
return created;
}

public void setCreated(Date created) {
this.created = created;
}

public Date getUpdated() {
return updated;
}

public void setUpdated(Date updated) {
this.updated = updated;
}

public Map<String, String> getZootAttributes() {
return zootAttributes;
}

public void setZootAttributes(Map<String, String> zootAttributes) {
this.zootAttributes = zootAttributes;
}
}

Which has @Embedded annotation on UserInfo. Does that a issue.??

Justin Lee

unread,
Mar 10, 2016, 10:54:23 AM3/10/16
to mor...@googlegroups.com
That was my first thought, too, when seeing this but that doesn't seem to be the case.  I just updated a unit test to validate that @Embedded classes are mapped properly.  You can see that test here:  https://github.com/mongodb/morphia/blob/84a6813f0a2b1b4d0ade44db92f8137506b505ba/morphia/src/test/java/org/mongodb/morphia/TestDAO.java#L71-L71

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "mongodb.com", "antwerkz.com" ],
  location : "New York, NY" }

Chiranjeevi Veerni

unread,
Mar 10, 2016, 12:30:15 PM3/10/16
to Morphia
Ok Justin Lee. Any other guess on what could be the issue ?

I could see there is one more  @PrePersist annotation they are calculating hash of userInfo object and nothing more than that.
...

Chiranjeevi Veerni

unread,
Mar 10, 2016, 2:26:56 PM3/10/16
to Morphia
hey justing.

I just removed @Embedded annotation for UserInfo class and magically its working fine.

...

Justin Lee

unread,
Mar 10, 2016, 2:38:15 PM3/10/16
to mor...@googlegroups.com
In that unit test both the field and the embedded class have @Embedded.  Try reinstating the annotation and see if the field comes back.  I'm hoping it doesn't but if it does then there could very well be an issue in Morphia.

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "mongodb.com", "antwerkz.com" ],
  location : "New York, NY" }

--

Chiranjeevi Veerni

unread,
Mar 15, 2016, 7:45:41 AM3/15/16
to Morphia
Hi Justin you are correct.

Its not because of removing embedded annotation. I intentionally made the field null after we calculate a hash of the object for a cache lookup . I forgot that i made that change and though removing embedded has made the fix. So making the element null is a temporary fix i need to fix this by using transient only. Dont know why this is taking so long to get it fixed. Everything seems fine to me. Took all latest versions but still same issue.
...
Reply all
Reply to author
Forward
0 new messages