最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jpa - Hibernate persistent context - Stack Overflow

programmeradmin2浏览0评论

I have an entity

@Entity
public class FirstEntity {

  @Id
  @Column(name = "id", nullable = false)
  private UUID id;


  @Column(name = "second_id")
  private UUID secondId;

  @ManyToOne
  @JoinColumn(name = "second_id", nullable = false, insertable = false, updatable = false)
  private SecondEntity second;

}

The idea behind the double mapping of second_id is to prevent inserting/updating a SecondEntity while saving a FirstEntity. So when I save a FirstEntity, I just set the secondId field. But when I read it, the second field is all set.

My current problem is with this usecase:

  • I save a single FirstEntity
  • Then I get all of them for some logic

--> Hibernate use the FirstEntity it has in the persistent context (set after the save) when getting all entities : that entity has a null second field.

I can't find a clean way to fix this. Should I set both secondId and second fields ? Should I remove the new entity from the persistent context for force a clean fetch ? Also, I use spring transaction.

Thanks :)

发布评论

评论列表(0)

  1. 暂无评论