简体   繁体   English

Hibernate 2 级缓存:在 oneToMany 关系中创建实体时更新缓存集合

[英]Hibernate 2nd Level Cache: Update cached collection on creating of entity in oneToMany relation

I have the following class我有以下 class

@Entity(name = "table")
@Table(name = "table")
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class SomeClass {
    @Id
    long id;
    @OneToMany(mappedBy = "someObject")
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    private List<AnotherClass> anotherObjects;
}

And another class:还有另一个 class:

public class AnotherClass{
    @Id   
    long id;
    @ManyToOne
    @JoinColumn(name = "some_coulmn")
    SomeClass someObject;

}

The scenario is that when I create an object from type AnotherClass assigning it a certain someClass object and then retrieve that someClass object, I do not see the newly created object (unless I flush the cache and restart). The scenario is that when I create an object from type AnotherClass assigning it a certain someClass object and then retrieve that someClass object, I do not see the newly created object (unless I flush the cache and restart).

Is there anything I can do to update the collection representing the relation between two entities whenever a new entity is added to that relation?每当将新实体添加到该关系中时,我能做些什么来更新表示两个实体之间关系的集合吗?

Note: I am using hibernate 5.3 in Spring boot 2.1.8 project.注意:我在 Spring 启动 2.1.8 项目中使用 hibernate 5.3。

When you have a bi-directional relationship, I think it's usually advisable to update both ends.当您有双向关系时,我认为通常建议更新两端。 In your case, when creating an instance of AnotherClass and linking it to an instance of SomeClass, you should also add the AnotherClass into the corresponding anotherObjects List.在您的情况下,当创建 AnotherClass 的实例并将其链接到 SomeClass 的实例时,您还应该将 AnotherClass 添加到相应的 anotherObjects 列表中。

You'll find more detailed explanations in this question你会在这个问题中找到更详细的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM