Do you know why this:
class Owner {
@OneToMany(mappedBy = "owner",
cascade = CascadeType.ALL)
public Set<Storage> storages
...
class Storage {
@ManyToOne
public Owner owner
...
doesn’t work as expected:
Storage s = new Storage("S1")
Owner o = new Owner("O1")
o.storages.add(s)
...
em.persistAndFlush(o)
...
s = Storage.get...
assert(s.owner != null, "Storage does not have an Owner")
o = Order.get...
assert(!o.storages.isEmpty, "Owner does not have any Storage")
The test fails:
[info] - Storage *** FAILED ***
[info] Storage does not have an Owner
How can I fix it?
This question gets asked once every 2 days. JPA uses the owning side of a bidirectional association to know if an association exists. The owning side is the side where there is no
mappedByattribute. So, to tell JPA that an association exists between owner and storage, you must set the owner of the storage: