当策略为IDENTITY时,Hibernate RX插入、刷新和刷新返回Exception



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

// 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())
)

class Author {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;

它将抛出CompletionException

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

有人能帮忙检查一下吗?

感谢

UPDATE:这是一个错误,将在Hibernate Reactive 1.0 CR1 中修复

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

您需要添加一个setter并将id类型更改为Long才能实现此操作。Author类变为:

@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。不知道你为什么需要它。

相关内容

  • 没有找到相关文章

最新更新