3

I have a Spring Data REST application using JPA as the underlying implementation. I have a Data model similar to the following:

@Entity
public class Person {

  @Id
  private Long id;
  private String name;

  @ManyToOne
  private Address address;

}

@Entity
public class Address {

  @Id
  private Integer id;
  private String street;
  private String city;
  private String state;
  private String zip;

  @OneToMany(mappedBy = 'address')
  private List<Person> people;
}

I've left out the getter/setters, but they are there. I then have two CrudRepository interfaces that defines access to the two entities. On the PersonRepository, I've defined a method findByNameAndAddress. The intent is to search for people with the given name and that have a linked address. The problem is that I think I'm supposed to pass in the Id of the address in the findByNameAndAddress query. But since I'm doing all of this via the REST client, I never have the id. When I retrieve the Address entities from the AddressRepository, SDR removes the Id field from the JSON respresentation. According to what I've read, the idea is to only use the self link when referring to the entity.

Now, obviously, I could determine what the ID is by parsing the self link and using that in the query, but that seems to be violating the whole HAL principal. Does anyone have any suggestions on how one is supposed to do something like this?

1 Answer 1

1

Ok, I figured out how to enable this, using a custom RepositoryRestMvcConfiguration class to override the config() method and use the exposeIdsFor() method to add in my domain classes.

NOTE: I was using Spring Boot, and when I configured my own configuration class, some of the auto-configured bits that Spring Boot was doing, including the Jackson Setup, broke. Figured out I had to extend org.springframework.boot.autoconfigure.data.rest.SpringBootRepositoryRestMvcConfiguration instead of RepositoryRestMvcConfiguration from Spring Data Rest.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.