JPA Transient Annotation and JSON

Solution 1:

I simply added JsonSerialize and JsonDeserialize annotations.

@Transient
@JsonSerialize
@JsonDeserialize
private String myField;

Solution 2:

Contrary to what I was telling you in comments, it seems that Jackson does care about JPA annotations when used to serialize instances of entity classes thanks to the Jackson's Hibernate module.

Within that module, there is an HibernateAnnotationIntrospector that the documentation refers to as a

simple AnnotationIntrospector that adds support for using Transient to denote ignorable fields (alongside with Jackson and/or JAXB annotations).

And as you can see here, the default behavior of Jackson is to check for any @Transient annotation it can find.

So in the end, your problem can be solved in either of those 3 ways :

  1. Configure Jackson (using HibernateAnnotationIntrospector's setUseTransient method) to disable the check for @Transient annotations (see this answer for implementation details).
  2. Use another object than PublicationVO as the returned result of your getPublicationDetailsJSON method. You'll have to copy properties from your value object to the object being returned at some point.
  3. Remove the @Transient annotation and persist the property (but I would understand if that is not an option for you since you probably have good reason to have made this property JPA-transient in the first place).

Cheers

Solution 3:

Just to add further to the answer provided by m4rtin

I went with the first approach - Configure Jackson (using HibernateAnnotationIntrospector's setUseTransient method) to disable the check for @Transient annotations.

In my project I follwed had to follow the following thread to avoid jackson serialization on non fetched lazy objects Avoid Jackson serialization on non fetched lazy objects

To configure my project to not ignore transient annotations, I set up the Hibernate4Module as follows

        Hibernate4Module hm = new Hibernate4Module();
    hm.disable(Feature.USE_TRANSIENT_ANNOTATION);

Thanks for your help on this m4rtin

Solution 4:

I use both @Transient and @JsonProperty, then it works!

@Transient
@JsonProperty
private String mapImageSrc;