小编Yuk*_*ida的帖子

JPA EntityGraph,使用静态 MetaModel 以可编程方式创建 PluralAttribute 的子图

当我有以下实体时。

@Entity
class Article {
  @OneToMany
  Set<Comment> comments;
}

@Entity
class Comment {
  @ManyToOne
  User author;
}
Run Code Online (Sandbox Code Playgroud)

并使用 EntityGraph 和静态元模型创建一些视图规范类,如下所示。

class ArticleViewSpec {

  /**
   * fetch comments and each of comment's author.
   */
  public static final Function<EntityManager, EntityGraph<Article>> DETAIL = em -> {
    EntityGraph<Article> graph = em.createEntityGraph(Article.class);

    Subgraph<Comment> sgComment = graph.addSubgraph(Article_.comments);

    sgComment.addAttributeNodes(Comment_.author);

    return graph;
  };
}
Run Code Online (Sandbox Code Playgroud)

但是上面的类不能被编译,因为预期的类型不是

Subgraph<Comment> sgComment = graph.addSubgraph(Article_.comments);
Run Code Online (Sandbox Code Playgroud)

Subgraph<Set<Comment>> sgComment = graph.addSubgraph(Article_.comments);
Run Code Online (Sandbox Code Playgroud)

当我们有扩展的属性时会出现这个问题javax.persistence.metamodel.PluralAttribute。(例如 SetAttribute、ListAttribute)

这种行为显然来自 api 规范。 javax.persistence.EntityGraph#addSubgraph(javax.persistence.metamodel.Attribute<T,X>)

但是,在这些情况下,如何使用 JPA 静态 MetaModel 以可编程方式和类型安全地创建 EntityGraph …

hibernate jpa entitygraph

5
推荐指数
1
解决办法
948
查看次数

RxJava:flatMap中的if/else操作

我尝试获取并呈现如下的一些数据

原始数据类

  @Data @AllArgsConstructor class Category {
    String name;
    List<String> items;
  }
Run Code Online (Sandbox Code Playgroud)

演讲课

  @Data @AllArgsConstructor class ViewModel {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_ITEM = 1;
    int type;
    String category;
    String itemName;
  }
Run Code Online (Sandbox Code Playgroud)

以下代码是请求并将订阅数据转换为表示对象.

  Observable.create(new Observable.OnSubscribe<Category>() {
      @Override public void call(Subscriber<? super Category> subscriber) {
        subscriber.onNext(new Category("1", Lists.newArrayList("", "a", "b")));
        subscriber.onNext(new Category("2", Lists.newArrayList("")));// this data does not output
        subscriber.onNext(new Category("3", Lists.newArrayList("c", "", "d")));
        subscriber.onNext(new Category("4", Lists.newArrayList("e", "f", "")));
      }
    }).flatMap(new Func1<Category, Observable<ViewModel>>() {
      @Override …
Run Code Online (Sandbox Code Playgroud)

java rx-java

3
推荐指数
1
解决办法
6835
查看次数

标签 统计

entitygraph ×1

hibernate ×1

java ×1

jpa ×1

rx-java ×1