【问题标题】:Blaze Persistence EntityView inheritance mappingBlaze Persistence EntityView 继承映射
【发布时间】:2023-03-26 16:05:01
【问题描述】:

我目前正在将 Quarkus 与 Blaze Persistence 结合用于我的微服务。我有以下实体模型:

@Entity
public class Content extends BaseEntity {
    private boolean deleted;
    private boolean published;
}

@Entity
public class WebContent extends Content {
    private String webpage;
}

我已将实体映射到以下 EntityView:

@EntityView(Content.class)
@EntityViewInheritance
@CreatableEntityView
@UpdatableEntityView
public interface ContentUpdateView {
    @IdMapping
    Long getId();
    boolean isPublished();
    void setPublished(boolean published);
}

@EntityView(WebContent.class)
@CreatableEntityView
@UpdatableEntityView
public interface WebContentUpdateView extends ContentUpdateView {
    String getWebpage();
    void setWebpage(String webpage);
}

我的 ContentsResource 中有以下方法:

@POST
public ContentUpdateView save(ContentUpdateView content) {
    return contentsService.save(content);
}

当我调用 post 操作时,我只得到基本的 ContentUpdateView 而不是 WebContentUpdateView。有什么配置要做吗? (对于 Jackson,我在实体上使用 @JsonTypeInfo 和 @JsonSubType 注释来完成此操作。

谢谢 尤克斯

【问题讨论】:

  • 您好,目前尚不支持在 JAX-RS 端点中使用这种多态性。我创建了github.com/Blazebit/blaze-persistence/issues/1300 来跟踪它。您是否介意评论一下您对@JsonTypeInfo 的使用方式,以便我们更好地了解您希望如何使用它?

标签: json inheritance blaze-persistence


【解决方案1】:

我使用 Jackson 注释让它工作。这是基类:

@EntityView(Content.class)
@EntityViewInheritance
@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = DescriptiveContentView.class, name = "descriptive"),
        @JsonSubTypes.Type(value = MediaContentView.class, name = "media"),
        @JsonSubTypes.Type(value = WebContentView.class, name = "web")
})
@JsonTypeName("content")
public abstract class ContentView {
    @IdMapping
    public abstract Long getId();
    public abstract boolean isPublished();
}

这是一个子类:

@EntityView(DescriptiveContent.class)
@JsonTypeName("descriptive")
public abstract class DescriptiveContentView extends ContentView {
    public abstract Set<LocalizedParagraphView> getLocalizedParagraphs();
}

我将抽象类用于其他目的,但它也适用于接口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-07
    • 2011-11-22
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    相关资源
    最近更新 更多