Springboot JPA Table Association query __springboot

Source: Internet
Author: User
Tags generator
today we are going to show you how to use JPA to implement table association queries. Today we're going to give you a one-to-many query, and it's implemented using the JPA native FindBy statement. There are two entity classes in total, one is floor (commodity floor class) and the other is floorcontent (commodity floor content table). Here's a look at the source code for two tables: Floor class:
Package Cms.model;
Import Cms.model.base.BaseDomain;

Import Org.hibernate.annotations.GenericGenerator;
Import javax.persistence.*;
Import java.io.Serializable;
Import java.util.List;
 /** * Created by Roney on 2016/10/10. * Floor Management */@Entity @Table (indexes = {@Index (name = "Idx_floor_user", columnlist = "user_id")}) public class floor Extend S Basedomain implements Serializable {@Id @GenericGenerator (name = "Pkuuid", strategy = "Uuid2") @GeneratedV

    Alue (generator = "Pkuuid") @Column (length = =) protected String ID;

    /** * Publish User ID/@Column (length = 36,name = "user_id") private String userId;

    /** * Floor name */private String name;

    /** * Floor Template path * * Private String Templateurl;


    /** * Type * 1 Management End * 2. Supplier/private Integer type;

    /** * Sort/@Column (name = "Show_index", Nullable = False) private Integer showindex; /** * is disabled */@Column (nUllable = False) private Boolean isdisable=false;

    @OneToMany (fetch = Fetchtype.lazy,mappedby = "Floor") private list<floorcontent> floorcontents;
    Public list<floorcontent> getfloorcontents () {return floorcontents;
    public void Setfloorcontents (list<floorcontent> floorcontents) {this.floorcontents = floorcontents;
    Public String GetId () {return id;
    public void SetId (String id) {this.id = ID;
    Public String GetUserID () {return userId;
    } public void Setuserid (String userId) {this.userid = UserId;
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    Public String Gettemplateurl () {return templateurl;
    } public void Settemplateurl (String templateurl) {this.templateurl = Templateurl; Public Integer Getshowindex () {return SHowindex;
    The public void Setshowindex (Integer showindex) {this.showindex = Showindex;
    Public Boolean getdisable () {return isdisable;
    } public void Setdisable (Boolean disable) {isdisable = disable;
        @Override public boolean equals (Object o) {if (this = O) return true;

        if (o = = NULL | | getclass ()!= O.getclass ()) return false;

        Floor Floor = (Floor) o; Return ID!= null?

    Id.equals (floor.id): Floor.id = = null;
    @Override public int hashcode () {return id!= null id.hashcode (): 0;
 }
}
Floorcontent class:
Package Cms.model;
Import Cms.model.base.BaseDomain;

Import Org.hibernate.annotations.GenericGenerator;
Import javax.persistence.*;

Import java.io.Serializable;
 /** * Created by Roney on 2016/10/10. * Floor content/@Entity @Table (indexes = {@Index (name = "Idx_floor_content_user", columnlist = "user_id")}) public class Floo
    Rcontent extends Basedomain implements Serializable {@Id @GenericGenerator (name = "Pkuuid", strategy = "Uuid2")

    @GeneratedValue (generator = "Pkuuid") @Column (length = =) protected String ID;

    /** * Publish User ID/@Column (length = +, name = "user_id") private String userId;

    /** * Content name is called/private String name;

    /** * * Content images/@Column (length = 256) private String contentimageurl;

    /** * Type * 1. Super link * 2. Picture check/private Integer type;

    /** * Hyperlink URL */Private String Linkurl; /** * Picture Sonejon/Private String picsearchcontent;

    /** * Sort/@Column (name = "Show_index", Nullable = False) private Integer showindex;

    /** * Whether to disable */@Column (nullable = False) Private Boolean isdisable = false;

    @ManyToOne @JoinColumn (name = "floor_id", ForeignKey = @ForeignKey (name = "Fk_floor_fc")) private floor floor;
    Public Floor Getfloor () {return Floor;
    public void Setfloor (Floor Floor) {this.floor = Floor;
    Public String GetId () {return id;
    public void SetId (String id) {this.id = ID;
    Public String GetUserID () {return userId;
    } public void Setuserid (String userId) {this.userid = UserId;
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    Public String Getcontentimageurl () {return contentimageurl;
 } public void Setcontentimageurl (String contentimageurl) {       This.contentimageurl = Contentimageurl;
    Public Integer GetType () {return type;
    The public void SetType (Integer type) {this.type = type;
    Public String Getlinkurl () {return linkurl;
    } public void Setlinkurl (String linkurl) {this.linkurl = Linkurl;
    Public String getpicsearchcontent () {return picsearchcontent;
    } public void Setpicsearchcontent (String picsearchcontent) {this.picsearchcontent = picsearchcontent;
    Public Integer Getshowindex () {return showindex;
    The public void Setshowindex (Integer showindex) {this.showindex = Showindex;
    Public Boolean getdisable () {return isdisable;
    } public void Setdisable (Boolean disable) {isdisable = disable;
        @Override public boolean equals (Object o) {if (this = O) return true; if (o = = NULL | | getclass ()!= O.getclass ()) return False;

        Floorcontent that = (floorcontent) o; Return ID!= null?

    Id.equals (that.id): That.id = = null;
    @Override public int hashcode () {return id!= null id.hashcode (): 0;
 }
}
The entity class has come out, now specifically how to implement the associated query using JPA findby:
Package cms.model.repository;
Import Cms.model.Floor;
Import cms.model.FloorContent;
Import Org.springframework.data.domain.Page;
Import org.springframework.data.domain.Pageable;
Import org.springframework.data.jpa.repository.JpaRepository;
/**
 * Created by Roney on 2016/10/10.
 * Created by Roney on 2016/10/10.
 * Floor Content Management DAO class
 * * Public
interface Floorcontentrepos extends jparepository<floorcontent,string>{
    Public page<floorcontent> FINDBYFLOOR_IDANDISDELETEORDERBYSHOWINDEXASC (String Floorid,boolean B, Pageable pageable);
}
We can see from the example that the JPA Association query is mainly "_"The use of this symbol, below to give you a specific description of what this symbol really means. first, the FindBy must be written to indicate that a query is made using JPA rules. If you are querying the contents of this table, such as querying the Name field in this table, you can write this: Findbyname (). If you are querying the Name field in the floor, you can write this: Findbyfloor_name (). if you want to query the Name field in this table as well as the name field in the floor, you can write this: Findbyfloor_nameandname (). as you can see from the case above, you can add the entity classes that you want to associate with the FindBy, and then write "_" after the entity class and the "_" symbol to add the field of the associated table instead of the table itself, remember. How to also want to associate more tables can be added later: and+ table name + "_" + table in the field to query. Or just want to associate itself with the query field can be added later: and+ query fields.
Do not write wrong, write wrong words run can not run up. So write more to see if they conform to the rules.
If you have any questions or doubts about the article, you can add my subscription number in the above message, the subscription number above I will regularly update the latest blog. If it's too much trouble, you can just add me wechat:lzqcode


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.