Hibernate 当策略为IDENTITY时,休眠RX插入、刷新和刷新返回异常
当我尝试hibernate rx库时 并运行示例Hibernate 当策略为IDENTITY时,休眠RX插入、刷新和刷新返回异常,hibernate,reactive-programming,hibernate-reactive,Hibernate,Reactive Programming,Hibernate Reactive,当我尝试hibernate rx库时 并运行示例 // obtain a reactive session factory.withTransaction( // persist the Authors with their Books in a transaction (session, tx) -> session.persist(author1, author2)
// 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())
)
及
它将抛出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
我把测试代码推进去
有人能帮我查一下吗
谢谢更新:它将在Hibernate Responsive 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;
}
}
@实体
@表(name=“authors”)
类作者{
@Id@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@大小为NotNull(最大值=100)
私有字符串名称;
@OneToMany(mappedBy=“author”,cascade=PERSIST)
private List books=new ArrayList();
作者(字符串名称){
this.name=名称;
}
作者(){}
无效集合id(长id){
this.id=id;
}
Long getId(){
返回id;
}
字符串getName(){
返回名称;
}
列出getBooks(){
还书;
}
}
另外,您不需要添加flush操作(.flatMap(Mutiny.Session::flush)
),因为withTransaction
已经为您添加了该操作
而且您也不需要
s.refresh
。不确定您为什么需要它。尝试创建setter,因为它在异常消息中抱怨:public void setId(Integer val)
。感谢您的回复,我添加了setter和getter,并将类型更改为Long,然后它就可以工作了fix commit
@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;
}
}