gpt4 book ai didi

java - hibernate 一对多标准不起作用

转载 作者:行者123 更新时间:2023-12-05 00:07:12 24 4
gpt4 key购买 nike

我有以下实体:

@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

数据库中显示以下数据:

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 功能找到所有写过包含适当标签的帖子的作者。所有那些只有那些包含指定标签(见上文 - 'Football','Basketball')的帖子的作者都必须被退回。

我写了这段代码:

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);

我希望收到:

只有一篇文章 (post_id = 26) 的作者 (author_id = 54) 和这篇文章包含两个标签('Football' 和 'Basketball'),因为我使用上面的 SQL 查询收到了它。

但实际结果是我收到了作者 (author_id = 54),他的所有帖子都显示在数据库中(这里有错误和问题!!!)并且每个帖子都包含所有标签,这些标签也显示在数据库中。

intellij-idea-debug-result

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 实现预期的正确过滤结果?

最佳答案

你问了一个写关于足球或篮球的博客的作者:

DetachedCriteria.forClass(Author.class, "author");

碰巧这位作者也写了一些别的东西的博客。所以你得到了你所要求的。在您的 sql 语句中,您要求投影,而在 hibernate 中,您要求 ORM 获取对象 (author) 及其 posts 集合。

关于java - hibernate 一对多标准不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36890154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com