Skip to content

Commit

Permalink
sebersole#14 - Create AnnotationUsage for sql-insert, sql-update, sq…
Browse files Browse the repository at this point in the history
…l-delete and sql-delete-all
  • Loading branch information
mbladel committed Oct 31, 2023
1 parent eb1f711 commit 598a5b7
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public static CustomSql extractCustomSql(AnnotationUsage<?> customSqlAnnotation)
}

final String sql = customSqlAnnotation.getAttributeValue( "sql" );
final boolean isCallable = customSqlAnnotation.getAttributeValue( "value" );
final boolean isCallable = customSqlAnnotation.getAttributeValue( "callable" );

final ResultCheckStyle checkValue = customSqlAnnotation.getAttributeValue( "check" );
final ExecuteUpdateResultCheckStyle checkStyle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.Nationalized;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.annotations.SortComparator;
import org.hibernate.annotations.SortNatural;
import org.hibernate.boot.internal.CollectionClassification;
Expand Down Expand Up @@ -572,6 +576,11 @@ public static MutableMemberDetails processElementCollectionAttribute(

XmlAnnotationHelper.applySqlRestriction( jaxbElementCollection.getSqlRestriction(), memberDetails, sourceModelBuildingContext );

XmlAnnotationHelper.applyCustomSql( jaxbElementCollection.getSqlInsert(), memberDetails, SQLInsert.class, sourceModelBuildingContext );
XmlAnnotationHelper.applyCustomSql( jaxbElementCollection.getSqlUpdate(), memberDetails, SQLUpdate.class, sourceModelBuildingContext );
XmlAnnotationHelper.applyCustomSql( jaxbElementCollection.getSqlDelete(), memberDetails, SQLDelete.class, sourceModelBuildingContext );
XmlAnnotationHelper.applyCustomSql( jaxbElementCollection.getSqlDeleteAll(), memberDetails, SQLDeleteAll.class, sourceModelBuildingContext );

// todo : attribute-override
// todo : association-override

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import java.util.Map;
import java.util.Set;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.boot.internal.Abstract;
import org.hibernate.boot.internal.Extends;
import org.hibernate.boot.internal.LimitedCollectionClassification;
Expand Down Expand Up @@ -457,6 +460,10 @@ private static void processEntityMetadata(

XmlAnnotationHelper.applySqlRestriction( jaxbEntity.getSqlRestriction(), classDetails, sourceModelBuildingContext );

XmlAnnotationHelper.applyCustomSql( jaxbEntity.getSqlInsert(), classDetails, SQLInsert.class, sourceModelBuildingContext );
XmlAnnotationHelper.applyCustomSql( jaxbEntity.getSqlUpdate(), classDetails, SQLUpdate.class, sourceModelBuildingContext );
XmlAnnotationHelper.applyCustomSql( jaxbEntity.getSqlDelete(), classDetails, SQLDelete.class, sourceModelBuildingContext );

processEntityOrMappedSuperclass( jaxbEntity, classDetails, sourceModelBuildingContext );

XmlAnnotationHelper.applyRowId( jaxbEntity.getRowid(), classDetails, sourceModelBuildingContext );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.hibernate.annotations.NaturalIdCache;
import org.hibernate.annotations.OptimisticLock;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.ResultCheckStyle;
import org.hibernate.annotations.RowId;
import org.hibernate.annotations.SQLJoinTableRestriction;
import org.hibernate.annotations.SQLRestriction;
Expand All @@ -43,6 +44,7 @@
import org.hibernate.boot.jaxb.mapping.spi.JaxbColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbConfigurationParameterImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbConvertImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbCustomSqlImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddedIdImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntity;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListenerImpl;
Expand All @@ -60,6 +62,7 @@
import org.hibernate.boot.jaxb.mapping.spi.JaxbTableImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbUserTypeImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbUuidGeneratorImpl;
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
import org.hibernate.models.ModelsException;
import org.hibernate.models.internal.CollectionHelper;
import org.hibernate.models.internal.StringHelper;
Expand Down Expand Up @@ -888,12 +891,41 @@ private static <A extends Annotation> void applySqlRestriction(
}
}

static <A extends Annotation> void applyCustomSql(
JaxbCustomSqlImpl jaxbCustomSql,
MutableAnnotationTarget target,
Class<A> annotationType,
SourceModelBuildingContext buildingContext) {
if ( jaxbCustomSql != null ) {
final MutableAnnotationUsage<A> annotation = getOrMakeAnnotation( annotationType, target );
annotation.setAttributeValue( "sql", jaxbCustomSql.getValue() );
annotation.setAttributeValue( "callable", jaxbCustomSql.isCallable() );
applyAttributeIfSpecified( annotation, "table", jaxbCustomSql.getTable() );
if ( jaxbCustomSql.getCheck() != null ) {
annotation.setAttributeValue( "check", getResultCheckStyle( jaxbCustomSql.getCheck() ) );
}
}
}

private static ResultCheckStyle getResultCheckStyle(ExecuteUpdateResultCheckStyle style) {
switch ( style ) {
case NONE:
return ResultCheckStyle.NONE;
case COUNT:
return ResultCheckStyle.COUNT;
case PARAM:
return ResultCheckStyle.PARAM;
default:
return null;
}
}

static void applyIdClass(
JaxbIdClassImpl jaxbIdClass,
MutableClassDetails target,
SourceModelBuildingContext buildingContext) {
if ( jaxbIdClass != null ) {
XmlProcessingHelper.makeAnnotation( IdClass.class, target ).setAttributeValue(
getOrMakeAnnotation( IdClass.class, target ).setAttributeValue(
"value",
buildingContext.getClassDetailsRegistry().resolveClassDetails( jaxbIdClass.getClazz() )
);
Expand Down Expand Up @@ -933,9 +965,9 @@ static void applyLifecycleCallbacks(
applyLifecycleCallback( lifecycleCallbackContainer.getPostLoad(), PostLoad.class, classDetails );
}

private static void applyLifecycleCallback(
private static <A extends Annotation> void applyLifecycleCallback(
JaxbLifecycleCallback lifecycleCallback,
Class<? extends Annotation> lifecycleAnnotation,
Class<A> lifecycleAnnotation,
MutableClassDetails classDetails) {
if ( lifecycleCallback != null ) {
final MethodDetails method = classDetails.findMethodByName( lifecycleCallback.getMethodName() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.List;

import org.hibernate.annotations.Filter;
import org.hibernate.annotations.ResultCheckStyle;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLRestriction;
import org.hibernate.annotations.SqlFragmentAlias;
import org.hibernate.boot.internal.BootstrapContextImpl;
Expand Down Expand Up @@ -81,6 +83,8 @@ void testSimpleCompleteEntity() {
assertThat( sqlRestriction ).isNotNull();
assertThat( sqlRestriction.<String>getAttributeValue( "value" ) ).isEqualTo( "name is not null" );

validateSqlInsert( root.getClassDetails().getAnnotationUsage( SQLInsert.class ));

validateFilterUsage( root.getClassDetails().getAnnotationUsage( Filter.class ) );
}
}
Expand All @@ -97,4 +101,12 @@ private void validateFilterUsage(AnnotationUsage<Filter> filter) {
.<ClassDetails>getAttributeValue( "entity" )
.getName() ).isEqualTo( SimpleEntity.class.getName() );
}

private void validateSqlInsert(AnnotationUsage<SQLInsert> sqlInsert) {
assertThat( sqlInsert ).isNotNull();
assertThat( sqlInsert.<String>getAttributeValue( "sql" ) ).isEqualTo( "insert into SimpleEntity(name) values(?)" );
assertThat( sqlInsert.<Boolean>getAttributeValue( "callable" ) ).isTrue();
assertThat( sqlInsert.<ResultCheckStyle>getAttributeValue( "check" ) ).isEqualTo( ResultCheckStyle.COUNT );
assertThat( sqlInsert.<String>getAttributeValue( "table" ) ).isEqualTo( "SimpleEntity" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
version="3.1">
<entity class="org.hibernate.models.orm.xml.SimpleEntity" metadata-complete="true" access="FIELD">
<sql-restriction>name is not null</sql-restriction>
<sql-insert callable="true" check="rowcount" table="SimpleEntity">insert into SimpleEntity(name) values(?)</sql-insert>
<filter name="name_filter" condition="{t}.name = :name">
<aliases alias="t" table="SimpleEntity" entity="org.hibernate.models.orm.xml.SimpleEntity"/>
</filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,11 @@
</xsd:choice>
<xsd:element name="collection-table" type="orm:collection-table" minOccurs="0"/>
<xsd:element name="sql-restriction" type="xsd:string" minOccurs="0"/>
<xsd:element name="sql-insert" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="sql-update" type="orm:custom-sql" minOccurs="0"/>
<!-- todo : sql-delete is currently ignored in ORM for element collections, should we leave it? -->
<xsd:element name="sql-delete" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="sql-delete-all" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="filter" type="orm:hbm-filter" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required"/>
Expand Down Expand Up @@ -1359,12 +1364,20 @@
<xsd:sequence>
<xsd:group ref="collection-structure-group"/>

<xsd:element name="join-table" type="orm:join-table" minOccurs="0"/>
<xsd:choice>
<xsd:sequence>
<xsd:element name="join-table" type="orm:join-table" minOccurs="0"/>
<xsd:element name="sql-join-table-restriction" type="xsd:string" minOccurs="0"/>
<xsd:element name="filter-join-table" type="orm:hbm-filter" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:choice>
<xsd:element name="cascade" type="orm:cascade-type" minOccurs="0"/>
<xsd:element name="sql-restriction" type="xsd:string" minOccurs="0"/>
<xsd:element name="sql-join-table-restriction" type="xsd:string" minOccurs="0"/>
<xsd:element name="sql-insert" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="sql-update" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="sql-delete" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="sql-delete-all" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="filter" type="orm:hbm-filter" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="filter-join-table" type="orm:hbm-filter" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="target-entity" type="xsd:string"/>
Expand Down Expand Up @@ -1849,6 +1862,10 @@
<xsd:element name="cascade" type="orm:cascade-type" minOccurs="0"/>
<xsd:element name="on-delete" type="orm:on-delete-type" minOccurs="0"/>
<xsd:element name="sql-restriction" type="xsd:string" minOccurs="0"/>
<xsd:element name="sql-insert" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="sql-update" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="sql-delete" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="sql-delete-all" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="filter" type="orm:hbm-filter" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required"/>
Expand Down Expand Up @@ -2629,6 +2646,7 @@
<xsd:extension base="xsd:string">
<xsd:attribute name="callable" type="xsd:boolean" default="false" />
<xsd:attribute name="check" type="orm:custom-sql-check-type"/>
<xsd:attribute name="table" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
Expand Down Expand Up @@ -2814,6 +2832,10 @@
<xsd:element name="key" type="orm:hbm-any-key"/>
<xsd:element name="cascade" type="orm:cascade-type" minOccurs="0"/>
<xsd:element name="sql-restriction" type="xsd:string" minOccurs="0"/>
<xsd:element name="sql-insert" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="sql-update" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="sql-delete" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="sql-delete-all" type="orm:custom-sql" minOccurs="0"/>
<xsd:element name="filter" type="orm:hbm-filter" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>

Expand Down

0 comments on commit 598a5b7

Please sign in to comment.