我有以下实体:
@Entity
@Table(name = "author")
public class Author implements Serializable {
private static final long serialVersionUID = 12345L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "author_id")
private int authorId;
@Column(name = "author_bio")
private String authorBio;
@Column(name = "author_email")
private String authorEmail;
@Column(name = "author_favourite_section")
private String authorFavouriteSection;
@Column(name = "author_password")
private String authorPassword;
@Column(name = "author_username")
private String authorUsername;
@OneToOne(mappedBy = "author", fetch = FetchType.LAZY)
private Blog blog;
@OneToMany(mappedBy = "author", fetch = FetchType.LAZY)
private List<Post> posts;
// getters and setters
@Entity
@Table(name = "blog")
public class Blog implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "blog_id")
private int blogId;
@Column(name = "blog_title")
private String blogTitle;
@OneToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "blog_author_id", unique = true)
private Author author;
@OneToMany(mappedBy = "blog", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Post> posts;
// getters and setters
@Entity
@Table(name = "post")
public class Post implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "post_id")
private int postId;
@Column(name = "post_subject")
private String postSubject;
@Column(name = "post_body")
private String postBody;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "blog_id")
private Blog blog;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "post_author_id")
private Author author;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "post_tag", joinColumns = {
@JoinColumn(name = "post_id", nullable = false, updatable = false)},
inverseJoinColumns = {@JoinColumn(name = "tag_id",
nullable = false, updatable = false)})
private Set<Tag> tags = new HashSet<Tag>();
// getters and setters
@Entity
@Table(name = "tag")
public class Tag implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "tag_id")
private int tagId;
@Column(name = "tag_name")
private String tagName;
@ManyToMany(mappedBy = "tags", fetch = FetchType.LAZY)
private Set<Post> posts = new HashSet<Post>();
// getters and setters
以下数据显示在db:
中author-blog-post-tag-AND-post_tag-tables
实现的主要目标是:查找所有包含适当标签的书面帖子的作者。
我可以使用SQL查询来完成:
SELECT a.author_id, a.author_bio, p.post_id, p.post_subject, t.tag_id, t.tag_name from author a
join blog b
on a.author_id = b.blog_author_id
join post p
on p.post_author_id = a.author_id
join post_tag pt
on p.post_id = pt.post_id
join tag t
on t.tag_id = pt.tag_id
where t.tag_name in ('Football', 'Basketball')
使用作者,过滤的帖子和标签返回正确的结果。
但我需要使用hibernate来做。
因此,使用hibernate我想找到所有包含适当标签的书面帖子的作者。 所有那些仅包含指示标签的帖子(见上文 - '足球','篮球')的作者都必须退回。
我写了这段代码:
final DetachedCriteria authorCriteria = DetachedCriteria.forClass(Author.class, "author");
authorCriteria.createAlias("author.posts", "post");
authorCriteria.createAlias("post.tags", "tag");
Criterion football = Restrictions.eq("tag.tagName", "Football");
Criterion basketball = Restrictions.eq("tag.tagName", "Basketball");
authorCriteria.add(Restrictions.or(football, basketball));
authorCriteria
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
final List<Author> result = (List<Author>)getConfiguredHibernateTemplate().findByCriteria(authorCriteria);
我希望收到:
作者(author_id = 54)只有一个帖子(post_id = 26),这个帖子包含两个标签('足球'和'篮球'),因为我使用上面的SQL查询收到它。
但实际结果是我收到了作者(author_id = 54),其中包含所有他在DB中的帖子(错误和问题在这里!!!),每个帖子都包含所有在db中显示的标签。
Hibernate生成了以下查询:
select this_.author_id as author_i1_0_2_, this_.author_bio as author_b2_0_2_, this_.author_email as author_e3_0_2_, this_.author_favourite_section as author_f4_0_2_, this_.author_password as author_p5_0_2_, this_.author_username as author_u6_0_2_, post1_.post_id as post_id1_2_0_, post1_.post_author_id as post_aut4_2_0_, post1_.blog_id as blog_id5_2_0_, post1_.post_body as post_bod2_2_0_, post1_.post_subject as post_sub3_2_0_, tags5_.post_id as post_id1_2_, tag2_.tag_id as tag_id2_3_, tag2_.tag_id as tag_id1_4_1_, tag2_.tag_name as tag_name2_4_1_ from author this_ inner join post post1_ on this_.author_id=post1_.post_author_id inner join post_tag tags5_ on post1_.post_id=tags5_.post_id inner join tag tag2_ on tags5_.tag_id=tag2_.tag_id where (tag2_.tag_name=? or tag2_.tag_name=?)
select blog0_.blog_id as blog_id1_1_0_, blog0_.blog_author_id as blog_aut3_1_0_, blog0_.blog_title as blog_tit2_1_0_ from blog blog0_ where blog0_.blog_author_id=?
select posts0_.post_author_id as post_aut4_0_0_, posts0_.post_id as post_id1_2_0_, posts0_.post_id as post_id1_2_1_, posts0_.post_author_id as post_aut4_2_1_, posts0_.blog_id as blog_id5_2_1_, posts0_.post_body as post_bod2_2_1_, posts0_.post_subject as post_sub3_2_1_ from post posts0_ where posts0_.post_author_id=?
如何使用hibernate实现预期和正确过滤的结果?
答案 0 :(得分:0)
您要求作者撰写关于Football或BaketBall的博客:
DetachedCriteria.forClass(Author.class, "author");
碰巧这位作者还撰写了关于其他内容的博客。所以你得到了你所要求的东西。在你的sql语句中,你要求进行投射,而使用hibernate,你要求ORM通过其帖子集合获取对象(作者)。
答案 1 :(得分:0)
我尝试使用投影(authorCriteria.setResultTransformer(CriteriaSpecification.PROJECTION)
final DetachedCriteria authorCriteria = DetachedCriteria.forClass(Author.class, "author");
authorCriteria.createAlias("author.posts", "post");
authorCriteria.createAlias("post.tags", "tag");
final Criterion football = Restrictions.eq("tag.tagName", "Football");
final Criterion basketball = Restrictions.eq("tag.tagName", "Basketball");
authorCriteria.add(Restrictions.or(football, basketball));
authorCriteria.setResultTransformer(CriteriaSpecification.PROJECTION);
final List<Author> result = (List<Author>) getConfiguredHibernateTemplate().findByCriteria(authorCriteria);
好的,这是正确的,我可以分析这些数据并将它们分类到作者 - &gt;帖子列表 - &gt;带有特定帖子的标签。但这是代码中的额外工作。 我假设也许hibernate有更优雅的方式来返回我需要或不需要的过滤数据。如果不行,那么我对hibernate感到失望。然后使用一些spring jdbc模板或mybatis或类似的东西会更方便。
使用方法“然后您可以选择Post作为您的根实体”涉及对db的额外查询。为什么我们需要做额外的工作?看起来hibernate不够灵活,如果无法运行我想要的查询并以我喜欢的方式返回结果就足够了。