BAEL-5440: hql distinct queries (#12007)

* BAEL-5440: hql distinct queries

* BAEL-5440: formated the code and renamed test class
This commit is contained in:
etrandafir93 2022-04-17 14:23:51 +02:00 committed by GitHub
parent de77319b35
commit 49b6155952
4 changed files with 171 additions and 0 deletions

View File

@ -13,6 +13,8 @@ import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import com.baeldung.hibernate.customtypes.LocalDateStringType;
import com.baeldung.hibernate.distinct.entities.Post;
import com.baeldung.hibernate.distinct.entities.Comment;
import com.baeldung.hibernate.entities.DeptEmployee;
import com.baeldung.hibernate.pojo.Student;
@ -41,6 +43,8 @@ public class HibernateUtil {
metadataSources.addAnnotatedClass(Student.class);
metadataSources.addAnnotatedClass(DeptEmployee.class);
metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Department.class);
metadataSources.addAnnotatedClass(Comment.class);
metadataSources.addAnnotatedClass(Post.class);
Metadata metadata = metadataSources.getMetadataBuilder()
.applyBasicType(LocalDateStringType.INSTANCE)

View File

@ -0,0 +1,28 @@
package com.baeldung.hibernate.distinct.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String text;
public Comment() {
}
public Comment(String text) {
this.text = text;
}
@Override
public String toString() {
return "Comment{" + "id=" + id + ", text='" + text + '\'' + '}';
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.hibernate.distinct.entities;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String title;
@OneToMany
@Cascade(CascadeType.ALL)
private List<Comment> comments;
public Post() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Comment> getComments() {
return comments;
}
public void setComments(List<Comment> comments) {
this.comments = comments;
}
@Override
public String toString() {
return "Post{" + "id=" + id + ", title='" + title + '\'' + ", comments=" + comments + '}';
}
}

View File

@ -0,0 +1,80 @@
package com.baeldung.hibernate.distinct.entities;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.annotations.QueryHints;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.baeldung.hibernate.HibernateUtil;
public class DistinctHqlQueriesUnitTest {
private Session session;
@Before
public void setUp() throws IOException {
this.session = HibernateUtil.getSessionFactory()
.openSession();
saveDummyData();
}
private void saveDummyData() {
Transaction trx = session.beginTransaction();
session.createQuery("delete from Post")
.executeUpdate();
Post post = new Post();
post.setTitle("Distinct Queries in HQL");
post.setComments(Arrays.asList(new Comment("Great article!"), new Comment("Nice one :)"), new Comment("Keep up the good work!")));
session.persist(post);
trx.commit();
}
@After
public void tearDown() {
session.close();
}
@Test
public void whenExecutingSelectQuery_thereWillBeDuplicates() {
String hql = "SELECT p FROM Post p LEFT JOIN FETCH p.comments";
List<Post> posts = session.createQuery(hql, Post.class)
.getResultList();
assertThat(posts).hasSize(3);
}
@Test
public void whenExecutingSelectDistinctQuery_thereShouldBeNoDuplicates() {
String hql = "SELECT DISTINCT p FROM Post p LEFT JOIN FETCH p.comments";
List<Post> posts = session.createQuery(hql, Post.class)
.getResultList();
assertThat(posts).hasSize(1)
.allMatch(post -> post.getTitle()
.equals("Distinct Queries in HQL") && post.getComments()
.size() == 3);
}
@Test
public void whenExecutingSelectDistinctQueryWithHint_thereShouldBeNoDuplicates() {
String hql = "SELECT DISTINCT p FROM Post p LEFT JOIN FETCH p.comments";
List<Post> posts = session.createQuery(hql, Post.class)
.setHint(QueryHints.PASS_DISTINCT_THROUGH, false)
.getResultList();
assertThat(posts).hasSize(1)
.allMatch(post -> post.getTitle()
.equals("Distinct Queries in HQL") && post.getComments()
.size() == 3);
}
}