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

后端 未结 2 1113
自闭症患者
自闭症患者 2021-01-15 12:48

I am unable to retrieve embedded .I am using Spring boot ,spring data rest and spring JPA. I have 3 tables in data base

  • user
  • competency
  • <
2条回答
  •  借酒劲吻你
    2021-01-15 13:24

    One way would be to use projections like for example:

    @Projection(name = "edit" , types = Employee.class)
    public interface EditEmployeeProjection {
        String getFirstName();
        String getLastName();
        Set 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 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 {
    
    After Implementing This ,In my case its not working to show desired jason   [![enter image description here][2]][2]
    

提交回复
热议问题