diff --git a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/HibernateUtil.java b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/HibernateUtil.java index 58724e690c..84b20197bd 100644 --- a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/HibernateUtil.java +++ b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/HibernateUtil.java @@ -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) diff --git a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/distinct/entities/Comment.java b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/distinct/entities/Comment.java new file mode 100644 index 0000000000..1d155b112e --- /dev/null +++ b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/distinct/entities/Comment.java @@ -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 + '\'' + '}'; + } +} diff --git a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/distinct/entities/Post.java b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/distinct/entities/Post.java new file mode 100644 index 0000000000..d2608e614c --- /dev/null +++ b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/distinct/entities/Post.java @@ -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 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 getComments() { + return comments; + } + + public void setComments(List comments) { + this.comments = comments; + } + + @Override + public String toString() { + return "Post{" + "id=" + id + ", title='" + title + '\'' + ", comments=" + comments + '}'; + } +} diff --git a/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/distinct/entities/DistinctHqlQueriesUnitTest.java b/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/distinct/entities/DistinctHqlQueriesUnitTest.java new file mode 100644 index 0000000000..799439a51b --- /dev/null +++ b/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/distinct/entities/DistinctHqlQueriesUnitTest.java @@ -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 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 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 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); + } +} \ No newline at end of file