mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-18 17:15:02 +00:00
HHH-7619 formula attribute is not working on annotation side yet
This commit is contained in:
parent
4446594568
commit
f6787672ec
@ -100,6 +100,9 @@ public List<RelationalValueSource> relationalValueSources() {
|
||||
else if ( attributeOverride != null && attributeOverride.getColumnValues() != null ) {
|
||||
valueSources.add( new ColumnSourceImpl( attribute, attributeOverride, null ) );
|
||||
}
|
||||
else if ( attribute.getFormulaValue() != null ){
|
||||
valueSources.add( new DerivedValueSourceImpl( attribute.getFormulaValue() ) );
|
||||
}
|
||||
return valueSources;
|
||||
}
|
||||
|
||||
|
@ -71,7 +71,7 @@ public class BasicAttribute extends MappedAttribute {
|
||||
private final IdGenerator idGenerator;
|
||||
|
||||
/**
|
||||
* Is this a versioned property (annotated w/ {@code @Version}.
|
||||
* Is this a versioned property (annotated w/ {@code @Version}).
|
||||
*/
|
||||
private final boolean isVersioned;
|
||||
|
||||
@ -145,7 +145,6 @@ public static BasicAttribute createSimpleAttribute(String name,
|
||||
|
||||
checkBasicAnnotation();
|
||||
checkGeneratedAnnotation();
|
||||
|
||||
List<AnnotationInstance> columnTransformerAnnotations = getAllColumnTransformerAnnotations();
|
||||
String[] readWrite = createCustomReadWrite( columnTransformerAnnotations );
|
||||
this.customReadFragment = readWrite[0];
|
||||
|
@ -27,7 +27,7 @@
|
||||
* @author Strong Liu
|
||||
*/
|
||||
public class FormulaValue {
|
||||
private String tableName;
|
||||
private final String tableName;
|
||||
private final String expression;
|
||||
|
||||
public FormulaValue(String tableName, String expression) {
|
||||
|
@ -31,6 +31,8 @@
|
||||
import org.jboss.jandex.AnnotationInstance;
|
||||
import org.jboss.jandex.DotName;
|
||||
|
||||
import org.hibernate.AnnotationException;
|
||||
import org.hibernate.internal.util.StringHelper;
|
||||
import org.hibernate.mapping.PropertyGeneration;
|
||||
import org.hibernate.metamodel.internal.source.annotations.attribute.type.AttributeTypeResolver;
|
||||
import org.hibernate.metamodel.internal.source.annotations.entity.EntityBindingContext;
|
||||
@ -77,6 +79,12 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
|
||||
*/
|
||||
private List<Column> columnValues = new ArrayList<Column>();
|
||||
|
||||
/**
|
||||
* Is this a formula property ( annotated w/ {@code Formula}).
|
||||
*/
|
||||
private final FormulaValue formulaValue;
|
||||
|
||||
|
||||
/**
|
||||
* Is this property an id property (or part thereof).
|
||||
*/
|
||||
@ -132,6 +140,8 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
|
||||
this.naturalIdMutability = checkNaturalId();
|
||||
this.role = context.getOrigin().getName() + "#" + name;
|
||||
checkColumnAnnotations( annotations );
|
||||
this.formulaValue = checkFormulaValueAnnotation();
|
||||
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@ -186,6 +196,10 @@ public String getCheckCondition() {
|
||||
return checkCondition;
|
||||
}
|
||||
|
||||
public FormulaValue getFormulaValue() {
|
||||
return formulaValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(MappedAttribute mappedProperty) {
|
||||
return name.compareTo( mappedProperty.getName() );
|
||||
@ -238,6 +252,28 @@ private SingularAttributeBinding.NaturalIdMutability checkNaturalId() {
|
||||
return mutable ? SingularAttributeBinding.NaturalIdMutability.MUTABLE : SingularAttributeBinding.NaturalIdMutability.IMMUTABLE;
|
||||
}
|
||||
|
||||
private FormulaValue checkFormulaValueAnnotation() {
|
||||
AnnotationInstance formulaAnnotation = JandexHelper.getSingleAnnotation(
|
||||
annotations(),
|
||||
HibernateDotNames.FORMULA
|
||||
);
|
||||
if ( formulaAnnotation != null ) {
|
||||
if ( !getColumnValues().isEmpty() ) {
|
||||
throw new AnnotationException( "Can't having both @Formula and @Column on same attribute : " + getRole() );
|
||||
}
|
||||
final String expression = JandexHelper.getValue( formulaAnnotation, "value", String.class );
|
||||
if ( StringHelper.isEmpty( expression ) ) {
|
||||
throw new AnnotationException(
|
||||
String.format(
|
||||
"Formula expression defined on %s can't be empty string",
|
||||
getRole()
|
||||
)
|
||||
);
|
||||
}
|
||||
return new FormulaValue( null, expression );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private void checkColumnAnnotations(Map<DotName, List<AnnotationInstance>> annotations) {
|
||||
// single @Column
|
||||
AnnotationInstance columnAnnotation = JandexHelper.getSingleAnnotation(
|
||||
|
Loading…
x
Reference in New Issue
Block a user