代码之家  ›  专栏  ›  技术社区  ›  dEs12ZER

如何在spring boot中获取实体的所有元素?

  •  0
  • dEs12ZER  · 技术社区  · 8 年前

    我正在从angular5向spring boot发送一个GET请求,以获取下面实体Contrat的所有元素。

    这是实体:

    @Entity
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Contrat implements Serializable{
    
            @Id @GeneratedValue
            private Long id;
            private Date dateDebut ;
            private Date dateFin ;
            private boolean valid; 
            @ManyToOne
            @JoinColumn(name = "Id_Project")
            @JsonBackReference(value="projet-contrat")
            private Project project;
    
            @ManyToOne
            @JoinColumn(name = "Id_AppUser")
            @JsonBackReference(value="appuser-contrat")
            private AppUser appUser;
    }
    

    这是存储库:

      public interface ContratRepo extends JpaRepository<Contrat,Long> {
    
        @Query("select from Contrat c where c.appUser = :userApp")
        public Page<Contrat> chercherContrat(@Param("userApp") AppUser userApp  
       , Pageable pageable);
    
       }
    

    在Angular5控制台。日志(数据)返回此结果,例如:

     {id: 1, dateDebut: 1526083200000, dateFin: 1526083200000} 
    

    正如你所看到的,这里没有项目元素(我需要在前端)。

    我想展示一些对比元素,以及一些与之相关的项目信息。

    我应该发送两个GET请求以从两个实体获取信息吗? 还是有更好的方法?

    结果我进入了mysql数据库

    enter image description here

    编辑2

    enter image description here

    1 回复  |  直到 8 年前
        1
  •  0
  •   Dovmo    8 年前

    你应该能够使用JPA的主要功能之一。。。 query derivation ! 就像这样:

    public interface ContratRepo extends JpaRepository<Contrat,Long> {
    
        public Page<Contrat> findByAppUser(AppUser userApp, Pageable pageable);
    
    }
    

    如果你想要 Project 使用 @EntityGraph :

    public interface ContratRepo extends JpaRepository<Contrat,Long> {
    
        @EntityGraph(attributePaths = { "project"}, type = EntityGraphType.LOAD)
        public Page<Contrat> findByAppUser(AppUser userApp, Pageable pageable);
    
    }