保存实体后,Hibernate对于以@Formula注释的实体属性返回null

我有一个简单的类,如下所示:


@Entity

@Table(name = "post")

public class Post {


    @Id

    @GeneratedValue

    Long id;


    @Column(name = "title")

    String title;


    @Formula("(select current_date())")

    Date currentDate;


    @OneToMany(mappedBy = "post")

    Set<PostComment> commentList = new HashSet<>();

}

并希望在服务中更新此实体:


@Service

@Transactional

public class PostService {


    private final PostRepository postRepository;


    public PostService(PostRepository postRepository) {

        this.postRepository = postRepository;

    }


    @Transactional

    public Post save(Post entity) {

        Post post = postRepository.saveAndFlush(entity);

        System.out.println("current_date: " + post.getCurrentDate());

        post.getCommentList().forEach(pc -> System.out.println(pc.getReview()));

        return post;

    }

}

当我检查休眠日志时,它首先select是Post实体的所有字段,但是当我调用post.getCurrentDate()(用注释@Formula)时返回null:


Hibernate: select post0_.id as id1_0_0_, post0_.title as title2_0_0_, (select current_date()) as formula0_0_ from post post0_ where post0_.id=?

Hibernate: update post set title=? where id=?

current_date: null

Hibernate: select commentlis0_.post_id as post_id3_1_0_, commentlis0_.id as id1_1_0_, commentlis0_.id as id1_1_1_, commentlis0_.post_id as post_id3_1_1_, commentlis0_.review as review2_1_1_ from post_comments commentlis0_ where commentlis0_.post_id=?

review

为什么返回并记录commentList但不返回currentDate?是冬虫吗?


牧羊人nacy
浏览 269回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java