1

I have the following 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:

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).

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.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Marwan Tushyeh
  • 1,465
  • 4
  • 23
  • 41

1 Answers1

1

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.

You'll find more detailed explanations in this question

Guillaume
  • 13,039
  • 1
  • 38
  • 37