Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not fetching ManyToOne eager association in Spring Data Rest

Tags:

java

spring

jpa

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" : "[email protected]",
 "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

like image 739
dkane Avatar asked Sep 30 '22 15:09

dkane


1 Answers

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.

like image 77
dkane Avatar answered Nov 15 '22 11:11

dkane