From ce81d326ba894d4141e73462347016b8deb0e871 Mon Sep 17 00:00:00 2001 From: Date: Fri, 25 Oct 2024 09:46:08 +0100 Subject: [PATCH 1/2] HHH-18766 producer --- .../org/hibernate/bugs/JPAUnitTestCase.java | 34 ++++++- .../hibernate/bugs/ORMStandaloneTestCase.java | 40 -------- .../org/hibernate/bugs/ORMUnitTestCase.java | 79 --------------- .../java/org/hibernate/pojos/Catalogue.java | 49 +++++++++ .../java/org/hibernate/pojos/HitCount.java | 99 +++++++++++++++++++ .../org/hibernate/bugs/JPAUnitTestCase.java | 49 ++++++++- .../hibernate/bugs/ORMStandaloneTestCase.java | 40 -------- .../org/hibernate/bugs/ORMUnitTestCase.java | 70 ------------- .../bugs/QuarkusLikeORMUnitTestCase.java | 73 -------------- .../java/org/hibernate/pojos/Catalogue.java | 49 +++++++++ .../java/org/hibernate/pojos/HitCount.java | 99 +++++++++++++++++++ 11 files changed, 374 insertions(+), 307 deletions(-) delete mode 100644 orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMStandaloneTestCase.java delete mode 100644 orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java create mode 100644 orm/hibernate-orm-5/src/test/java/org/hibernate/pojos/Catalogue.java create mode 100644 orm/hibernate-orm-5/src/test/java/org/hibernate/pojos/HitCount.java delete mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMStandaloneTestCase.java delete mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java delete mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/QuarkusLikeORMUnitTestCase.java create mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/Catalogue.java create mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount.java diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java index f36c69e3..786644c5 100644 --- a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java @@ -31,7 +31,39 @@ public void destroy() { public void hhh123Test() throws Exception { EntityManager entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); - // Do stuff... + + /*-HitCount event = new HitCount(); + + entityManager.persist(event); + + Catalogue catalogue = new Catalogue(); + entityManager.persist(catalogue); + + Map args = new LinkedHashMap(); + + args.put("newPartnumber", "23456"); + args.put("partnumber", "12345"); + + String query = "update HitCount set partNumber=:newPartnumber where partNumber=:partnumber"; + // String query = "update HitCount hc set hc.partNumber=:newPartnumber + // where hc.partNumber=:partnumber"; + // String query = "update HitCount hc set hc.partNumber=:newPartnumber + // where fk(o.catalogue)=:partnumber"; + + Query dbQuery = entityManager.createQuery(query); + + for (Map.Entry entry : args.entrySet()) { + try { + + dbQuery.setParameter(entry.getKey(), entry.getValue()); + } catch (IllegalArgumentException e) { + // ignored + } + + } + + dbQuery.executeUpdate();*/ + entityManager.getTransaction().commit(); entityManager.close(); } diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMStandaloneTestCase.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMStandaloneTestCase.java deleted file mode 100644 index 92f3b364..00000000 --- a/orm/hibernate-orm-5/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.Before; -import org.junit.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! - */ -public class ORMStandaloneTestCase { - - private SessionFactory sf; - - @Before - public 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. - - @Test - public void hhh123Test() throws Exception { - - } -} diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java deleted file mode 100644 index 66757be0..00000000 --- a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java +++ /dev/null @@ -1,79 +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.Session; -import org.hibernate.Transaction; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.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! - */ -public class ORMUnitTestCase extends BaseCoreFunctionalTestCase { - - // Add your entities here. - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { -// Foo.class, -// Bar.class - }; - } - - // If you use *.hbm.xml mappings, instead of annotations, add the mappings here. - @Override - protected String[] getMappings() { - return new String[] { -// "Foo.hbm.xml", -// "Bar.hbm.xml" - }; - } - // If those mappings reside somewhere other than resources/org/hibernate/test, change this. - @Override - protected String getBaseForMappings() { - return "org/hibernate/test/"; - } - - // Add in any settings that are specific to your test. See resources/hibernate.properties for the defaults. - @Override - protected void configure(Configuration configuration) { - super.configure( configuration ); - - configuration.setProperty( AvailableSettings.SHOW_SQL, Boolean.TRUE.toString() ); - configuration.setProperty( AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString() ); - //configuration.setProperty( AvailableSettings.GENERATE_STATISTICS, "true" ); - } - - // Add your tests, using standard JUnit. - @Test - public void hhh123Test() throws Exception { - // BaseCoreFunctionalTestCase automatically creates the SessionFactory and provides the Session. - Session s = openSession(); - Transaction tx = s.beginTransaction(); - // Do stuff... - tx.commit(); - s.close(); - } -} diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/pojos/Catalogue.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/pojos/Catalogue.java new file mode 100644 index 00000000..4dd2917a --- /dev/null +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/pojos/Catalogue.java @@ -0,0 +1,49 @@ +package org.hibernate.pojos; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class Catalogue { + + private String partNumber = "12345"; + private String userName = "testuser"; + + /** + * Gets the part number. + * + * @return the part number + */ + @Id + public String getPartNumber() { + return partNumber; + } + + /** + * Sets the part number. + * + * @param partNumber the new part number + */ + public void setPartNumber(String partNumber) { + this.partNumber = partNumber; + } + + /** + * Gets the user name. + * + * @return the user name + */ + public String getUserName() { + return userName; + } + + /** + * Sets the user name. + * + * @param userName the new user name + */ + public void setUserName(String userName) { + this.userName = userName; + } + +} diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/pojos/HitCount.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/pojos/HitCount.java new file mode 100644 index 00000000..7c5323ee --- /dev/null +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/pojos/HitCount.java @@ -0,0 +1,99 @@ +package org.hibernate.pojos; + +import org.hibernate.annotations.NotFound; +import org.hibernate.annotations.NotFoundAction; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +@Entity +public class HitCount { + + private String id = "123456789"; + // private String partNumber = "12345"; + private String userName = "testUser"; + + private Catalogue catalogue = null; + + /** + * Gets the id. + * + * @return the id + */ + @Id + public String getId() { + return id; + } + + /** + * Sets the id. + * + * @param id the new id + */ + public void setId(String id) { + this.id = id; + } + + /** + * Gets the part number. + * + * @return the part number + */ + // @Column(insertable = false, updatable = false) + // public String getPartNumber() { + // return partNumber; + // } + + /** + * Sets the part number. + * + * @param partNumber the new part number + */ + // public void setPartNumber(String partNumber) { + // this.partNumber = partNumber; + // } + + /** + * Gets the user name. + * + * @return the user name + */ + public String getUserName() { + return userName; + } + + /** + * Sets the user name. + * + * @param userName the new user name + */ + public void setUserName(String userName) { + this.userName = userName; + } + + /** + * Gets the catalogue. + * + * @return the catalogue + */ + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "partNumber") + @NotFound(action = NotFoundAction.IGNORE) + public Catalogue getCatalogue() { + return catalogue; + } + + /** + * Sets the catalogue + * + * @param catalogue the new catalogue + */ + public void setCatalogue(Catalogue catalogue) { + this.catalogue = catalogue; + } + +} 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..39a81593 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 java.util.LinkedHashMap; +import java.util.Map; + +import org.hibernate.pojos.Catalogue; +import org.hibernate.pojos.HitCount; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -7,9 +12,11 @@ import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; +import jakarta.persistence.Query; /** - * This template demonstrates how to develop a test case for Hibernate ORM, using the Java Persistence API. + * This template demonstrates how to develop a test case for Hibernate ORM, + * using the Java Persistence API. */ class JPAUnitTestCase { @@ -17,7 +24,8 @@ class JPAUnitTestCase { @BeforeEach void init() { - entityManagerFactory = Persistence.createEntityManagerFactory( "templatePU" ); + entityManagerFactory = Persistence + .createEntityManagerFactory("templatePU"); } @AfterEach @@ -29,9 +37,42 @@ void destroy() { // Add your tests, using standard JUnit. @Test void hhh123Test() throws Exception { - EntityManager entityManager = entityManagerFactory.createEntityManager(); + EntityManager entityManager = entityManagerFactory + .createEntityManager(); entityManager.getTransaction().begin(); - // Do stuff... + + HitCount event = new HitCount(); + + entityManager.persist(event); + + Catalogue catalogue = new Catalogue(); + entityManager.persist(catalogue); + + Map args = new LinkedHashMap(); + + args.put("newPartnumber", "23456"); + args.put("partnumber", "12345"); + + String query = "update HitCount set partNumber=:newPartnumber where partNumber=:partnumber"; + // String query = "update HitCount hc set hc.partNumber=:newPartnumber + // where hc.partNumber=:partnumber"; + // String query = "update HitCount hc set hc.partNumber=:newPartnumber + // where fk(o.catalogue)=:partnumber"; + + Query dbQuery = entityManager.createQuery(query); + + for (Map.Entry entry : args.entrySet()) { + try { + + dbQuery.setParameter(entry.getKey(), entry.getValue()); + } catch (IllegalArgumentException e) { + // ignored + } + + } + + dbQuery.executeUpdate(); + 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/QuarkusLikeORMUnitTestCase.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/QuarkusLikeORMUnitTestCase.java deleted file mode 100644 index f96bffd4..00000000 --- a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/QuarkusLikeORMUnitTestCase.java +++ /dev/null @@ -1,73 +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.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"), - - // Add your own settings that are a part of your quarkus configuration: - // @Setting( name = AvailableSettings.SOME_CONFIGURATION_PROPERTY, value = "SOME_VALUE" ), - } -) -@SessionFactory -@BytecodeEnhanced -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/java/org/hibernate/pojos/Catalogue.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/Catalogue.java new file mode 100644 index 00000000..e06501ed --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/Catalogue.java @@ -0,0 +1,49 @@ +package org.hibernate.pojos; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +@Entity +public class Catalogue { + + private String partNumber = "12345"; + private String userName = "testuser"; + + /** + * Gets the part number. + * + * @return the part number + */ + @Id + public String getPartNumber() { + return partNumber; + } + + /** + * Sets the part number. + * + * @param partNumber the new part number + */ + public void setPartNumber(String partNumber) { + this.partNumber = partNumber; + } + + /** + * Gets the user name. + * + * @return the user name + */ + public String getUserName() { + return userName; + } + + /** + * Sets the user name. + * + * @param userName the new user name + */ + public void setUserName(String userName) { + this.userName = userName; + } + +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount.java new file mode 100644 index 00000000..5cf9a30d --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount.java @@ -0,0 +1,99 @@ +package org.hibernate.pojos; + +import org.hibernate.annotations.NotFound; +import org.hibernate.annotations.NotFoundAction; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; + +@Entity +public class HitCount { + + private String id = "123456789"; + // private String partNumber = "12345"; + private String userName = "testUser"; + + private Catalogue catalogue = null; + + /** + * Gets the id. + * + * @return the id + */ + @Id + public String getId() { + return id; + } + + /** + * Sets the id. + * + * @param id the new id + */ + public void setId(String id) { + this.id = id; + } + + /** + * Gets the part number. + * + * @return the part number + */ + // @Column(insertable = false, updatable = false) + // public String getPartNumber() { + // return partNumber; + // } + + /** + * Sets the part number. + * + * @param partNumber the new part number + */ + // public void setPartNumber(String partNumber) { + // this.partNumber = partNumber; + // } + + /** + * Gets the user name. + * + * @return the user name + */ + public String getUserName() { + return userName; + } + + /** + * Sets the user name. + * + * @param userName the new user name + */ + public void setUserName(String userName) { + this.userName = userName; + } + + /** + * Gets the catalogue. + * + * @return the catalogue + */ + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "partNumber") + @NotFound(action = NotFoundAction.IGNORE) + public Catalogue getCatalogue() { + return catalogue; + } + + /** + * Sets the catalogue + * + * @param catalogue the new catalogue + */ + public void setCatalogue(Catalogue catalogue) { + this.catalogue = catalogue; + } + +} From 22ce8004357920762cae0fc1f0fe637202ee313c Mon Sep 17 00:00:00 2001 From: ghuber Date: Fri, 22 Nov 2024 10:23:20 +0000 Subject: [PATCH 2/2] Add mariadb/mysql test --- orm/hibernate-orm-6/pom.xml | 8 + .../org/hibernate/bugs/JPAUnitTestCase.java | 57 ++++++- .../org/hibernate/bugs/ORMUnitTestCase.java | 157 ++++++++++++++++++ .../java/org/hibernate/pojos/HitCount.java | 3 +- .../java/org/hibernate/pojos/HitCount_.java | 24 +++ .../src/test/resources/hibernate.properties | 17 +- 6 files changed, 253 insertions(+), 13 deletions(-) create mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java create mode 100644 orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount_.java diff --git a/orm/hibernate-orm-6/pom.xml b/orm/hibernate-orm-6/pom.xml index 9b02fdd5..b8171f5c 100644 --- a/orm/hibernate-orm-6/pom.xml +++ b/orm/hibernate-orm-6/pom.xml @@ -70,6 +70,14 @@ ${version.org.assertj.assertj-core} test + + + org.mariadb.jdbc + mariadb-java-client + 3.5.0 + provided + + 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 39a81593..d6605803 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,10 +1,14 @@ package org.hibernate.bugs; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + import java.util.LinkedHashMap; import java.util.Map; import org.hibernate.pojos.Catalogue; import org.hibernate.pojos.HitCount; +import org.hibernate.pojos.HitCount_; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -13,6 +17,10 @@ import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; import jakarta.persistence.Query; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Predicate; +import jakarta.persistence.criteria.Root; /** * This template demonstrates how to develop a test case for Hibernate ORM, @@ -37,28 +45,57 @@ void destroy() { // Add your tests, using standard JUnit. @Test void hhh123Test() throws Exception { + EntityManager entityManager = entityManagerFactory .createEntityManager(); entityManager.getTransaction().begin(); - HitCount event = new HitCount(); + Catalogue catalogue = new Catalogue(); + Catalogue catalogue1 = new Catalogue(); + catalogue1.setPartNumber("23456"); + + HitCount hc = new HitCount(); + hc.setCatalogue(catalogue); - entityManager.persist(event); + entityManager.persist(hc); - Catalogue catalogue = new Catalogue(); + // Catalogue catalogue = new Catalogue(); entityManager.persist(catalogue); + entityManager.persist(catalogue1); + + entityManager.flush(); Map args = new LinkedHashMap(); args.put("newPartnumber", "23456"); args.put("partnumber", "12345"); - String query = "update HitCount set partNumber=:newPartnumber where partNumber=:partnumber"; - // String query = "update HitCount hc set hc.partNumber=:newPartnumber - // where hc.partNumber=:partnumber"; + // String query = "update HitCount set partNumber=:newPartnumber where + // partNumber=:partnumber"; + // String query = "update HitCount set catalogue.id=:newPartnumber where + // catalogue.id=:partnumber"; // String query = "update HitCount hc set hc.partNumber=:newPartnumber // where fk(o.catalogue)=:partnumber"; + CriteriaBuilder builder = entityManager.getCriteriaBuilder(); + + CriteriaQuery criteriaQuery = builder + .createQuery(HitCount.class); + Root root = criteriaQuery.from(HitCount.class); + + criteriaQuery.select(root); + + Predicate before = builder.equal(root.get(HitCount_.catalogue), + catalogue1); + + org.hibernate.query.Query queryb = (org.hibernate.query.Query) entityManager + .createQuery(criteriaQuery.where(before)); + + HitCount obj = queryb.getSingleResultOrNull(); + assertNull(obj, "Should be null"); + + String query = "update HitCount set catalogue.id=:newPartnumber where catalogue.id=:partnumber"; + Query dbQuery = entityManager.createQuery(query); for (Map.Entry entry : args.entrySet()) { @@ -73,6 +110,14 @@ void hhh123Test() throws Exception { dbQuery.executeUpdate(); + entityManager.flush(); + + org.hibernate.query.Query querya = (org.hibernate.query.Query) entityManager + .createQuery(criteriaQuery.where(before)); + + HitCount objAfter = querya.getSingleResultOrNull(); + assertNotNull(objAfter, "Should not be null"); + entityManager.getTransaction().commit(); entityManager.close(); } 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 new file mode 100644 index 00000000..4de2edeb --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java @@ -0,0 +1,157 @@ +/* + * 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 static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.pojos.Catalogue; +import org.hibernate.pojos.HitCount; +import org.hibernate.pojos.HitCount_; +import org.hibernate.query.MutationQuery; +import org.hibernate.query.Query; +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; + +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Predicate; +import jakarta.persistence.criteria.Root; + +/** + * 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. + Catalogue.class, HitCount.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... + + // Test for mariadb Server version: 10.11.6-MariaDB-0+deb12u1 - + // Debian 12 see hibernate.properties + + Catalogue catalogue = new Catalogue(); + Catalogue catalogue1 = new Catalogue(); + catalogue1.setPartNumber("23456"); + + HitCount hc = new HitCount(); + hc.setCatalogue(catalogue); + + session.persist(hc); + + // Catalogue catalogue = new Catalogue(); + session.persist(catalogue); + session.persist(catalogue1); + + session.flush(); + + Map args = new LinkedHashMap(); + + args.put("newPartnumber", "23456"); + args.put("partnumber", "12345"); + + CriteriaBuilder builder = session.getCriteriaBuilder(); + + CriteriaQuery criteriaQuery = builder + .createQuery(HitCount.class); + Root root = criteriaQuery.from(HitCount.class); + + criteriaQuery.select(root); + + Predicate before = builder.equal(root.get(HitCount_.catalogue), + catalogue1); + + Query querybz = session + .createQuery(criteriaQuery.where(before)); + + HitCount obj = querybz.getSingleResultOrNull(); + assertNull(obj, "Should be null"); + + // Verify test works, uncomment partNumber in HitCount pojo. + // String query = "update HitCount set partNumber=:newPartnumber + // where partNumber=:partnumber"; + + String query = "update HitCount set catalogue.id=:newPartnumber where catalogue.id=:partnumber"; + + MutationQuery dbQuery = session.createMutationQuery(query); + + for (Map.Entry entry : args.entrySet()) { + try { + + dbQuery.setParameter(entry.getKey(), entry.getValue()); + } catch (IllegalArgumentException e) { + // ignored + } + + } + + dbQuery.executeUpdate(); + + session.flush(); + session.clear(); + + Query querya = session + .createQuery(criteriaQuery.where(before)); + + HitCount objAfter = querya.getSingleResultOrNull(); + assertNotNull(objAfter, "Should not be null"); + + System.out.println(objAfter.getId()); + + }); + } +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount.java index 5cf9a30d..2257fc30 100644 --- a/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount.java +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount.java @@ -3,7 +3,6 @@ import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; -import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.Id; @@ -15,7 +14,7 @@ public class HitCount { private String id = "123456789"; // private String partNumber = "12345"; - private String userName = "testUser"; + private String userName = "testuser"; private Catalogue catalogue = null; diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount_.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount_.java new file mode 100644 index 00000000..50e26157 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/pojos/HitCount_.java @@ -0,0 +1,24 @@ +package org.hibernate.pojos; + +import jakarta.annotation.Generated; +import jakarta.persistence.metamodel.EntityType; +import jakarta.persistence.metamodel.SingularAttribute; +import jakarta.persistence.metamodel.StaticMetamodel; + +@StaticMetamodel(HitCount.class) +@Generated("org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor") +public abstract class HitCount_ { + + public static volatile SingularAttribute catalogue; + + public static volatile SingularAttribute userName; + + public static volatile SingularAttribute id; + + public static volatile EntityType class_; + + public static final String CATALOGUE = "catalogue"; + public static final String USER_NAME = "userName"; + public static final String ID = "id"; + +} diff --git a/orm/hibernate-orm-6/src/test/resources/hibernate.properties b/orm/hibernate-orm-6/src/test/resources/hibernate.properties index 9c894d7b..e30aa8de 100644 --- a/orm/hibernate-orm-6/src/test/resources/hibernate.properties +++ b/orm/hibernate-orm-6/src/test/resources/hibernate.properties @@ -5,12 +5,19 @@ # See the lgpl.txt file in the root directory or . # -hibernate.dialect org.hibernate.dialect.H2Dialect -hibernate.connection.driver_class org.h2.Driver +#hibernate.dialect org.hibernate.dialect.H2Dialect +#hibernate.dialect org.hibernate.dialect.MySQLDialect +#hibernate.connection.driver_class org.h2.Driver +hibernate.connection.driver_class org.mariadb.jdbc.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 -hibernate.connection.username sa -hibernate.connection.password +#hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1 +hibernate.connection.url jdbc:mariadb://localhost:3306/dev?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&mysqlEncoding=utf8 + +#hibernate.connection.username sa +#hibernate.connection.password + +hibernate.connection.username testuser +hibernate.connection.password 1234 hibernate.connection.pool_size 5