spring-data-jpa中findOne與getOne的區別

項目中用到了spring-data-jpa,今天在寫一個update方法的時候報了個空指針,看了看是由於一個對象中的關聯沒有取出來,我用的是getOne取得這個對象,加斷點看以一下這個對象是個hibernate的代理對象,而不是實體,裏面基本沒有值。
182002_3Nz7_323.pngspring

而後我又換成findOne試了一下,斷點查看這個取出來的對象,這個對象是實體,關聯也有。
180930_ef2P_323.pngide

findOne與getOne的註釋中也有些幫助spa

/**
     * Retrieves an entity by its id.
     * 
     * @param id must not be {@literal null}.
     * @return the entity with the given id or {@literal null} if none found
     * @throws IllegalArgumentException if {@code id} is {@literal null}
     */
    T findOne(ID id);

/**
     * Returns a reference to the entity with the given identifier.
     * 
     * @param id must not be {@literal null}.
     * @return a reference to the entity with the given identifier.
     * @see EntityManager#getReference(Class, Object)
     */
    T getOne(ID id);

注意getOne是「Returns a reference「hibernate

相關文章
相關標籤/搜索