1.前景
JPA作為一種持久層框架的指導思想窘游,對于數(shù)據(jù)庫實體的復合主鍵唠椭,提到有兩種方式可以實現(xiàn):
1>@IdClass標簽的使用; 2> @EmbededId標簽的使用忍饰。
具體細節(jié)贪嫂,可參照官方文檔介紹:JPA Composite Primary Key---JPA復合主鍵
2.項目中使用
項目中有一個MerchantsProduct表,有兩個字段:mid 和 productcode艾蓝。這個兩個字段一起力崇,構(gòu)成一個復合主鍵。
同時這個實體結(jié)合SpringDataJPA的findAll分頁使用赢织,負責前端展示亮靴。
最終決定選擇使用@IdClass這種方式,更符合常規(guī)的方法于置。
3.問題
If you try to use findAll(Pageable)on a PagingAndSortingRepository you get an "java.lang.IllegalStateException: No supertype found" if the entity key is defined using @IdClass and you're using Hibernate 4.1.9
具體解決方式:使用maven中排除依賴傳遞 pom.xml的<exclusions></exclusions>,將hibernate-core茧吊、hibernate-entitymanagement默認的4.1.8.Final版本,升高為4.2.9.Final版本,才得以解決java.lang.IllegalStateException: No supertype found異常搓侄。
參考文章:
1.Spring Data Jpa 復合主鍵 @IdClass使用
2.JPA Composite Primary Key---JPA復合主鍵
3.SpringDataJPA: Can't use pagination (Pageable) with @IdClass entities (spring-data-jpa 1.4.3 & Hibernate 4.1.9)
4.maven中排除依賴傳遞 pom.xml的exclusions