SpringBoot JPA table associates query instances

  • 2020-06-19 10:26:25
  • OfStack

Today I'll show you how to implement table correlation queries using JPA.

Today I'll show you a one-to-many associated query implemented using the JPA native findBy statement.

There are a total of two physical classes in the example, one Floor (commodity floor class) and one FloorContent (Commodity floor content table). Here's the source code for the 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 extends BaseDomain implements Serializable { 
 
  @Id 
  @GenericGenerator(name = "PKUUID", strategy = "uuid2") 
  @GeneratedValue(generator = "PKUUID") 
  @Column(length = 36) 
  protected String id; 
 
  /** 
   *  Release the user ID 
   */ 
  @Column(length = 36,name = "user_id") 
  private String userId; 
 
  /** 
   *  Name of floor  
   */ 
  private String name; 
 
  /** 
   *  Template paths for floors  
   */ 
  private String templateUrl; 
 
  /** 
   *  type  
   * 1. The management end  
   * 2. supplier  
   */ 
  private Integer type; 
 
 
  /** 
   *  The sorting  
   */ 
  @Column(name = "show_index", nullable = false) 
  private Integer showIndex; 
 
  /** 
   *  Whether to disable  
   * */ 
 
 
  @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; 
  } 
 
  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. 
 *  Contents of the floor  
 */ 
 
@Entity 
@Table(indexes = {@Index(name = "idx_floor_content_user", columnList = "user_id")}) 
public class FloorContent extends BaseDomain implements Serializable { 
 
  @Id 
  @GenericGenerator(name = "PKUUID", strategy = "uuid2") 
  @GeneratedValue(generator = "PKUUID") 
  @Column(length = 36) 
  protected String id; 
 
  /** 
   *  Release the user ID 
   */ 
  @Column(length = 36, name = "user_id") 
  private String userId; 
 
  /** 
   *  � � volume name  
   */ 
  private String name; 
 
  /** 
   * 
   *  � � piece  
   */ 
  @Column(length = 256) 
  private String contentImageUrl; 
 
  /** 
   *  � type  
   * 1. Super � meet  
   * 2. � � cable  
   */ 
  private Integer type; 
 
  /** 
   *  Super � meet url 
   */ 
  private String linkUrl; 
 
  /** 
   *  Video and entertainment  
   */ 
  private String picSearchContent; 
 
  /** 
   *  The sorting  
   */ 
  @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; 
  } 
 
  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; 
  } 
 
  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; 
  } 
} 

Now that the entity class is out, let's talk more about how to use findBy in JPA to implement associated queries:


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); 
} 

It can be seen from the example that JPA correlation query is mainly used in the use of the symbol "_". The following will give you a specific introduction to the meaning of the symbol 1.

First, findBy is mandatory, indicating that queries are made using the JPA rule.

If you are querying the contents of this table, such as the name field in this table, you can write findByName().

If the query is for the name field in the floor, write findByFloor_Name().

If you want to query both the name field in this table and the name field in the floor, write findByFloor_NameAndName().

As you can see from the above example, you can add the entity class you want to associate with findBy, and then write "_" after the entity class. Remember that the "_" symbol is followed by the fields for which the associated table is added, not the fields for the table itself. How to associate more tables can be added later: And+ table name + "_" + fields to query in the table. Or just to associate query fields of its own, you can add: And+ query fields.

Don't make a mistake. If you make a mistake, it won't work. So when you write it, look for rules.


Related articles: