0

I have a very basic system using Spring with HATEOAS, and I found a problem. I have two very basic entities a car, and a person. Getters and setters avoided to make the question more readable.

@Entity
public class Car implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long carId;
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="personId")
    private Person owner;
    private String color;
    private String brand;
}

@Entity
public class Person implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long personId;
    private String firstName;
    private String lastName;
    @OneToMany(mappedBy="owner")
    private List<Car> cars;
}

This are my repositories:

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
    List<Person> findByLastName(@Param("name") String name);
}

@RepositoryRestResource(collectionResourceRel = "cars", path = "cars")
public interface CarRepository extends PagingAndSortingRepository<Car, Long> {
    List<Person> findByBrand(@Param("brand") String name);
}

I can create and query them, but some reference links are broken. For example, a couple of POSTs successfully creates two related entities:

http://localhost:8080/people
{  "firstName" : "Frodo",  "lastName" : "Baggins"}

http://localhost:8080/cars
{ "color":"black","brand":"volvo", "owner":"http://localhost:8080/people/1"}

This are the GET reply on them:

http://localhost:8080/cars/2

 {
    color: "black2",
    brand: "volvo2",
    _links: {
       self: {
           href: "http://localhost:8080/cars/2"
       },
       owner: {
           href: "http://localhost:8080/cars/2/owner"
       }
    }
 }

http://localhost:8080/people/1

{
   firstName: "Frodo",
   lastName: "Baggins",
   _links: {
      self: {
         href: "http://localhost:8080/people/1"
      },
      cars: {
         href: "http://localhost:8080/people/1/cars"
      }
   }
}

But I don't know why the owner has this URL on the car:

http://localhost:8080/cars/2/owner

which actually doesn't work.

Any help on that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Madhava Carrillo
  • 3,998
  • 3
  • 18
  • 24

1 Answers1

1

It's there because that's what HATEOAS is all about, representing entity/resource relationships with links.

I'm not sure why it doesn't work. I'd guess it probably doesn't work because the owner isn't eagerly fetched when retrieving a car resource.

You can customize which links are generated by following the steps at https://stackoverflow.com/a/24660635/442773

Community
  • 1
  • 1
Chris DaMour
  • 3,650
  • 28
  • 37
  • I'm not sure why the fetch has anything to do with this, but now it works! – Madhava Carrillo Aug 06 '14 at 08:06
  • 1
    Spring data rest creates controllers for you automatically for each Repository type. They respect / navigation and translate it to . navigation. The car controller has no idea a person controller or a person repository exists, it only knows about cars. That url routes to the car controller, it loads the car, then navs to the owner...which unless eagerly fetched is not there. – Chris DaMour Aug 06 '14 at 15:50