jpa建立視圖與實體的映射

其實和實際table 與實體建立映射方式一樣

視圖 user_view

比如:
我有以下視圖:user_view
在這裏插入圖片描述由於其中幾個數據是由其他表統計而來,爲了減小代碼編寫難度,採用視圖的方式

實體 UserInfoView

視圖對應的實體UserInfoView.java
注意:如果表名與實體不一致,在映射時需要指定name 屬性


@Table(name = "user_view")
@Entity

public class UserInfoView {
    @Id
    private Integer id;
    @Column
    private String account;
    @Column
    private String nikeName;
    @Column
    private String photo;
    @Column
    private String about;
    @Column
    private String signature;
    @Column
    private Integer likeCount;
    @Column
    private Integer articleCount;
    @Column
    private Integer visitCount;
    @Column
    private Integer fansCount;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getNikeName() {
        return nikeName;
    }

    public void setNikeName(String nikeName) {
        this.nikeName = nikeName;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public Integer getLikeCount() {
        return likeCount;
    }

    public void setLikeCount(Integer likeCount) {
        this.likeCount = likeCount;
    }

    public Integer getArticleCount() {
        return articleCount;
    }

    public void setArticleCount(Integer articleCount) {
        this.articleCount = articleCount;
    }

    public Integer getVisitCount() {
        return visitCount;
    }

    public void setVisitCount(Integer visitCount) {
        this.visitCount = visitCount;
    }

    public Integer getFansCount() {
        return fansCount;
    }

    public void setFansCount(Integer fansCount) {
        this.fansCount = fansCount;
    }
}

dao 定義:


public interface UserInfoViewDao extends CrudRepository<UserInfoView, Integer> {
    UserInfoView findByAccount(String account);

}

測試:

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class BlogApplicationTests {
    @Autowired
    UserInfoViewDao userInfoViewDao;

     @Test
    public void testViewDao(){
        log.info("view:{}",userInfoViewDao.findByAccount("zhangsan"));
     }
}

效果;

Hibernate: select userinfovi0_.id as id1_12_, userinfovi0_.about as about2_12_, userinfovi0_.account as account3_12_, userinfovi0_.article_count as article_4_12_, userinfovi0_.fans_count as fans_cou5_12_, userinfovi0_.like_count as like_cou6_12_, userinfovi0_.nike_name as nike_nam7_12_, userinfovi0_.photo as photo8_12_, userinfovi0_.signature as signatur9_12_, userinfovi0_.visit_count as visit_c10_12_ from user_view userinfovi0_ where userinfovi0_.account=?
2019-06-07 22:40:48.860  INFO 14128 --- [           main] com.blog.demo.BlogApplicationTests       : view:UserInfoView{id=14, account='zhangsan', nikeName='張三三', photo='太帥無法顯示', about='string', signature='隨便想一個簽名', likeCount=2, articleCount=4, visitCount=3, fansCount=4}

jpa 其實就是hibernate ,但是實現了java 代碼查詢單表,複雜查詢還是推薦使用原生sql
多表分頁即需原生sql。
之前我就寫過一篇博客記錄了在hibernate 中使用了視圖-實體映射,現在在jpa由於也是hibernate,那麼應該是可以的。

在實現的過程中遇到一個問題:
擅自更改了數據庫一個字段名,同時也將實體中的字段屬性也作出了相應更改,但是沒有改
get,set 方法,導致報錯:

javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister

錯誤來看是沒能創建sesstionfactory,不能找到實體的構造函數額,這個構造可能看的有點疑惑,默認都是有無參構造的,誰能想到是 getXXX方法與屬性名不一致導致的

    @Column
    public String getSingnature() {// 應該寫成getSignature
        return signature;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章