JPA中這兩行有什么區別
public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findByUser_Id(Long id);
}
和
public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findByUserId(Long id);
}
所以我編輯了我的問題,使其更清楚并得到了很好的回應,所以有我的域類 User 和 Post,我在 Post 類中有關系@ManyToOne。
領域
...
public class Post implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
和
...
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
uj5u.com熱心網友回復:
如果您使用的是 Spring Data JPA,那么 class 是一個Spring JPA 存盤庫,其中的實作由 Spring Framework 完成,并且方法名稱用于構造查詢。
檢查2.3.2 查詢創建以了解如何構造查詢。
因此,在您的情況下,List<Post> findByUser_Id(Long id);方法可能會在運行時拋出錯誤,因為它不是有效的方法格式。
uj5u.com熱心網友回復:
它只是方法的名稱。除此之外,這些線之間沒有區別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/363958.html
下一篇:如何進行良好的JPA查詢
