[SOLVED] Java, Hibernate and, @ManyToMany annotation - no results from one of Entity
I have an odd problem with relation as collection.
The problem was my collecion of Bars element returns 0 rows.
My exaples class was like this:
2. I decided to check what query Hibernate generated when
@ManyToMany. In my case I had a Set
/**
* Foo is NOT owner of relation
*/
class Foo {
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "foos")
Collection bars;
}
/**
* 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;
}
Solution:
1. I tried change 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)?
Komentarze
Prześlij komentarz