Using transient for both Gson and Hibernate in the same file











up vote
0
down vote

favorite












I have entity that I need some of the fields not to be persisted and some of the fields not to be serialized.



I am using the @Transient on some of the fields but when I want to mark transient for the Gson. The issue is that hibernate picks it up and also not persist it since its also a keyword in Hibernate .



I use Hibernate-jpa-2.1-api javax.persistence.Transient



I am trying prevent addresses from being serialized and getDefaultAddress should not be saved.



Code:



@Entity
@Table(name="Business")
public class Business{

@OneToMany(mappedBy="business")
private transient List<Phone> addresses;

@Transient
public Phone getDefaultPhone() {
return phones.get(0);
}
}


Any solution?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have entity that I need some of the fields not to be persisted and some of the fields not to be serialized.



    I am using the @Transient on some of the fields but when I want to mark transient for the Gson. The issue is that hibernate picks it up and also not persist it since its also a keyword in Hibernate .



    I use Hibernate-jpa-2.1-api javax.persistence.Transient



    I am trying prevent addresses from being serialized and getDefaultAddress should not be saved.



    Code:



    @Entity
    @Table(name="Business")
    public class Business{

    @OneToMany(mappedBy="business")
    private transient List<Phone> addresses;

    @Transient
    public Phone getDefaultPhone() {
    return phones.get(0);
    }
    }


    Any solution?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have entity that I need some of the fields not to be persisted and some of the fields not to be serialized.



      I am using the @Transient on some of the fields but when I want to mark transient for the Gson. The issue is that hibernate picks it up and also not persist it since its also a keyword in Hibernate .



      I use Hibernate-jpa-2.1-api javax.persistence.Transient



      I am trying prevent addresses from being serialized and getDefaultAddress should not be saved.



      Code:



      @Entity
      @Table(name="Business")
      public class Business{

      @OneToMany(mappedBy="business")
      private transient List<Phone> addresses;

      @Transient
      public Phone getDefaultPhone() {
      return phones.get(0);
      }
      }


      Any solution?










      share|improve this question















      I have entity that I need some of the fields not to be persisted and some of the fields not to be serialized.



      I am using the @Transient on some of the fields but when I want to mark transient for the Gson. The issue is that hibernate picks it up and also not persist it since its also a keyword in Hibernate .



      I use Hibernate-jpa-2.1-api javax.persistence.Transient



      I am trying prevent addresses from being serialized and getDefaultAddress should not be saved.



      Code:



      @Entity
      @Table(name="Business")
      public class Business{

      @OneToMany(mappedBy="business")
      private transient List<Phone> addresses;

      @Transient
      public Phone getDefaultPhone() {
      return phones.get(0);
      }
      }


      Any solution?







      java hibernate gson transient






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 14 '15 at 10:31

























      asked Jun 14 '15 at 3:22









      special0ne

      2,520135595




      2,520135595
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can use @Expose



          @Expose(serialize = false)
          @OneToMany(cascade=CascadeType.ALL, mappedBy="business")
          private List<Address> addresses;


          In order to be able to work with this annotation,



          final GsonBuilder builder = new GsonBuilder();
          builder.excludeFieldsWithoutExposeAnnotation();
          final Gson gson = builder.create();


          Source : http://www.javacreed.com/gson-annotations-example/






          share|improve this answer




























            up vote
            0
            down vote













            I am using Play Framework's implementation of JPA, and Gson.



            I got it working with JPA class as follows, which involved constructing the embedded object dynamically before marshalling to json, and no changes were required for the GSON builder, as modifying GSONBuilder will change the strategy globally for other classes as well.



            public class DBServiceDefinition {
            @Transient
            @Expose(serialize = true)
            @SerializedName("serviceStatus")
            public ServiceDefinitionStatus status = new
            ServiceDefinitionStatus(201,"SUCCESS","","Test");
            }





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30825669%2fusing-transient-for-both-gson-and-hibernate-in-the-same-file%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              You can use @Expose



              @Expose(serialize = false)
              @OneToMany(cascade=CascadeType.ALL, mappedBy="business")
              private List<Address> addresses;


              In order to be able to work with this annotation,



              final GsonBuilder builder = new GsonBuilder();
              builder.excludeFieldsWithoutExposeAnnotation();
              final Gson gson = builder.create();


              Source : http://www.javacreed.com/gson-annotations-example/






              share|improve this answer

























                up vote
                0
                down vote













                You can use @Expose



                @Expose(serialize = false)
                @OneToMany(cascade=CascadeType.ALL, mappedBy="business")
                private List<Address> addresses;


                In order to be able to work with this annotation,



                final GsonBuilder builder = new GsonBuilder();
                builder.excludeFieldsWithoutExposeAnnotation();
                final Gson gson = builder.create();


                Source : http://www.javacreed.com/gson-annotations-example/






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You can use @Expose



                  @Expose(serialize = false)
                  @OneToMany(cascade=CascadeType.ALL, mappedBy="business")
                  private List<Address> addresses;


                  In order to be able to work with this annotation,



                  final GsonBuilder builder = new GsonBuilder();
                  builder.excludeFieldsWithoutExposeAnnotation();
                  final Gson gson = builder.create();


                  Source : http://www.javacreed.com/gson-annotations-example/






                  share|improve this answer












                  You can use @Expose



                  @Expose(serialize = false)
                  @OneToMany(cascade=CascadeType.ALL, mappedBy="business")
                  private List<Address> addresses;


                  In order to be able to work with this annotation,



                  final GsonBuilder builder = new GsonBuilder();
                  builder.excludeFieldsWithoutExposeAnnotation();
                  final Gson gson = builder.create();


                  Source : http://www.javacreed.com/gson-annotations-example/







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 14 '15 at 3:59









                  Darshan Patel

                  1,91611529




                  1,91611529
























                      up vote
                      0
                      down vote













                      I am using Play Framework's implementation of JPA, and Gson.



                      I got it working with JPA class as follows, which involved constructing the embedded object dynamically before marshalling to json, and no changes were required for the GSON builder, as modifying GSONBuilder will change the strategy globally for other classes as well.



                      public class DBServiceDefinition {
                      @Transient
                      @Expose(serialize = true)
                      @SerializedName("serviceStatus")
                      public ServiceDefinitionStatus status = new
                      ServiceDefinitionStatus(201,"SUCCESS","","Test");
                      }





                      share|improve this answer



























                        up vote
                        0
                        down vote













                        I am using Play Framework's implementation of JPA, and Gson.



                        I got it working with JPA class as follows, which involved constructing the embedded object dynamically before marshalling to json, and no changes were required for the GSON builder, as modifying GSONBuilder will change the strategy globally for other classes as well.



                        public class DBServiceDefinition {
                        @Transient
                        @Expose(serialize = true)
                        @SerializedName("serviceStatus")
                        public ServiceDefinitionStatus status = new
                        ServiceDefinitionStatus(201,"SUCCESS","","Test");
                        }





                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          I am using Play Framework's implementation of JPA, and Gson.



                          I got it working with JPA class as follows, which involved constructing the embedded object dynamically before marshalling to json, and no changes were required for the GSON builder, as modifying GSONBuilder will change the strategy globally for other classes as well.



                          public class DBServiceDefinition {
                          @Transient
                          @Expose(serialize = true)
                          @SerializedName("serviceStatus")
                          public ServiceDefinitionStatus status = new
                          ServiceDefinitionStatus(201,"SUCCESS","","Test");
                          }





                          share|improve this answer














                          I am using Play Framework's implementation of JPA, and Gson.



                          I got it working with JPA class as follows, which involved constructing the embedded object dynamically before marshalling to json, and no changes were required for the GSON builder, as modifying GSONBuilder will change the strategy globally for other classes as well.



                          public class DBServiceDefinition {
                          @Transient
                          @Expose(serialize = true)
                          @SerializedName("serviceStatus")
                          public ServiceDefinitionStatus status = new
                          ServiceDefinitionStatus(201,"SUCCESS","","Test");
                          }






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 21 at 7:27









                          piet.t

                          9,86863245




                          9,86863245










                          answered Aug 31 at 22:19









                          kanaparthikiran

                          35159




                          35159






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30825669%2fusing-transient-for-both-gson-and-hibernate-in-the-same-file%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Berounka

                              Sphinx de Gizeh

                              Different font size/position of beamer's navigation symbols template's content depending on regular/plain...