当前位置: 首页 > 知识库问答 >
问题:

Spring data rest和jpa@OneTo很多重复"_links"

傅皓君
2023-03-14

我对Spring数据Rest与Spring数据JPA一起使用有一个问题。我使用的是Spring boot 1.4.4。释放

我的rest spring数据存储库在这里:

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

这里没有显示我的setters和bregetter。

轮廓爪哇:

@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;

    ...
}

属性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“要创建实体:

{
    "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,这些“_链接”没有“复制”。

有人能解释一下吗?我对我的JPA实体有什么误解吗?我需要在rest存储库中配置什么吗?

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

谢谢

共有1个答案

张敏达
2023-03-14

为了解决这个问题,我一直在使用投影(更多信息如下:http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-摘录)。我发帖时没有注意到这个功能。

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

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

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

在属性类上,我必须添加@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();
}

然后在调用restendpoint时需要应用投影:

http://localhost:8880/profiles?projection=inlineAttributes
 类似资料:
  • 在Play2.0.2中使用Ebean我有两个这样的模型: 我的模型看起来像: 我想加入上述两个模型,以便在我的控制器中: 但这给我下面的例外列没有找到: 为什么我的列被转换为而不是

  • 我目前在数据库中的一对一/多对一映射时遇到问题。我希望我的图书表具有与“出版商”、“流派”、“评级”和“状态”表的@ManyToOne映射。 这是我的Book实体类的外观: 例如,我的Publisher实体类(类型、评级和状态类完全相同): 唯一的区别是类型、评级和状态的书籍字段,f. e: 这就是我的堆栈跟踪的样子: 更新的堆栈跟踪:

  • 我正在用jpa处理spring boot,我尝试使用onetomany,由于某种原因,当我尝试在postman上发送请求时,它创建了我的父对象,但没有使用子外键,因此我得到了null。 还有一件事:有一次我玩了我的代码,不知怎么的,我让它工作了,但它只成功了一次,现在它不再工作了。我试图在网上学习很多教程,但没有一个帮助我解决这个问题。

  • 我在Hibernate状态下使用关系时得到空。这是我的代码用户实体 登录历史实体 要获取登录历史详细信息,请执行以下操作: 我得到一张空名单。请帮忙

  • 我知道这是Telegram的一个副本,它向webhook发送重复的POST JSON请求,并向webhoook发送重复的POST JSON请求。然而,这个问题没有任何充分的答案,因此: 我有一个PHP应用程序处理来自Telegram的webhook请求。然而,Telegram并没有看到webhook成功运行(尽管hurl.it清楚地显示,它在这样一个请求上发送了200条消息)。 因此,我让我的机器

  • 下面是我的spring-data-rest存储库: 下面是我的实体(为简洁起见,没有显示getter和setter)。 profile.java: 有关依赖关系版本的更多信息可以在这里找到。如果需要,我没有重写这些(http://docs.spring.io/spring-boot/docs/1.4.4.release/reference/html/appendience-dependency-v