tests for @DialectOverride.Xxxx

This commit is contained in:
Gavin King 2022-01-18 20:37:20 +01:00
parent 8aa0665731
commit 019f87106a
2 changed files with 115 additions and 2 deletions

View File

@ -12,8 +12,16 @@ import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.hibernate.annotations.DialectOverride;
import org.hibernate.annotations.Formula;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@ -22,7 +30,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
/**
@ -73,7 +80,8 @@ public class FormulaTests {
criteria.select( root );
criteria.where( criteriaBuilder.equal( root.get( "id" ), criteriaBuilder.literal( 1L ) ) );
Account account = session.createQuery( criteria ).uniqueResult();
assertThat( account.getInterest(), is( 62.5d ));
assertThat( account.getInterest(), is( 62.5d ) );
assertThat( account.getRatePercent(), is("1.25%") );
} );
}
@ -97,6 +105,23 @@ public class FormulaTests {
@Formula(value = "credit * rate")
private Double interest;
@Formula(value = "rate * 100 || '%'")
@DialectOverride.Formula(dialect = MySQLDialect.class,
override = @Formula("concat(rate * 100, '%')"))
@DialectOverride.Formula(dialect = HSQLDialect.class,
override = @Formula("replace(cast(rate * 100 as varchar(10)),'E0','') || '%'"))
@DialectOverride.Formula(dialect = DerbyDialect.class,
override = @Formula("trim(cast(cast(rate * 100 as decimal(10,2)) as char(10))) || '%'")) //LOL, Derby
@DialectOverride.Formula(dialect = DB2Dialect.class,
override = @Formula("varchar_format(rate * 100) || '%'"))
@DialectOverride.Formula(dialect = OracleDialect.class,
override = @Formula("to_char(rate * 100) || '%'"))
@DialectOverride.Formula(dialect = SQLServerDialect.class,
override = @Formula("ltrim(str(rate * 100, 10, 2)) + '%'"))
@DialectOverride.Formula(dialect = SybaseDialect.class,
override = @Formula("ltrim(str(rate * 100, 10, 2)) + '%'"))
private String ratePercent;
public Long getId() {
return id;
}
@ -129,5 +154,12 @@ public class FormulaTests {
this.interest = interest;
}
public String getRatePercent() {
return ratePercent;
}
public void setRatePercent(String ratePercent) {
this.ratePercent = ratePercent;
}
}
}

View File

@ -0,0 +1,81 @@
package org.hibernate.orm.test.mapping.generated.sqldefault;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DialectOverride;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.orm.junit.DomainModel;
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.Test;
import java.math.BigDecimal;
import static org.junit.Assert.assertEquals;
/**
* @author Gavin King
*/
@DomainModel(annotatedClasses = OverriddenDefaultTest.OrderLine.class)
@SessionFactory
public class OverriddenDefaultTest {
@Test
public void test(SessionFactoryScope scope) {
BigDecimal unitPrice = new BigDecimal("12.99");
scope.inTransaction( session -> {
OrderLine entity = new OrderLine( unitPrice, 5 );
session.persist(entity);
session.flush();
assertEquals( getDefault(scope), entity.status );
assertEquals( unitPrice, entity.unitPrice );
assertEquals( 5, entity.quantity );
} );
scope.inTransaction( session -> {
OrderLine entity = session.createQuery("from WithDefault", OrderLine.class ).getSingleResult();
assertEquals( unitPrice, entity.unitPrice );
assertEquals( 5, entity.quantity );
assertEquals( getDefault(scope), entity.status );
entity.status = "old"; //should be ignored when fetch=true
} );
scope.inTransaction( session -> {
OrderLine entity = session.createQuery("from WithDefault", OrderLine.class ).getSingleResult();
assertEquals( unitPrice, entity.unitPrice );
assertEquals( 5, entity.quantity );
assertEquals( getDefault(scope), entity.status );
} );
}
String getDefault(SessionFactoryScope scope) {
return scope.getMetadataImplementor().getDatabase().getDialect() instanceof H2Dialect ? "NEW" : "new";
}
@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete WithDefault" ).executeUpdate() );
}
@Entity(name="WithDefault")
public static class OrderLine {
@Id
private BigDecimal unitPrice;
@Id @ColumnDefault("1")
private int quantity;
@Generated(GenerationTime.INSERT)
@ColumnDefault("'new'")
@DialectOverride.ColumnDefault(dialect = H2Dialect.class,
sameOrAfter = @DialectOverride.Version(major=1, minor=4),
override = @ColumnDefault("'NEW'"))
private String status;
public OrderLine() {}
public OrderLine(BigDecimal unitPrice, int quantity) {
this.unitPrice = unitPrice;
this.quantity = quantity;
}
}
}