Don't set up a lot of interesting associations in Spring Data Rest

My Spring Data Warehouses are stored as the default @RepositoryRestResource without any configuration.

JPA Essence:

@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 the obkect object to be returned in JSON as an inline part of the Flat object, but only get the URL of the house

/ repository / apartments / 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, OneToMany User-Role relationships are selected with the 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 not understand the difference, except for the type of relationship. Any ideas? Thanks you

+4
source share
2 answers

Well, I found the reason, but still do not understand if it can be configured.

User-Role JSON , Role . House and Flat JpaRepository, URL- Flat JSON. , Spring Data REST REST- ​​JSON , REST. , JSON Flat , , 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"
    }
  }
}

, Spring Data REST House. ?

+2

. org.springframework.data.rest.core.config.Projection HAL Flat.House.

+2

Source: https://habr.com/ru/post/1543289/


All Articles