当我尝试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中推送测试代码
有人能帮我检查一下吗?
谢谢
发布于 2021-01-07 20:19:08
更新:This is a bug将在Hibernate Reactive 1.0 CR1中修复
答案在评论中,但我会在这里重复一遍。
您需要添加一个设置器,并将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。不知道你为什么需要它。
https://stackoverflow.com/questions/62946269
复制相似问题