使用现有关系创建新模型

Create a new model with a existing relationship

所以我有一个名为 Issue 的模型,这个模型有一个名为 IssueType 的关系。
Issue 有一个 type_id,将它们链接在一起。
IssueType 有一个 id 和一个唯一的 name

问题是当我想创建一个问题时我想发送这样的请求

POST https://server/issues
Content-Type: application/json

{
  "description": "just some random description",
  "type": "Generic"
}

所以这里的 typeIssueTypename,在创建它时。

我该怎么做?

@Table(name = "issues")
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
public class Issue {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "description")
    private String description;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "type_id", referencedColumnName = "id")
    private IssueType type;
}
@Table(name = "issue_types")
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
public class IssueType {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column()
    private String name;
}
@RepositoryRestResource(
    collectionResourceRel = "issues",
    path = "issues",
    excerptProjection = IssueProjection.class
)
public interface IssueRepository extends JpaRepository<Issue, Long> {
}
@Projection( types = Issue.class)
public interface IssueProjection {
    Long getId();

    Double getLongitude();

    Double getLatitude();

    Integer getFloor();

    String getDescription();

    @Value("#{target.type.name}")
    String getType();
}

您可以创建一个单独的对象,例如 IssueDto,它将代表您的 JSON 请求,如下所示:

@Data
public class IssueDto {
   private String type;
   private String description;
}

然后将此对象转换为您的 IssueIssueType 对象。

我相信您在这里想要的是 Spring Data Rest 指的是定义查找类型:

Very often, domain models contain types that are value objects but actually represent a particular value out of a dedicated set of possible values. The Country class of the example above actually falls into that category. Because we need to manage the super set of values, there’s a repository in place. If it should be allowed to manage the set via REST as well, the repository needs to be exported, too. As repositories usually indicate an aggregate being managed, Spring Data REST’s default way of handling that scenario would be to render links to an association resource wherever a Country instance is encountered. The Hopper release train adds means to declare so called lookup types, for which Spring Data REST then renders an individual property inlined in the representation and also registers the according Jackson Deserializer to make sure that that property value gets translated back into an instance of that value type for PUT and POST requests.

https://spring.io/blog/2016/05/03/what-s-new-in-spring-data-hopper#lookup-types

https://docs.spring.io/spring-data/rest/docs/current/reference/html/#_customizing_item_resource_uris

所以像这样:

  @Override
  public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
    config.withEntityLookup()
      .forRepository(IssueType.class)
      .withIdMapping(IssueType::name)
      .withLookup(IssueTypeRepository::findByName); 
  }
}