Explanation of @transient, @JsonIgnoreProperties, @JsonIgnore, @JsonFormat, @JsonSerialize and other annotations in Hibernate JPA

Source: Internet
Author: User
<span id="Label3"></p><p><p>The role of @jsonignore<br>The effect is that JSON serialization ignores some attributes in the Java bean, and both serialization and deserialization are Affected.</p></p><p><p>Http://www.cnblogs.com/toSeeMyDream/p/4437858.html<br><br><br>When there are one2many or many2one between the tables, there will be infinite loops of the scene, how to break?</p></p><p><p>Just add the following annotations before the set method:</p></p><pre><pre> @JsonIgnore public Set xxxs () { return this.xxxyyyy; }</pre></pre><p><p><strong>Http://www.cnblogs.com/tompandas/p/4618668.html</strong></p></p><p><p></p></p><p><p></p></p><p><p><strong>1. @Transient</strong></p></p><p><p>@Transient indicates that the property is not a mapping to a field in a database table, and the ORM framework ignores the property;<br>If a property is not a field mapping for a database table, it must be marked as @transient, otherwise the ORM framework defaults to its annotation as @basic;</p></p><p><p>Indicates that the field is not in the database table</p></p><p><p>@Transient<br>public int Getage () {<br>return;<br>}</p></p><p><p></p></p><p><p><strong>Jackson Related:</strong></p></p><p><p><strong>2. @JsonIgnoreProperties</strong></p></p><p><p>This annotation is a class annotation that, when JSON is serialized, ignores some properties in the Java bean, and both serialization and deserialization are Affected.</p></p><p><p></p></p><p><p><strong>3. @JsonIgnore</strong></p></p><p><p>This annotation is used on properties or methods (preferably on attributes), as in the @jsonignoreproperties Above.</p></p><p><p></p></p><p><p><strong>4. @JsonFormat</strong></p></p><p><p>This annotation is used for attributes or methods (preferably on attributes), and it is convenient to convert the date type directly into the pattern we want, such as @jsonformat (pattern = "yyyy-mm-dd Hh-mm-ss")</p></p><p><p></p></p><p><p><strong>5. @JsonSerialize</strong></p></p><p><p>This annotation is used on attributes or Getter methods to embed our custom code when serializing, such as when serializing a double with a two-bit decimal point behind it.</p></p><p><p></p></p><p><p><strong>6. @JsonDeserialize</strong></p></p><p><p>This annotation is used on attributes or setter methods, and can be used to embed our custom code when deserializing, similar to the above @jsonserialize</p></p><p><p>Http://www.cnblogs.com/guijl/p/3855329.html</p></p><p><p></p></p>Hibernate lazy Loading and JSON serialization conflicts<p><p>Suppose a POJO has the following attributes:</p></p><pre><pre>Private set<user> users = new hashset<> (0), @OneToMany (fetch = fetchtype.lazy, Mappedby = "xuser") public SET&L T user> getusers () { return this.users;}</pre></pre><p><p>If we use Jackson to serialize it, the runtime will get an error:</p></p><pre><pre>Failed to lazily initialize a collection of role ...</pre></pre><p><p></p></p><p><p><strong>Workaround one:</strong></p></p><p><p>Using Hibernate's opensessioninviewfilter to make the Fetchtype LAZY property empty at serialization, Add the code in Web. XML as Follows:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre><filter> <filter-name>openSession</filter-name> <filter-class> Org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>false</param-value> </init-param> </filter> <filter-mapping> <filter-name>opensession</ filter-name> <url-pattern>/*</url-pattern> </filter-mapping></pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Although the users are empty, the fields remain, corresponding to the Output:</p></p><pre><pre>{..., "users": [],...}</pre></pre><p><p></p></p><p><p><strong>Workaround Two:</strong></p></p><p><p>The comment @JsonIgnore before the Get method of the property, so the field is ignored when converting to JSON:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>Import com.fasterxml.jackson.annotation.JsonIgnore;</pre></pre><pre><pre></pre></pre><pre><pre>...</pre></pre><pre><pre>Private set<user> users = new hashset<> (0), @JsonIgnore @onetomany (fetch = fetchtype.lazy, Mappedby = "xuser") Public set<user> getusers () { return this.users;}</pre></pre><pre><pre>...</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Note that the class introduced is com.fasterxml.jackson.annotation.JsonIgnore, and if the use of Org.codehaus.jackson.annotate.JsonIgnore is not effective, see Spring @ Jsonignore not Working.</p></p><p><p></p></p><p><p><strong>Workaround three:</strong></p></p><p><p><span style="font-family: Consolas;">fetch = Fetchtype.lazy</span> is changed to <span style="font-family: Consolas;">fetch = Fetchtype.eager</span>, but this causes every query database to immediately extract all Onetomany objects, so it is <span style="color: #ff0000;">not recommended </span>.</p></p><p><p>Http://www.cnblogs.com/gugia/p/5117735.html</p></p><p><p></p></p><p><p>Because lazy loading of this object property is just a proxy object, if the JSON directly as an existing property to serialize the error will occur, so this is the only way, of course, there are other ways.</p></p><p><p>or add it to class.</p></p><p><p>@JsonIgnoreProperties (value={"hibernatelazyinitializer", "handler", "fieldhandler"})<br>public class Productprice {</p></p><p><p>}</p></p><p><p>or add @JsonIgnore annotations to the properties of this object</p></p><p><p>@JsonIgnore<br>Public set<user> getusers () {<br>Return this.users;<br>}</p></p><p><p>The real reason we're going to do this is to ignore this attribute when mappingjackson2httpmessageconverter through AOP into Json.</p></p><p><p>Http://www.cnblogs.com/cbread/p/4017987.html</p></p><p><p>The effect is that JSON serialization ignores some attributes in the Java bean, and both serialization and deserialization are Affected.</p></p><p><p>As follows:</p></p><pre>Package Com.hzboy.orm;import Java.util.list;import Javax.persistence.cascadetype;import javax.persistence.Column; Import Javax.persistence.entity;import Javax.persistence.fetchtype;import Javax.persistence.joincolumn;import Javax.persistence.jointable;import Javax.persistence.manytomany;import Javax.persistence.manytoone;import Javax.persistence.table;import Javax.persistence.transient;import org.codehaus.jackson.annotate.JsonIgnore; Import Org.codehaus.jackson.annotate.jsonignoreproperties;import org.codehaus.jackson.annotate.jsonmanagedreference;/** * System User entity class * @author Tinner * */@Entity (name = " Com.hzboy.orm.User ") @Table (name =" sys_user ") @JsonIgnoreProperties ({" hibernatelazyinitializer "," Handler "}) public Class User extends baseentity {/** * */private static final long Serialversionuid = -1343842065506227012l ; Public user () {} public user (Integer Id) {this.id = id; }//logon Name Private String loginName; Password 123456-b594510740d2ac4261c1b2fe87850d08 Private String pswd; Name Private String nickname; Sex private Short sex; Age Section Private String agestage; Whether the system administrator private Boolean systemadmin; Contact Phone Private String tel; Email Private String email; Whether the engineer private Boolean isenginner; Whether the front-end customer is the foreground HTML is displayed, but also retains the background related module private Boolean isfrontuser; Relevant department private Department Department; Related department front desk description private String departmentname; Private Integer departmentid; User number Private String usercode; Append for importing private String idcode; Additional departmental information is used to calculate the private String departmentcode; @Column (name = "login_name") public String getloginname () {return loginName; } public void Setloginname (String LoginName) {this.loginname = loginName; } @Column (name = "pswd") public String getpswd () {return pswd; } public void setpswd (String pswd) {this.pswd = pswd; } @Column (name = "nick_name"Public String getnickname () {return nickname; } public void Setnickname (String Nickname) {this.nickname = nickname; } @Column (name = "sex") public short getsex () {return sex; } public void Setsex (short Sex) {this.sex = sex; } @Column (name = "age_stage") public String getagestage () {return agestage; } public void Setagestage (String Agestage) {this.agestage = agestage; } @Column (name = "system_admin") public boolean issystemadmin () {return systemadmin; The public void setsystemadmin (boolean systemadmin) {this.systemadmin = systemadmin; } Private List<role> roles; @ManyToMany (fetch = fetchtype.lazy, cascade = (cascadetype.remove)) @JoinTable (name = "sys_user_role", Joincolumns = { @JoinColumn (name = "user_id")}, Inversejoincolumns = {@JoinColumn (name = "role_id")}) public list<role> Getrol Es () {return roles; } public void Setroles (list<role> roles) {this.roles = roles; } Private integer[] roleids; @Transient public integer[] getroleids () {return roleids; } public void Setroleids (integer[] Roleids) {this.roleids = roleids; } @Column (name= "email") public String getemail () {return email; } public void Setemail (String Email) {this.email = email; } @Column (name= "is_enginner") public Boolean getisenginner () {return isenginner; The public void Setisenginner (Boolean isenginner) {this.isenginner = isenginner; } @Column (name= "is_front_user") public Boolean getisfrontuser () {return isfrontuser; The public void Setisfrontuser (Boolean isfrontuser) {this.isfrontuser = isfrontuser; } public String Gettel () {return tel; public void Settel (String Tel) {this.tel = tel; } @JsonIgnore @ManyToOne (cascade = {cascadetype.persist, cascadetype.merge}, FETch = fetchtype.lazy) @JoinColumn (name= "department_id") Public Department getdepartment () {return Departmen T } @JsonManagedReference public void setdepartment (Department Department) {this.department = Department; } @Transient public String getdepartmentname () {return departmentname; } public void Setdepartmentname (String Departmentname) {this.departmentname = departmentname; } @Transient public Integer getdepartmentid () {return departmentid; } public void Setdepartmentid (Integer departmentid) {this.departmentid = departmentid; } @Column (name= "user_code") public String getusercode () {return usercode; } public void Setusercode (String Usercode) {this.usercode = usercode; } @Column (name= "id_from_import") public String getidcode () {return idcode; } public void Setidcode (String Idcode) {this.idcode = idcode; } public String Getdepartmentcode () {return departmentcode; } public void Setdepartmentcode (String Departmentcode) {this.departmentcode = departmentcode; }}</pre><p><p>The department attribute in the last entity user adds a @JsonIgnore to the getdepartment attribute, which is ignored when the data is pushed from the background to the foreground, department the reference Property.</p></p><p><p>Http://www.mamicode.com/info-detail-578520.html</p></p><p><p></p></p><p><p>Explanation of @transient, @JsonIgnoreProperties, @JsonIgnore, @JsonFormat, @JsonSerialize and other annotations in Hibernate JPA</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.