Skip to content

Testcase for CriteriaQuery for Sum of Case/When/then using Enumeration - Hibernate only #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.hibernate.bugs;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.criteria.Selection;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -31,7 +36,29 @@ void destroy() {
void hhh123Test() throws Exception {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// Do stuff...

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

CriteriaQuery<OurEntityPercentageStatus> criteriaQuery = criteriaBuilder.createQuery(OurEntityPercentageStatus.class);
Root<OurEntity> root = criteriaQuery.from(OurEntity.class);
criteriaQuery.groupBy(root.get("commonName"));

OurStatus ourStatus = OurStatus.STATUS_1;

// Get percentage of ourEntities in status STATUS_1. PSEUDOCODE: SUM(case when our_status == STATUS_1 then 1 else 0 end) / cast(Count(*) as decimal)) * 100 as percentage_out
Selection<Double> percentageOutSelection = criteriaBuilder.prod(criteriaBuilder.quot(criteriaBuilder.sum(
criteriaBuilder.selectCase(root.get("ourStatus"))
.when(ourStatus.ordinal(), 1.0)
.otherwise(0.0)
.as(Double.class)), criteriaBuilder.count(root)), 100.0).as(Double.class).alias("percentage_out");

//Get objects with dealerDatabaseId and percentage_out
criteriaQuery.select(
criteriaBuilder.construct(OurEntityPercentageStatus.class, root.get("commonName"), percentageOutSelection));
CriteriaQuery<OurEntityPercentageStatus> finalQuery = criteriaQuery.multiselect(root.get("commonName"),
percentageOutSelection);
List<OurEntityPercentageStatus> resultList = entityManager.createQuery(finalQuery).getResultList();

entityManager.getTransaction().commit();
entityManager.close();
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.hibernate.bugs;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.SequenceGenerator;

@Entity
public class OurEntity {

@Id
@SequenceGenerator(name = "ourEntitySeq", sequenceName = "OUR_ENTITY_ID_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ourEntitySeq")
private Long id;
@Column(name = "name")
private String name;

@Column(name = "common_name")
private String commonName;
@Column(name = "our_status")
private OurStatus ourStatus;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCommonName() {
return commonName;
}

public void setCommonName(String common_name) {
this.commonName = common_name;
}

public OurStatus getOurStatus() {
return ourStatus;
}

public void setOurStatus(OurStatus ourStatus) {
this.ourStatus = ourStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.hibernate.bugs;

public class OurEntityPercentageStatus {

private String commonName;
private Double percentageStatus;

public OurEntityPercentageStatus(String commonName, Double percentageStatus) {
this.commonName = commonName;
this.percentageStatus = percentageStatus;
}

public String getCommonName() {
return commonName;
}

public void setCommonName(String commonName) {
this.commonName = commonName;
}

public Double getPercentageOut() {
return percentageStatus;
}

public void setPercentageOut(Double percentageOut) {
this.percentageStatus = percentageOut;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.hibernate.bugs;

public enum OurStatus {
STATUS_1,
STATUS_2,
;
}

This file was deleted.

This file was deleted.

6 changes: 3 additions & 3 deletions orm/hibernate-orm-6/src/test/resources/hibernate.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
#

hibernate.dialect org.hibernate.dialect.H2Dialect
hibernate.dialect org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class org.h2.Driver
#hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE
hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1
Expand All @@ -14,7 +14,7 @@ hibernate.connection.password

hibernate.connection.pool_size 5

hibernate.show_sql false
hibernate.show_sql true
hibernate.format_sql true

hibernate.max_fetch_depth 5
Expand All @@ -27,4 +27,4 @@ hibernate.jdbc.batch_versioned_data true

jakarta.persistence.validation.mode=NONE
hibernate.service.allow_crawling=false
hibernate.session.events.log=true
hibernate.session.events.log=true