2

I t tried use new lastes version of Spring Hateoas 1.0 in my Spring Project, in this project all entities classes inherit from an Abstract Class, but in the Hateoas documentation my Entities must be extend the RepresentationModel Class https://docs.spring.io/spring-hateoas/docs/1.0.1.RELEASE/reference/html/#fundamentals.representation-models I've problems to extend the RepresentationModel on my Entity parent Class , somebody can help me please.

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

import org.springframework.hateoas.RepresentationModel;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@MappedSuperclass
@Getter @Setter @NoArgsConstructor
public abstract class Entity<T> extends RepresentationModel<T> implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private T id;   

    @Column
    private Date createAt;

    @Column
    private Date updateAt;

} 
Jericob
  • 1,947
  • 2
  • 14
  • 16

1 Answers1

0

I didn't use generic classes, but instead cast the whole object back to my concrete class in my controller.

Here is my super class

public class AbstractEntity extends RepresentationModel<AbstractEntity> implements Serializable {
}

Now my class extends the AbstractEntity:

public class User extends AbstractEntity {}

And in the controller we can have a method for adding the selfRef:

private User addSelfLink(User user) {
        return (User) user.add(linkTo(methodOn(UserController.class).getById(user.getId())).withSelfRel());
    }

All we have to do is cast the object bask to the final object we are using

double-beep
  • 5,031
  • 17
  • 33
  • 41
James Wagstaff
  • 150
  • 1
  • 7