Spring Jpa Specification Join查询

属性

是否必须

说明

name

指定该连接表的表名

JoinColumns

该属性值可接受多个@JoinColumn,用于配置连接表中外键列的信息,这些外键列参照当前实体对应表的主键列

inverseJoinColumns

该属性值可接受多个@JoinColumn,用于配置连接表中外键列的信息,这些外键列参照当前实体的关联实体对应表的主键列

targetEntity

该属性指定关联实体的类名。在默认情况下,Hibernate将通过反射来判断关联实体的类名

catalog

设置将该连接表放入指定的catalog中。如果没有指定该属性,连接表将放入默认的catalog

schema

设置将该连接表放入指定的schema中。如果没有指定该属性,连接表将放入默认的schema

uniqueConstraints

该属性用于为连接表增加唯一约束

indexes

该属性值为@Index注解数组,用于为该连接表定义多个索引

@Entity
@Table(name = "db1_News_Files", schema = "dbo")
@IdClass(Db1NewsFilesEntityPK.class)
public class Db1NewsFilesEntity  {
    private String appId;
    private String fileId;
    /....省略.../
    private Db1NewsItemsEntity newsItem;

    @Id
    @Column(name = "appid", nullable = false, length = 50)
    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    @Id
    @Column(name = "fileId", nullable = false, length = 50)
    public String getFileId() {
        return fileId;
    }

    public void setFileId(String fileId) {
        this.fileId = fileId;
    }
     /....省略.../
    @ManyToOne()
    @JoinTable(name="db1_News_Items",
            inverseJoinColumns = {
                @JoinColumn(name = "AppId",referencedColumnName="appId",insertable = false,updatable = false)
                ,@JoinColumn(name = "ItemId",referencedColumnName="itemId",insertable = false,updatable = false)
            })
    public Db1NewsItemsEntity getNewsItem() {
        return newsItem;
    }

    public void setNewsItem(Db1NewsItemsEntity newsItem) {
        this.newsItem = newsItem;
    }
@Entity
@Table(name = "db1_News_Items", schema = "dbo")
@IdClass(Db1NewsItemsEntityPK.class)
public class Db1NewsItemsEntity {
	private String appId;
	private String itemId;
    /....省略...../
	private List folders;

	@Id
	@Column(name = "appid", nullable = false, length = 50)
	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	@Id
	@Column(name = "itemid", nullable = false, length = 50)
	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	/....省略...../

	@OneToMany()
	@JoinTable(name="db1_News_Folders",
			inverseJoinColumns = {
				@JoinColumn(name = "AppId", referencedColumnName = "appId", insertable = false, updatable = false),
				@JoinColumn(name = "FolderId", referencedColumnName = "FolderId", insertable = false, updatable = false)
			})
	public List getFolders() {
		return folders;
	}

	public void setFolders(List folders) {
		this.folders = folders;
	}

	
}
private Specification countByAppidAndKwAndSearchId(String appId, String wk , String searchId){
        return new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
                List list = new ArrayList<>();
                Join join = root.join("newsItem", JoinType.INNER);
                list.add(criteriaBuilder.equal(root.get("appId"),appId));
                list.add(criteriaBuilder.equal(root.get("img"),true));
                list.add(criteriaBuilder.equal(join.get("appId"),appId));
                list.add(criteriaBuilder.equal(join.get("isOk"),2));
                list.add(criteriaBuilder.isNotNull(join.get("topImgId")));
                if(!StringUtils.isEmpty(searchId)){
                    list.add(criteriaBuilder.equal(join.get("searchId"),searchId));
                }
                if(!StringUtils.isEmpty(wk)){
                    list.add(criteriaBuilder.like(join.get("ItemTitle"),"%"+wk+"%"));
                }
                Predicate[] p = new Predicate[list.size()];
                query.where(criteriaBuilder.and(list.toArray(p)));
                return query.getRestriction();
            }
        };
    }

 

你可能感兴趣的:(Spring Jpa Specification Join查询)