HHH-18809 improve the test

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-11-05 11:20:55 +01:00
parent 9886b7acf9
commit b83a7fef80
1 changed files with 19 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import jakarta.persistence.Entity;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import org.hibernate.annotations.Formula; import org.hibernate.annotations.Formula;
import org.hibernate.annotations.Generated; import org.hibernate.annotations.Generated;
import org.hibernate.generator.EventType;
import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.SessionFactoryScope;
@ -37,12 +38,14 @@ public class FormulaGeneratedTest {
assertEquals( "new", entity.status ); assertEquals( "new", entity.status );
assertEquals( unitPrice, entity.unitPrice ); assertEquals( unitPrice, entity.unitPrice );
assertEquals( 5, entity.quantity ); assertEquals( 5, entity.quantity );
assertEquals( 64.95f, entity.total.floatValue(), 0.01f );
} ); } );
scope.inTransaction( session -> { scope.inTransaction( session -> {
OrderLine entity = session.createQuery("from WithDefault", OrderLine.class ).getSingleResult(); OrderLine entity = session.createQuery("from WithDefault", OrderLine.class ).getSingleResult();
assertEquals( unitPrice, entity.unitPrice ); assertEquals( unitPrice, entity.unitPrice );
assertEquals( 5, entity.quantity ); assertEquals( 5, entity.quantity );
assertEquals( "new", entity.status ); assertEquals( "new", entity.status );
assertEquals( 64.95f, entity.total.floatValue(), 0.01f );
entity.status = "old"; //should be ignored when fetch=true entity.status = "old"; //should be ignored when fetch=true
} ); } );
scope.inTransaction( session -> { scope.inTransaction( session -> {
@ -50,6 +53,17 @@ public class FormulaGeneratedTest {
assertEquals( unitPrice, entity.unitPrice ); assertEquals( unitPrice, entity.unitPrice );
assertEquals( 5, entity.quantity ); assertEquals( 5, entity.quantity );
assertEquals( "new", entity.status ); assertEquals( "new", entity.status );
assertEquals( 64.95f, entity.total.floatValue(), 0.01f );
entity.quantity = 10;
session.flush();
assertEquals( 129.90f, entity.total.floatValue(), 0.01f );
} );
scope.inTransaction( session -> {
OrderLine entity = session.createQuery("from WithDefault", OrderLine.class ).getSingleResult();
assertEquals( unitPrice, entity.unitPrice );
assertEquals( 10, entity.quantity );
assertEquals( "new", entity.status );
assertEquals( 129.90f, entity.total.floatValue(), 0.01f );
} ); } );
} }
@ -61,12 +75,16 @@ public class FormulaGeneratedTest {
@Entity(name="WithDefault") @Entity(name="WithDefault")
public static class OrderLine { public static class OrderLine {
@Id @Id
private long id;
private BigDecimal unitPrice; private BigDecimal unitPrice;
@Id
private int quantity = 1; private int quantity = 1;
@Generated @Generated
@Formula(value = "'new'") @Formula(value = "'new'")
private String status; private String status;
@Generated(event = {EventType.INSERT, EventType.UPDATE})
@Formula(value = "quantity*unitPrice")
private BigDecimal total;
public OrderLine() {} public OrderLine() {}
public OrderLine(BigDecimal unitPrice, int quantity) { public OrderLine(BigDecimal unitPrice, int quantity) {