JPA Transient Annotation and JSON

后端 未结 10 1399
无人共我
无人共我 2020-11-29 03:03

This is a follow up to the following question on the JPA Transient annotation Why does JPA have a @Transient annotation?

I have a transient variable that I do not wa

相关标签:
10条回答
  • 2020-11-29 03:12

    What it worked for me:

    @Transient
    @JsonProperty
    

    in the GETTER (not in the private field definition)

    AND

    @JsonAutoDetect(fieldVisibility = Visibility.ANY) 
    

    annotating the class

    0 讨论(0)
  • 2020-11-29 03:19

    I simply added JsonSerialize and JsonDeserialize annotations.

    @Transient
    @JsonSerialize
    @JsonDeserialize
    private String myField;
    
    0 讨论(0)
  • 2020-11-29 03:20

    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

    0 讨论(0)
  • 2020-11-29 03:20

    I had the same problem. Below solution worked for me:

    @Bean
    public Module hibernate5Module() {
        Hibernate5Module hnetModule = new Hibernate5Module();
        hnetModule.disable(Hibernate5Module.Feature.USE_TRANSIENT_ANNOTATION);
        return hnetModule;
    }
    

    Thanks to m4rtin.

    0 讨论(0)
  • 2020-11-29 03:21

    In my case only works with this:

    @Transient
    @JsonGetter(value = "transientProperty")
    public String getSomething() {
        return something;
    }
    

    I hope this can be useful for someone. Regards.

    0 讨论(0)
  • 2020-11-29 03:25

    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

    0 讨论(0)
提交回复
热议问题