@ManyToMany. In my case I had a Set
/** * Foo is NOT owner of relation */ class Foo { @ManyToMany(fetch = FetchType.LAZY, mappedBy = "foos") CollectionSolution: 1. I tried changebars; } /** * Bar is owner of relation */ class Bar { @OneToOne(fetch = FetchType.EAGER, mappedBy = "bar", optional = false, cascade = CascadeType.ALL) private RequiredFieldA requiredFieldA; @OneToOne(fetch = FetchType.EAGER, mappedBy = "bar", optional = false, cascade = CascadeType.ALL) private RequiredFieldB requiredFieldB; @OneToOne(fetch = FetchType.EAGER, mappedBy = "bar", optional = false, cascade = CascadeType.ALL) private RequiredFieldC requiredFieldC; @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "BAR_FOO", joinColumns = @JoinColumn(name = "BAR_ID"), inverseJoinColumns = @JoinColumn(name = "FOO_ID")) Collection foos; }
FetchTypefrom LAZY to EAGER - it works but I must save LAZY.
2. I decided to check what query Hibernate generated when
FetchTypeis LAZY. Results: In my Bar entity I have required fields but in my Database i haven't got it. But the most odd is that hibernate generated INNER JOIN with only one (only first) field. This returns 0 rows becouse my first required field has no row in database for entity Bar. The question is: - Why FetchType.EAGER has no problems with required fileds? - Why FetchType.LAZY INNER JOIN with ONLY ONE of required fileds (and join with only the first field)?
Brak komentarzy:
Prześlij komentarz