简体   繁体   中英

Not fetching ManyToOne eager association in Spring Data Rest

My Spring Data repositories are confiured as default @RepositoryRestResource without any customization.

JPA entity:

@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;

.....

I want House obkect to be returned in JSON as embedded part of Flat object, but get only URL to the house

/repository/flats/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"
     }
  }
}

At the same time, User-Role OneToMany relationship is fetched fine, with role name:

@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<Role> roles = new HashSet<Role>();
  .....

request: /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"
}
}
}

I can't figure out the difference, except of relationship type. Any idea ? Thank you

Well, I have found the cause but still don't understand is it possible to configure.

For User-Role relationship, Role is returned as embedded JSON object of User, because there is no repository defined for Role entity. But House and Flat both has corresponding JpaRepository, and house is returned as only URL in Flat JSON object. It seems that Spring Data REST automatically substitutes entity with REST link in JSON always when it is possible to get it with REST. Once I removed House repository from project, JSON response for Flat turned to the form I wanted, with embedded 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"
    }
  }
}

But this is not a solution, because I need Spring Data REST repository also for House entity. Any ideas ?

Answering to myself again. Problem is finally solved by using org.springframework.data.rest.core.config.Projection I've left classic HAL as a default, and created custom projection with Flat.House property exposed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM