Spring Data JPA 多個實體類表聯合視圖查詢

Spring Data JPA 查詢數據庫時,若是兩個表有關聯,那麼就設個外鍵,在查詢的時候用Specification建立Join 查詢即可。可是隻支持左鏈接,不支持右鏈接,雖然說左右鏈接反過來就能實現同樣的效果,可是這就關係到誰是誰的外鍵的問題。外鍵搞起來有時候確實麻煩。因此爲了查詢個視圖,沒有找到更好的辦法,只好在service層查兩次合併起來了。數據庫

    @Entity
    @Table(name="tb_user")
    public class UserInfo implements Serializable{
     
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Long userId;
        private String userName;
        private String password;
        private String name;
        private int age;
        private String sex;
        private String email;
        private Date dateOfBirth;
        private String telNumber;
        private String education;
        private String school;
    //    @ManyToOne
    //    @JoinColumn(name="addressId")
    //    private Address address;
        private Long addressId;
    // getter and setter
    }
    @Entity
    @Table(name="tb_address")
    public class Address implements Serializable{
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long addressId;   private Long userId 
        private String areaCode;
        private String country;
        private String province;
        private String city;
        private String area;
        private String detailAddress;
    // getter and setter
    }

建立一個類包含UserInfo和Address中的全部屬性:this

    public class ViewInfo implements Serializable{
     
        private UserInfo userInfo;
        private Address address;
        public ViewInfo(){
     
        }
        public ViewInfo(UserInfo userInfo){
            Address address = new Address();
            this.userInfo = userInfo;
            this.address = address;
        }
        public ViewInfo(Address address){
            UserInfo userInfo = new UserInfo();
            this.userInfo = userInfo;
            this.address = address;
        }
        public ViewInfo(UserInfo userInfo,Address address){
            this.userInfo = userInfo;
            this.address = address;
        }
    // getter and setter
    }

接下來就是在DAO層中寫自定義查詢語句了:spa

    public interface UserInfoRepository extends CrudRepository<UserInfo, Long>{
        @Query(value="SELECT new com.demo.test.Entity.ViewInfo(u,a)FROM "
        + " UserInfo u, com.demo.test.Entity.Address a WHERE u.addressId = a.id) ")
        List<ViewInfo> findViewInfo();
        @Query("SELECT new com.demo.test.Entity.ViewInfo"
        + "(u) FROM UserInfo u WHERE u.addressId IS NULL OR u.addressId NOT IN (SELECT a.id FROM Address a)")
        List<ViewInfo> findViewInfoLeft();
        @Query("SELECT new com.demo.test.Entity.ViewInfo"
        + "(a) FROM Address a WHERE a.id NOT IN (SELECT u.addressId FROM UserInfo u WHERE u.addressId IS NOT NULL)")
        List<ViewInfo> findViewInfoRight();
     
    }

而後在service層中查詢各個部分:code

        public void summary(){
          System.out.println("=======middle part=======");
          List<ViewInfo> userInfos = userInfoRepository.findViewInfo();
          for(ViewInfo item : userInfos){
              System.out.println(item.getUserInfo().getUserName()+" "+item.getAddress().getCity());
          }
          System.out.println("=======left part=======");
          List<ViewInfo> userInfoLeft = userInfoRepository.findViewInfoLeft();
          for(ViewInfo item : userInfoLeft){
              System.out.println(item.getUserInfo().getUserName()+" "+item.getAddress().getCity());
          }
          System.out.println("=======right part=======");
          List<ViewInfo> userInfoRight = addressRepository.findViewInfoRight();
          for(ViewInfo item : userInfoRight){
              System.out.println(item.getUserInfo().getUserName()+" "+item.getAddress().getCity());
          }
     
        }

數據庫的Inner Join選交集,Outer Join 選並集,Left Join 選左表與右表的差集加上交集,Right Join選右表與左表的差集加上交集。暫且如此了。若是哪位看到個人文章有更好的方法請不吝賜教。blog

相關文章
相關標籤/搜索