我从angular5发送一个GET请求到spring,以获取下面实体控件的所有元素。
这是一个实体:
@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中,console.log(数据)返回此结果作为示例:
{id: 1, dateDebut: 1526083200000, dateFin: 1526083200000} 因此,正如您所看到的,没有项目元素(我需要在前端)。
我想要显示角的控制元素,再加上一些有关项目的信息。
我应该发送两个GET请求从两个实体获得信息吗?还是有更好的方法?
结果我在mysql数据库中获得了

编辑2

发布于 2018-05-24 20:48:03
你应该可以使用JPA的主要功能之一..。查询推导!就像这样:
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);
}https://stackoverflow.com/questions/50516073
复制相似问题