上面的快照显示了关系,因为我需要使用Criteria。 我有三个表是内容(Id - PK,标题),文章(Id - PK,articleId,sectionId)部分(Id - PK,标题)。 在数据库中没有使用外键概念。
@Entity
@Table(name="content")
public class Content{
private int id; //PK
private String title;
private Set<Article> article;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
@Column(name="title")
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
@OneToMany(fetch=FetchType.LAZY)
@JoinColumn(name="articleId")
public Set<Article> getArticle(){
return article;
}
public void setArticle(Set<Article> article){
this.article=article;
}
}
@Entity
@Table(name="article")
public class Article{
private int id; //PK
private int articleId;
private int sectionId;
private Set<Section> section;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
@Column(name="articleId")
public int getArticleId(){
return articleId;
}
public void setArticleId(int articleId){
this.articleId = articleId;
}
@Column(name="sectionId")
public int getSectionId(){
return sectionId;
}
public void setSectionId(int sectionId){
this.sectionId = sectionId;
}
@OneToMany(fetch=FetchType.LAZY)
@JoinColumn(name="id")
public Set<Section> getSection(){
return section;
}
public void setSection(Set<Section> section){
this.section = section;
}
}
我在这里使用Criteria加入Content,Article&amp;基于这些列使用标准的部分是
Criteria content = session.createCriteria(Content.class,"con");
con.createAlias("con.article", "ar");
con.createAlias("ar.section", "sec");
content.list();
这就是我得到的结果:
SELECT * FROM Content con inner join Article ar on con.id = ar.articleId INNER JOIN Section sec ON ar.id = sec.id
哪个不正确,如果我将其与预期的SQL查询匹配为:
SELECT * FROM Content con,Article ar,Section sec WHERE con.id = ar.articleId AND ar.sectionId = sec.id
任何人都可以帮我解决这个问题,如何使用Criteria作为相同的预期SQL查询来实现结果。
答案 0 :(得分:1)
// Firstly,you need entity to your Pojo like this:
// In Article, add a field: private Content content,support Getter and Setter for it
// In Section, add a field: private Article article,also Getter and Setter
// there have three Entity:content,article,section
// Create a DetachedCriteria for Section
DetachedCriteria sectionCriteria = DetachedCriteria
.forClass(Section.class);
// Create a SubDetachedCriteria for Article InnerJoin Section
// the article is Section's field
DetachedCriteria articleCriteria = sectionCriteria .createCriteria("article", DetachedCriteria.INNER_JOIN);
// Add join Restriction
// id is article's identity
// for this SQL:INNER JOIN Section sec ON ar.id=sec.id
articleCriteria.add(Restrictions.eq("id",section.id));
// Create a SubDetachedCriteria for Content InnerJoin Article
DetachedCriteria contentCriteria = articleCriteria.createCriteria("content", DetachedCriteria.INNER_JOIN);
// Add join Restriction
// for this SQL:inner join Article ar on con.id=ar.articleId
contentCriteria.add(Restrictions.eq("id",article.articleId));
// finally,after you finish the join,use the sectionCriteria to execute the CriteriaQuery
我希望它可以帮到你,因为我在我的项目中使用过它