Spring数据休息和jpa @OneToMany重复“_links”

时间:2017-02-21 14:28:52

标签: spring-boot spring-data-jpa spring-data-rest

我遇到Spring Data Rest与Spring Data JPA一起使用的问题。我正在使用Spring-boot 1.4.4.RELEASE。

这是我的spring-data-rest存储库:

public interface ProfileJpaRepository extends JpaRepository<Profile, Long> {
}

以下是我的实体(为简洁而未显示的getter和setter)。

Profile.java:

@Entity
@Table(name = "PROFILE")
public class Profile {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String description;

    // Don't use mappedBy="profile" because attributes are not persisted properly
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "PROFILE_ID")
    private Set<Attribute> attributes;

    ...
}

Attribute.java

@Entity
@Table(name = "ATTRIBUTE")
public class Attribute {

     @Id
     @GeneratedValue(strategy=GenerationType.AUTO)
     @Column(name = "ID")
     private Long id;

     private String uri;

     @ManyToOne(fetch = FetchType.EAGER)
     private Profile profile;

     @ElementCollection(fetch = FetchType.EAGER)
     @CollectionTable(name="ATTRIBUTE_DATAS")
     private List<String> datas = new ArrayList<>();

     public Attribute() {}

     public Attribute(String uri, List<String> datas) {
        this.uri = uri;
        this.datas = datas;
    }

    ...
}

http://localhost:8880/profiles”上的POST以创建实体:

{
    "description" : "description-value",
    "attributes" : [
        {
            "uri" : "uri-a",
            "datas" : ["uri-a-value"]
        },
        {
            "uri" : "uri-b",
            "datas" : ["uri-b-value"]
        }
    ]
}

这是我点击http://localhost:8880/profiles时的结果:

{
  "_embedded" : {
    "profiles" : [ {
      "description" : "description-value",
      "attributes" : [ {
        "uri" : "uri-a",
        "datas" : [ "uri-a-value" ],
        "_links" : {
          "profile" : {
            "href" : "http://localhost:8880/profiles/1"
          }
        }
      }, {
        "uri" : "uri-b",
        "datas" : [ "uri-b-value" ],
        "_links" : {
          "profile" : {
            "href" : "http://localhost:8880/profiles/1"
          }
        }
      } ],
      "_links" : {
        "self" : {
          "href" : "http://localhost:8880/profiles/1"
        },
        "profile" : {
          "href" : "http://localhost:8880/profiles/1"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8880/profiles"
    },
    "profile" : {
      "href" : "http://localhost:8880/profile/profiles"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

我认为存在问题,因为在每个属性下都指定了"_links"。相反,我会期待这样的事情:

{
  "_embedded" : {
    "profiles" : [ {
      "description" : "description-value",
      "attributes" : [ {
        "uri" : "uri-a",
        "datas" : [ "uri-a-value" ]
      }, {
        "uri" : "uri-b",
        "datas" : [ "uri-b-value" ]
      } ],
      "_links" : {
        "self" : {
          "href" : "http://localhost:8880/profiles/1"
        },
        "profile" : {
          "href" : "http://localhost:8880/profiles/1"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8880/profiles"
    },
    "profile" : {
      "href" : "http://localhost:8880/profile/profiles"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

请注意,我一直在从MongoRepository切换到JpaRepository,并且使用MongoRepository,这些"_links"并未“重复”。

有人可以对此有所了解吗?我在JPA实体上配置错误了吗?我是否需要在其余存储库中配置某些内容?

如果您需要,可以在此处找到有关依赖项版本的更多信息,我没有覆盖这些(http://docs.spring.io/spring-boot/docs/1.4.4.RELEASE/reference/html/appendix-dependency-versions.html

感谢。

1 个答案:

答案 0 :(得分:0)

要解决我一直在使用Projections的问题(更多信息请参见:http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts)。发布时我不知道该功能。

我必须注释我的存储库并告诉它使用我的InlineAttributes投影:

import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(excerptProjection = InlineAttributes.class)
public interface ProfileJpaRepository extends JpaRepository<Profile, Long> {
}

在Attribute类中,我必须添加@JsonIgnore注释:

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name = "ATTRIBUTE")
public class Attribute {
     ...

     @ManyToOne(fetch = FetchType.EAGER)
     @JsonIgnore
     private Profile profile;

     ...
}

我必须创建的InlineAttributes投影类。我指定了顺序,因为它与以前不一样:

import org.springframework.data.rest.core.config.Projection;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;    

@Projection(name = "inlineAttributes", types = { Profile.class })
@JsonPropertyOrder({ "description", "attributes" })
public interface InlineAttributes {

    public String getDescription();
    public Set<Attribute> getAttributes();
}

然后,当您调用其余端点时,需要应用投影:

http://localhost:8880/profiles?projection=inlineAttributes