HHH-18809 support @Formula @Generated
Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
parent
18ccd6ed80
commit
e60d674bfe
|
@ -10,7 +10,6 @@ import java.util.Locale;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.hibernate.internal.util.StringHelper;
|
|
||||||
import org.hibernate.metamodel.mapping.JdbcMapping;
|
import org.hibernate.metamodel.mapping.JdbcMapping;
|
||||||
import org.hibernate.metamodel.mapping.SelectableMapping;
|
import org.hibernate.metamodel.mapping.SelectableMapping;
|
||||||
import org.hibernate.metamodel.mapping.SelectablePath;
|
import org.hibernate.metamodel.mapping.SelectablePath;
|
||||||
|
@ -22,6 +21,7 @@ import org.hibernate.sql.ast.tree.update.Assignable;
|
||||||
|
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
|
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;
|
||||||
import static org.hibernate.internal.util.StringHelper.replace;
|
import static org.hibernate.internal.util.StringHelper.replace;
|
||||||
import static org.hibernate.sql.Template.TEMPLATE;
|
import static org.hibernate.sql.Template.TEMPLATE;
|
||||||
|
|
||||||
|
@ -116,21 +116,20 @@ public class ColumnReference implements Expression, Assignable {
|
||||||
boolean isFormula,
|
boolean isFormula,
|
||||||
String customReadExpression,
|
String customReadExpression,
|
||||||
JdbcMapping jdbcMapping) {
|
JdbcMapping jdbcMapping) {
|
||||||
this.qualifier = StringHelper.nullIfEmpty( qualifier );
|
this.qualifier = nullIfEmpty( qualifier );
|
||||||
|
|
||||||
if ( isFormula ) {
|
if ( isFormula ) {
|
||||||
assert qualifier != null;
|
this.columnExpression = qualifier == null
|
||||||
this.columnExpression = replace( columnExpression, TEMPLATE, qualifier );
|
? replace( columnExpression, TEMPLATE + '.', "" )
|
||||||
|
: replace( columnExpression, TEMPLATE, qualifier );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.columnExpression = columnExpression;
|
this.columnExpression = columnExpression;
|
||||||
}
|
}
|
||||||
if ( selectablePath == null ) {
|
|
||||||
this.selectablePath = new SelectablePath( this.columnExpression );
|
this.selectablePath = selectablePath == null
|
||||||
}
|
? new SelectablePath( this.columnExpression )
|
||||||
else {
|
: selectablePath;
|
||||||
this.selectablePath = selectablePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.isFormula = isFormula;
|
this.isFormula = isFormula;
|
||||||
this.readExpression = customReadExpression;
|
this.readExpression = customReadExpression;
|
||||||
|
@ -181,12 +180,9 @@ public class ColumnReference implements Expression, Assignable {
|
||||||
appender.accept( columnExpression );
|
appender.accept( columnExpression );
|
||||||
}
|
}
|
||||||
else if ( readExpression != null ) {
|
else if ( readExpression != null ) {
|
||||||
if ( qualifier == null ) {
|
appender.accept( qualifier == null
|
||||||
appender.accept( replace( readExpression, TEMPLATE + ".", "" ) );
|
? replace( readExpression, TEMPLATE + '.', "" )
|
||||||
}
|
: replace( readExpression, TEMPLATE, qualifier ) );
|
||||||
else {
|
|
||||||
appender.accept( replace( readExpression, TEMPLATE, qualifier ) );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ( qualifier != null ) {
|
if ( qualifier != null ) {
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||||
|
* Copyright Red Hat Inc. and Hibernate Authors
|
||||||
|
*/
|
||||||
|
package org.hibernate.orm.test.mapping.generated.formula;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import org.hibernate.annotations.Formula;
|
||||||
|
import org.hibernate.annotations.Generated;
|
||||||
|
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.AfterEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
import static org.hibernate.cfg.JdbcSettings.USE_GET_GENERATED_KEYS;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Gavin King
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("JUnitMalformedDeclaration")
|
||||||
|
@DomainModel(annotatedClasses = FormulaGeneratedTest.OrderLine.class)
|
||||||
|
@SessionFactory
|
||||||
|
@ServiceRegistry(settings = @Setting(name = USE_GET_GENERATED_KEYS, value = "false"))
|
||||||
|
public class FormulaGeneratedTest {
|
||||||
|
|
||||||
|
@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( "new", 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( "new", 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( "new", entity.status );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
private int quantity = 1;
|
||||||
|
@Generated
|
||||||
|
@Formula(value = "'new'")
|
||||||
|
private String status;
|
||||||
|
|
||||||
|
public OrderLine() {}
|
||||||
|
public OrderLine(BigDecimal unitPrice, int quantity) {
|
||||||
|
this.unitPrice = unitPrice;
|
||||||
|
this.quantity = quantity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue