首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当策略为标识时,Hibernate RX插入、刷新和刷新返回异常

当策略为标识时,Hibernate RX插入、刷新和刷新返回异常
EN

Stack Overflow用户
提问于 2020-07-17 10:39:39
回答 1查看 186关注 0票数 2

当我尝试hibernate rx库并运行示例时

代码语言:javascript
复制
        // obtain a reactive session
        factory.withTransaction(
                // persist the Authors with their Books in a transaction
                (session, tx) -> session.persist(author1, author2)
                        .flatMap(Mutiny.Session::flush)
                        .flatMap(s -> s.refresh())
        )

代码语言:javascript
复制
class Author {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    Integer id;

它将抛出CompletionException

代码语言:javascript
复制
Exception in thread "main" java.util.concurrent.CompletionException: org.hibernate.PropertyAccessException: Could not set field value [1] value by reflection : [class org.hibernate.example.reactive.Author.id] setter of org.hibernate.example.reactive.Author.id

我在https://github.com/semistone/hibernate-reactive/commit/398b1570666ed81a7d257020166f2ae59f1c5eb8中推送测试代码

有人能帮我检查一下吗?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-01-07 20:19:08

更新:This is a bug将在Hibernate Reactive 1.0 CR1中修复

答案在评论中,但我会在这里重复一遍。

您需要添加一个设置器,并将id类型更改为Long才能使其正常工作。Author类变为:

代码语言:javascript
复制
@Entity
@Table(name="authors")
class Author {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull @Size(max=100)
    private String name;

    @OneToMany(mappedBy = "author", cascade = PERSIST)
    private List<Book> books = new ArrayList<>();

    Author(String name) {
        this.name = name;
    }

    Author() {}

    void setId(Long id) {
        this.id = id;
    }

    Long getId() {
        return id;
    }

    String getName() {
        return name;
    }

    List<Book> getBooks() {
        return books;
    }
}

而且,您不需要添加刷新操作(.flatMap(Mutiny.Session::flush)),因为withTransaction已经为您完成了该操作。

而且你也不需要s.refresh。不知道你为什么需要它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62946269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档