here is my mapping, it can work. but when i load the "Syntax" with HibernateTemplate, data was wrong.
table info
_syntax
id
1
_syntax_example
id example syntax_id
1 a 1
_syntax_tag
id tag syntax_id
1 bb 1
2 cc 1
3 dd 1
I load the "Syntax" object has three SyntaxExample
syntaxExamples:a,a,a
tags:bb,cc,dd
Code:
@Entity
@Table(name="_syntax")
public class Syntax {
private int id;
private List<SyntaxExample> syntaxExamples = new ArrayList<SyntaxExample>();
private Set<SyntaxTag> syntaxTags = new HashSet<SyntaxTag>();
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@OneToMany(mappedBy="syntax", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
public List<SyntaxExample> getSyntaxExamples() {
return syntaxExamples;
}
public void setSyntaxExamples(List<SyntaxExample> syntaxExamples) {
this.syntaxExamples = syntaxExamples;
}
@OneToMany(mappedBy="syntax", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
public Set<SyntaxTag> getSyntaxTags() {
return syntaxTags;
}
public void setSyntaxTags(Set<SyntaxTag> tags) {
this.syntaxTags = tags;
}
}
@Entity
@Table(name="_syntax_example")
public class SyntaxExample {
private int id;
private String example;
private Syntax syntax;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getExample() {
return example;
}
public void setExample(String example) {
this.example = example;
}
@ManyToOne
@JoinColumn(name="syntax_id")
public Syntax getSyntax() {
return syntax;
}
public void setSyntax(Syntax syntax) {
this.syntax = syntax;
}
}
@Entity
@Table(name="_syntax_tag")
public class SyntaxTag {
private int id;
private String tag;
private Syntax syntax;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
@ManyToOne
@JoinColumn(name="syntax_id")
public Syntax getSyntax() {
return syntax;
}
public void setSyntax(Syntax syntax) {
this.syntax = syntax;
}
}
Code:
Hibernate:
select
syntax0_.id as id0_2_,
syntax0_.description as descript2_0_2_,
syntax0_.syntax as syntax0_2_,
syntaxexam1_.syntax_id as syntax3_0_4_,
syntaxexam1_.id as id4_,
syntaxexam1_.id as id1_0_,
syntaxexam1_.example as example1_0_,
syntaxexam1_.syntax_id as syntax3_1_0_,
syntaxtags2_.syntax_id as syntax3_0_5_,
syntaxtags2_.id as id5_,
syntaxtags2_.id as id2_1_,
syntaxtags2_.syntax_id as syntax3_2_1_,
syntaxtags2_.tag as tag2_1_
from
_syntax syntax0_
left outer join
_syntax_example syntaxexam1_
on syntax0_.id=syntaxexam1_.syntax_id
left outer join
_syntax_tag syntaxtags2_
on syntax0_.id=syntaxtags2_.syntax_id
where
syntax0_.id=?