From 30bfe8924661cf1b86025e6918cc392116706103 Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Wed, 26 Feb 2020 15:58:26 -0500 Subject: [PATCH] verify 'column quoting' --- .../mapping/quote/ColumnQuoteTest.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/quote/ColumnQuoteTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/quote/ColumnQuoteTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/quote/ColumnQuoteTest.java new file mode 100644 index 0000000000..c2ac8af43a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/metamodel/mapping/quote/ColumnQuoteTest.java @@ -0,0 +1,99 @@ +package org.hibernate.orm.test.metamodel.mapping.quote; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +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.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +@DomainModel( + annotatedClasses = { + ColumnQuoteTest.Product.class + } +) +@SessionFactory +@ServiceRegistry +@SuppressWarnings( "unused" ) +public class ColumnQuoteTest { + + private Long testProductId = 1L; + + private String fieldWithHibernateQuotingValue = "value1"; + private String fieldWithJpaQuotingValue = "value2"; + + private String changedFieldWithHibernateQuotingValue = "changedValue1"; + private String changedFieldWithJpaQuotingValue = "changedValue2"; + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { + Product product = new Product( testProductId, fieldWithHibernateQuotingValue, fieldWithJpaQuotingValue ); + session.save( product ); + } ); + scope.getSessionFactory().getQueryEngine().getInterpretationCache().close(); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.createQuery( "delete from Product" ).executeUpdate(); + } ); + } + + @Test + public void testGet(SessionFactoryScope scope) { + scope.inTransaction( session -> { + Product product = session.get( Product.class, testProductId ); + assertThat( product.fieldWithHibernateQuoating, is( fieldWithHibernateQuotingValue ) ); + assertThat( product.fieldWithJpaQuoting, is( fieldWithJpaQuotingValue ) ); + } ); + } + + @Test + public void testUpdate(SessionFactoryScope scope) { + scope.inTransaction( session -> { + Product product = session.get( Product.class, testProductId ); + product.fieldWithHibernateQuoating = changedFieldWithHibernateQuotingValue; + product.fieldWithJpaQuoting = changedFieldWithJpaQuotingValue; + session.flush(); + } ); + scope.inTransaction( session -> { + Product product = session.get( Product.class, testProductId ); + assertThat( product.fieldWithHibernateQuoating, is( changedFieldWithHibernateQuotingValue ) ); + assertThat( product.fieldWithJpaQuoting, is( changedFieldWithJpaQuotingValue ) ); + } ); + } + + @Entity(name = "Product") + @Table(name = "Product") + public static class Product { + @Id + private Long id; + + @Column(name = "`field1`") // Hibernate quoting + private String fieldWithHibernateQuoating; + + @Column(name = "\"field2\"") // JPA quoting + private String fieldWithJpaQuoting; + + public Product() { + } + + public Product(Long id, String fieldWithHibernateQuoating, String fieldWithJpaQuoting) { + this.id = id; + this.fieldWithHibernateQuoating = fieldWithHibernateQuoating; + this.fieldWithJpaQuoting = fieldWithJpaQuoting; + } + + } +}