簡體   English   中英

Spring Boot JPA 實體

[英]Spring boot JPA entity

我有一個名為 Order 的實體,它引用了一個名為 project 的實體,如下所示:

@Entity
@Table(name = "customer_order")
public class Order {

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

    @ManyToOne
    @JoinColumn(name = "project_id", nullable = false)
    private Project project;

    @Column(name = "user_id")
    private String userId;

    @Column(name = "created_at")
    @CreationTimestamp
    private Date createdAt;
}

我的存儲庫如下:

@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
}

當我調用我的 rest get 端點以獲取所有訂單的列表時,在響應中,我在主訂單對象中獲得了一個項目對象,其中包含“項目”類的所有屬性。 我不要這個。 我需要一個只包含它引用的項目 ID 的精益訂單響應對象。 我嘗試在 Order 類中的“project”屬性上使用以下注釋,但它完全擺脫了項目詳細信息。

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)

我仍然需要關聯的項目的 id。 我該怎么做?

我假設您的端點返回 JSON。

在這種情況下,您將不得不編寫自己的序列化程序。

  • 對於 Gson,這可以通過實現 JsonSerializer 並將其注冊為類型適配器來實現
  • 對於 ObjectMapper (Jackson),您必須擴展 StdSerializer 並將其作為序列化程序添加到 ObjectMapper 或在模型類上使用注解 @JsonSerialize(using = ItemSerializer.class)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM