java – 在Spring Data Rest中不提取ManyToOne eager关联

前端之家收集整理的这篇文章主要介绍了java – 在Spring Data Rest中不提取ManyToOne eager关联前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我的Spring Data存储库被默认为默认的@RepositoryRestResource而没有任何自定义.

JPA实体:

@Entity
@Table(name = "flat")
public class Flat implements Serializable {

private static final long serialVersionUID = -7402659216552976109L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "flat_id")
private Integer id ;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "house_id",referencedColumnName="house_id",insertable=false,updatable=false)
private House house;

@Column(name = "kad_num")
private String kadNum;

…..

我希望House obkect在JSON中作为Flat对象的嵌入部分返回,但只获取到房子的URL

/资料库/单位/ 442991:

{
"kadNum" : "78:06:0002202:8981","sqrFull" : 52.7000,"flatNum" : "311","_links" : {
    "self" : {
       "href" : "http://localhost:8080/kap/repository/flats/442991"
     },"house" : {
       "href" : "http://localhost:8080/kap/repository/flats/442991/house"
     }
  }
}

同时,用户角色OneToMany关系被正确获取,角色名称为:

@Entity
@Table(name = "\"user\"")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private Integer id;

private String login;

private String email;

private String password;

@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "user_id",nullable=false,updatable = false,insertable = true)
private Set

请求:/ repository / users / 5

{
 "id" : 5,"login" : "op898","email" : "op20140603@gmail.com","password" : "c6172176f8f5d7e660eb4dcfad07a6ca","roles" : [ {
    "roleName" : "OPERATOR"
  } ],"_links" : {
  "self" : {
  "href" : "http://localhost:8080/kap/repository/users/5"
}
}
}

除了关系类型,我无法弄清楚差异.任何的想法 ?
谢谢

最佳答案
好吧,我找到了原因,但仍然不明白是否可以配置.

对于用户 – 角色关系,角色作为User的嵌入式JSON对象返回,因为没有为Role实体定义存储库.
但是House和Flat都有相应的JpaRepository,并且house作为Flat JSON对象中的唯一URL返回.似乎Spring Data REST总是在可以通过REST获取它时自动用JSON中的REST链接替换实体.一旦我从项目中删除了House存储库,Flat的JSON响应就转向了我想要的形式,嵌入式House:

{
  "house" : {
    "streetFull" : "KIMa pr.","houseNum" : "1","kadNum" : "78:06:0002202:3072","building" : null,"liter" : "В","sqrFull" : 10426.50,"sqrRooms" : 12868.10
  },"kadNum" : "78:06:0002202:9051","sqrFull" : 43.8000,"flatNum" : "421","_links" : {
    "self" : {
      "href" : "http://localhost:8080/kap/repository/flats/442987"
    },"flats" : {
      "href" : "http://localhost:8080/kap/repository/flats/442987/flats"
    }
  }
}

但这不是一个解决方案,因为我还需要House Data实体的Spring Data REST存储库.有任何想法吗 ?

原文链接:https://www.f2er.com/spring/431515.html

猜你在找的Spring相关文章