hibernate 如何坚持休眠React和复杂的id?

zbdgwd5y  于 7个月前  发布在  React
关注(0)|答案(1)|浏览(77)

当我想持久化Hibernate-reactive(hibernate-reactive-core 2.0.6.Final)& spring-boot & java 17时出现错误。
我的实体:

import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDateTime;

@Table(name = "MY_TABLE")
@Entity
@IdClass(MyTableId.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MyTableEntity {

    @Id
    @Column(name = "CODE_1")
    private String code1;

    @Column(name = "NUM")
    private Long num;

    @Id
    @Column(name = "CODE_2")
    private String code2;

    @Id
    @Column(name = "DATE")
    private LocalDateTime date;
}

字符串
类ID:

@AllArgsConstructor
@NoArgsConstructor
@Data
public class MyTableId implements Serializable {
    private String code1;
    private String code2;
    private LocalDateTime date;
}


我的仓库:

@Repository
@AllArgsConstructor
public class MyTableRepository {

    private Stage.SessionFactory sessionFactory;

    public Mono<Void> save(MyTableEntity myTableEntity) {
        return Mono.fromCompletionStage(
                sessionFactory.withSession(
                    session -> session.persist(myTableEntity)
                ));
    }

// ... others methods to find in MyTable


但是我在调用session. persistent时出现了一个错误:
java.util.concurrent.CompletableFuture@68e22cdc[已异常完成:java.util.concurrent.CompletionException:org.hibernate.HibernateException:java.util.concurrent.CompletionException:org.hibernate.property.access.spi. PropertyException:通过持久性属性[myPackagetity#code2]的反射访问字段[private java.lang.String myPackage.MyTablePackage.code2]时出错:MyTableEntity(code1=CODE_1,num=3,code2=ABCD1234,date=2023-11-08T21:21:39.835527200...
作为参考,我还有另外两个方法可以找到实体,它们可以工作.例如:

public Mono<MyTableEntity> findBlablabla(String code1) {
        return Mono.fromCompletionStage(
                sessionFactory.withSession(
                        session ->
                                session
                                        .createQuery("""
                                                        SELECT m from MyTableEntity m
                                                        WHERE c.code1 like :code1
                                                        ORDER BY c.date DESC
                                                        LIMIT 1
                                                        """
                                                , MyTableEntity.class)
                                        .setParameter("code1", code1)
                                        .getSingleResultOrNull()
                )
        );
    }


你知道吗?谢谢。

eivnm1vs

eivnm1vs1#

你试过为那些私有字段创建getter和setter吗?
关于documentation
1.3.2. Getters和setters在Quarkus环境外使用Hibernate Reactive时,您需要根据通常的JPA约定编写实体类,这需要:
用于持久属性的私有字段,以及
空值构造器。

相关问题