diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java index 3734df49..daceda69 100644 --- a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java @@ -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; @@ -31,7 +36,29 @@ void destroy() { void hhh123Test() throws Exception { EntityManager entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); - // Do stuff... + + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(OurEntityPercentageStatus.class); + Root 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 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 finalQuery = criteriaQuery.multiselect(root.get("commonName"), + percentageOutSelection); + List resultList = entityManager.createQuery(finalQuery).getResultList(); + entityManager.getTransaction().commit(); entityManager.close(); } diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMStandaloneTestCase.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMStandaloneTestCase.java deleted file mode 100644 index 7b7615ed..00000000 --- a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMStandaloneTestCase.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.hibernate.bugs; - -import org.hibernate.SessionFactory; -import org.hibernate.boot.Metadata; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -/** - * This template demonstrates how to develop a standalone test case for Hibernate ORM. Although this is perfectly - * acceptable as a reproducer, usage of ORMUnitTestCase is preferred! - */ -class ORMStandaloneTestCase { - - private SessionFactory sf; - - @BeforeEach - void setup() { - StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder() - // Add in any settings that are specific to your test. See resources/hibernate.properties for the defaults. - .applySetting( "hibernate.show_sql", "true" ) - .applySetting( "hibernate.format_sql", "true" ) - .applySetting( "hibernate.hbm2ddl.auto", "update" ); - - Metadata metadata = new MetadataSources( srb.build() ) - // Add your entities here. - // .addAnnotatedClass( Foo.class ) - .buildMetadata(); - - sf = metadata.buildSessionFactory(); - } - - // Add your tests, using standard JUnit 5: - @Test - void hhh123Test() throws Exception { - - } -} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java deleted file mode 100644 index eefb7df4..00000000 --- a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2014 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.hibernate.bugs; - -import org.hibernate.cfg.AvailableSettings; - -import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.ServiceRegistry; -import org.hibernate.testing.orm.junit.SessionFactory; -import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.hibernate.testing.orm.junit.Setting; -import org.junit.jupiter.api.Test; - -/** - * This template demonstrates how to develop a test case for Hibernate ORM, using its built-in unit test framework. - * Although ORMStandaloneTestCase is perfectly acceptable as a reproducer, usage of this class is much preferred. - * Since we nearly always include a regression test with bug fixes, providing your reproducer using this method - * simplifies the process. - *

- * What's even better? Fork hibernate-orm itself, add your test case directly to a module's unit tests, then - * submit it as a PR! - */ -@DomainModel( - annotatedClasses = { - // Add your entities here. - // Foo.class, - // Bar.class - }, - // If you use *.hbm.xml mappings, instead of annotations, add the mappings here. - xmlMappings = { - // "org/hibernate/test/Foo.hbm.xml", - // "org/hibernate/test/Bar.hbm.xml" - } -) -@ServiceRegistry( - // Add in any settings that are specific to your test. See resources/hibernate.properties for the defaults. - settings = { - // For your own convenience to see generated queries: - @Setting(name = AvailableSettings.SHOW_SQL, value = "true"), - @Setting(name = AvailableSettings.FORMAT_SQL, value = "true"), - // @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), - - // Add your own settings that are a part of your quarkus configuration: - // @Setting( name = AvailableSettings.SOME_CONFIGURATION_PROPERTY, value = "SOME_VALUE" ), - } -) -@SessionFactory -class ORMUnitTestCase { - - // Add your tests, using standard JUnit 5. - @Test - void hhh123Test(SessionFactoryScope scope) throws Exception { - scope.inTransaction( session -> { - // Do stuff... - } ); - } -} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/OurEntity.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/OurEntity.java new file mode 100644 index 00000000..2c6345f2 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/OurEntity.java @@ -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; + } +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/OurEntityPercentageStatus.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/OurEntityPercentageStatus.java new file mode 100644 index 00000000..585980e1 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/OurEntityPercentageStatus.java @@ -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; + } +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/OurStatus.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/OurStatus.java new file mode 100644 index 00000000..7349524f --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/OurStatus.java @@ -0,0 +1,7 @@ +package org.hibernate.bugs; + +public enum OurStatus { + STATUS_1, + STATUS_2, + ; +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/QuarkusLikeEnhancementContext.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/QuarkusLikeEnhancementContext.java deleted file mode 100644 index 9d9e2d6e..00000000 --- a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/QuarkusLikeEnhancementContext.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.hibernate.bugs; - -import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; -import org.hibernate.bytecode.enhance.spi.UnloadedField; -import org.hibernate.bytecode.enhance.spi.UnsupportedEnhancementStrategy; - -public class QuarkusLikeEnhancementContext extends DefaultEnhancementContext { - @Override - public boolean doBiDirectionalAssociationManagement(final UnloadedField field) { - //Don't enable automatic association management as it's often too surprising. - //Also, there's several cases in which its semantics are of unspecified, - //such as what should happen when dealing with ordered collections. - return false; - } - - @Override - public UnsupportedEnhancementStrategy getUnsupportedEnhancementStrategy() { - // We expect model classes to be enhanced. - // Lack of enhancement could lead to many problems, - // from bad performance, to Quarkus-specific optimizations causing errors/data loss, - // to incorrect generated bytecode (references to non-existing methods). - // If something prevents enhancement, it's just safer to have Hibernate ORM's enhancer fail - // with a clear error message pointing to the application class that needs to be fixed. - return UnsupportedEnhancementStrategy.FAIL; - } -} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/QuarkusLikeORMUnitTestCase.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/QuarkusLikeORMUnitTestCase.java deleted file mode 100644 index 77c6a7af..00000000 --- a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/QuarkusLikeORMUnitTestCase.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2014 JBoss Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.hibernate.bugs; - -import org.hibernate.cfg.AvailableSettings; - -import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; -import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; -import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.ServiceRegistry; -import org.hibernate.testing.orm.junit.SessionFactory; -import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.hibernate.testing.orm.junit.Setting; -import org.junit.jupiter.api.Test; - -/** - * This template demonstrates how to develop a test case for Hibernate ORM, using its built-in unit test framework. - *

- * What's even better? Fork hibernate-orm itself, add your test case directly to a module's unit tests, then - * submit it as a PR! - */ -@DomainModel( - annotatedClasses = { - // Add your entities here, e.g.: - // Foo.class, - // Bar.class - } -) -@ServiceRegistry( - // Add in any settings that are specific to your test. See resources/hibernate.properties for the defaults. - settings = { - // For your own convenience to see generated queries: - @Setting(name = AvailableSettings.SHOW_SQL, value = "true"), - @Setting(name = AvailableSettings.FORMAT_SQL, value = "true"), - // @Setting( name = AvailableSettings.GENERATE_STATISTICS, value = "true" ), - - // Other settings that will make your test case run under similar configuration that Quarkus is using by default: - @Setting(name = AvailableSettings.PREFERRED_POOLED_OPTIMIZER, value = "pooled-lo"), - @Setting(name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "16"), - @Setting(name = AvailableSettings.BATCH_FETCH_STYLE, value = "PADDED"), - @Setting(name = AvailableSettings.QUERY_PLAN_CACHE_MAX_SIZE, value = "2048"), - @Setting(name = AvailableSettings.DEFAULT_NULL_ORDERING, value = "none"), - @Setting(name = AvailableSettings.IN_CLAUSE_PARAMETER_PADDING, value = "true"), - @Setting(name = AvailableSettings.SEQUENCE_INCREMENT_SIZE_MISMATCH_STRATEGY, value = "none"), - @Setting(name = AvailableSettings.ORDER_UPDATES, value = "true"), - - // Add your own settings that are a part of your quarkus configuration: - // @Setting( name = AvailableSettings.SOME_CONFIGURATION_PROPERTY, value = "SOME_VALUE" ), - } -) -@SessionFactory -@BytecodeEnhanced -@CustomEnhancementContext(QuarkusLikeEnhancementContext.class) -class QuarkusLikeORMUnitTestCase { - - // Add your tests, using standard JUnit. - @Test - void hhh123Test(SessionFactoryScope scope) throws Exception { - scope.inTransaction( session -> { - // Do stuff... - } ); - } -} diff --git a/orm/hibernate-orm-6/src/test/resources/hibernate.properties b/orm/hibernate-orm-6/src/test/resources/hibernate.properties index 9c894d7b..025a73bc 100644 --- a/orm/hibernate-orm-6/src/test/resources/hibernate.properties +++ b/orm/hibernate-orm-6/src/test/resources/hibernate.properties @@ -5,7 +5,7 @@ # See the lgpl.txt file in the root directory or . # -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 @@ -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 @@ -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 \ No newline at end of file +hibernate.session.events.log=true