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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With