Hibernate and Jackson

Source: Internet
Author: User

When there is a one-to-many, multi-class relationship, use Hibernate to query and turn Jackson into JSON


For example, persistent class user

public class User implements java.io.serializable{private string ID;    Private set<usertorole> Usertoroles=new hashset<usertorole> (0); Getter Setter ...}


Its hibernate configuration file User.hbm.xml

<id name= "id" type= "java.lang.String" > <column name= "id" length= "ten"/> <generator class= "Assigned" ; </generator></id><set name= "Usertoroles" table= "Usertorole" inverse= "true" lazy= "false" Fetch= " Select "> <key> <column name=" ID "/> </key> <one-to-many class=" Com.xxx.entity.Usert Orole "/></set>


and Persistence class Usertorole

public class Usertorole implements java.io.serializable{private string ID;    private user user; Getter Setter ...}


By User.hbm.xml, the user class is associated with a one-to-many and Usertorole class, and in order to fetch the value of the Usertorole collection, it also sets the

Lazy= "false"

Run times wrong, dead loop, wait a long time before overflow stop.


Then Jackson's annotations are about to work.


Add annotations above the user property of the Usertorole class @jsonbackreference

That

public class Usertorole implements java.io.serializable{private string ID;    @JsonBackReference Private User User; Getter Setter ...}

It's OK to run again. This is to prevent this infinite loop.

This article is from the "under the Tree Beast" blog, please make sure to keep this source http://shuxiayeshou.blog.51cto.com/4452347/1866766

Hibernate and Jackson

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.