Unable to retrieve Spring HATEOAS embedded resource object in case of @ManytoMany relationship and lookup table with extra column

不打扰是莪最后的温柔 提交于 2019-12-01 10:31:11
KLHauser

One way would be to use projections like for example:

@Projection(name = "edit" , types = Employee.class)
public interface EditEmployeeProjection {
    String getFirstName();
    String getLastName();
    Set<Project> getProjects();
}

With this the project list will be embedded in the result for http://localhost:8080/api/employee/1?projection=edit

Projections would be used automatically if you add excerptProjection to you repository like described here: How to expose a complete tree structure with Spring Data REST and HATEOAS?

See for example here: https://shinesolutions.com/2015/04/15/spring-data-rest-and-projections/

EDITED

In you case a projection would look like:

@Projection(name = "edit" , types = UserCompetency.class)
public interface UserCompetencyProjection {
    User getUser();
    Competency getCompetency();
}

With http://localhost:8080/userCompetencies?projection=edit you would then see wanted result.

EDITED 2 The code I used:

Competency.class

@Entity
@Table(name = "competency", schema = "public")
@JsonIdentityInfo(
          generator = ObjectIdGenerators.IntSequenceGenerator.class,
          property = "competencyId")
public class Competency implements java.io.Serializable {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "competency_id", unique = true, nullable = false)
    private Long competencyId;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "competency")
    private List<UserCompetency> userCompetencies = new ArrayList<>();

UserCompetency.class

@Entity
@Table(name = "user_competency", schema = "public")
@JsonIdentityInfo(
          generator = ObjectIdGenerators.IntSequenceGenerator.class,
          property = "id")
public class UserCompetency implements java.io.Serializable {

    @EmbeddedId
    @AttributeOverrides({
        @AttributeOverride(name = "competencyId", column = @Column(name = "competency_id", nullable = false)),
        @AttributeOverride(name = "userId", column = @Column(name = "user_id", nullable = false)) })
    private UserCompetencyId id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
    private User user;

    @ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinColumn(name = "competency_id", nullable = false, insertable = false, updatable = false)
    private Competency competency;

UserCompetencyId.class

@Embeddable
public class UserCompetencyId implements java.io.Serializable {

    @Column(name = "competency_id", nullable = false)
    private Long competencyId;

    @Column(name = "user_id", nullable = false)
    private Long userId;

UserCompetencyRepository.class

@RepositoryRestResource(excerptProjection = UserCompetencyProjection.class)    
public interface UserCompetencyRepository extends JpaRepository<UserCompetency, UserCompetencyId> {

After Implementing This ,In my case its not working to show desired jason   [![enter image description here][2]][2]

It worked out ,actually i was missing annotation of
@RepositoryRestResource(excerptProjection = UserCompetencyProjection.class) on UserCompetencyRepository class now the output look like this I am skipping as it is output , and putting necessary output.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!