springDataJpa多表級聯查詢(@ManyToOne @OneToOne)

c&0xff00發表於2018-12-15

背景:

主要是記錄自己遇到的坑,最後是通過註解解決的@ManyToOne @OneToOne,其實這個問題碰到過,今天又踩了

主要問題:

兩個類要實現級聯查詢,比如Iteminformation 、Timesetting,類裡面欄位先不管,第一反應是建個VO類(主要是看到公司了有類似的建DTO類的程式碼,那是對一個有太多欄位的類而只查詢部分欄位的臨時類)

    @Query("select distinct new cn.com.dto.SpxxDTO(t.dzsphm, t.kprq, t.fkjnfsDm, t.fkssswjgDm, t.zffsDm) from SpxxCMP t where t.spztDm in (:spztList)")
    List<SpxxDTO> findSpztDmInAndZffsDmIn(@Param("spztList") List<String> spztList);

所以就這樣寫了,如下

public class ItemInformationVO {
	private Iteminformation iteminformation;
	private Timesetting timesetting;

	public ItemInformationVO() {
		super();
	}

	public ItemInformationVO(Iteminformation iteminformation, Timesetting timesetting) {
		this.iteminformation = iteminformation;
		this.timesetting = timesetting;
	}

    //getter setter
}

在資料庫介面類中新增介面如下:

@Repository
public interface ItemInfomationRepository extends JpaRepository<Iteminformation, Integer>,JpaSpecificationExecutor<Iteminformation>,PagingAndSortingRepository<Iteminformation, Integer>{

	@Query(value = "select new com.sssp.utils.DTO.ItemInformationVO(i,t) FROM Iteminformation i,Timesetting t where i.itemid = t.itemid")
	public List<ItemInformationVO> findAllItemInformation1();

}

看似挺好的呀,將查出來的物件建個物件,級聯查詢成功 ,資料也能獲取。

       此時,有一個問題,這個查詢介面怎麼做分頁查詢,怎麼做條件查詢呢?一下子想不出來了,感覺要自己寫jpa了(會的大佬給我提個醒,感謝)

         然後才發現我們這樣建表不是有外來鍵銜接的嗎?

        

@Cacheable
@Table(name = "iteminformation")
@Entity
public class Iteminformation {
	@Id
	@Column(name = "itemid")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer itemid;
	
	@Column(name = "itemname")
	private String itemname;
	
	@Column(name = "grade")
	private String grade;
	
	@Column(name = "itemhead")
	private String itemhead;
	
	@Column(name = "phone")
	private String phone;
	
	@Column(name = "email")
	private String email;
	
	@Column(name = "professional")
	private String professional;
	
	@Column(name = "teammembers")
	private String teammembers;

	@OneToOne
	@JoinColumn(name="timesetting",referencedColumnName="timesettingId")
	private Timesetting timesetting;
//以下省略
}

 @OneToOne

        這裡的@OneToOne指的是兩個類物件是一對一的關係,比如一夫一妻制,當然也有@ManyToMany,@ManyToOne,比如人可以有很多本書,一本書也可以被很多人借一樣

 @JoinColumn(name="timesetting",referencedColumnName="timesettingId")

       @JoinColumn註解指定表關係

       name指定了外來鍵的名稱,referencedColumnName指定被連結的表的外來鍵名稱,這樣就關聯了起來。

@Cacheable
@Table(name = "timesetting")
@Entity
public class Timesetting {
	@Id
	@Column(name = "timesettingId")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer timesettingId;
	
	@JsonFormat(pattern = "yyyy-MM-dd")
	@Column(name = "starttime")
	private Date starttime;
	
	@JsonFormat(pattern = "yyyy-MM-dd")
	@Column(name = "endtime")
	private Date endtime;
	
	@JsonFormat(pattern = "yyyy-MM-dd")
	@Column(name = "midtime")
	private Date midtime;
	
	@JsonFormat(pattern = "yyyy-MM-dd")
	@Column(name = "finaltime")
	private Date finaltime;

//以下省略
}

這樣就能查出來了

 有什麼問題歡迎大家一起討論,對你有所幫助也請您點個贊哦。

JPA使用記錄:

@Query(value = "select new cn.com.dto.xnhplsb.response.XnhResponseDTO(count(t.id),SUM(t.yjfe), SUM(CASE WHEN t.zsxmDm = '10201' or t.zsxmDm = '10210' then t.yjfe ELSE 0 END)," +
      "SUM(CASE WHEN t.zsxmDm = '10203' or t.zsxmDm = '10207' or t.zsxmDm = '10212' then t.yjfe ELSE 0 END))" +
      "from WtdsXnhTzmxCMP t where t.tzjlId =:tzjlId and t.jyjg=:jyjg")
XnhResponseDTO findZjeByTzjlIdAndJyjg(@Param("tzjlId") String tzjlId, @Param("jyjg") String jyjg);

XnhResponseDTO中只有配置相應的建構函式既可以了

    public XnhResponseDTO(double zje, double yangLJhej, double yiLJehj) { //臺賬明細總金額計算用
        this.yiLJehj = new BigDecimal(yiLJehj);
        this.yangLJhej = new BigDecimal(yangLJhej);
        this.zje = new BigDecimal(zje);
    }

這樣就可以實現多表查詢

相關文章