Skip to content

Commit

Permalink
#74 Implement ManyToOneAttributeProcessing
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Nov 17, 2023
1 parent 858e13c commit 8341702
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
package org.hibernate.models.orm.categorize.xml.internal.attr;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
Expand All @@ -17,6 +19,7 @@
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.OptimisticLock;
import org.hibernate.boot.internal.Target;
import org.hibernate.boot.jaxb.mapping.spi.JaxbCascadeTypeImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbJoinColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbManyToOneImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbSingularFetchModeImpl;
Expand Down Expand Up @@ -68,6 +71,7 @@ public static MutableMemberDetails processManyToOneAttribute(
applyFetching( memberDetails, jaxbManyToOne, manyToOneAnn, sourceModelBuildingContext );
applyOptimisticLock( memberDetails, jaxbManyToOne, manyToOneAnn, sourceModelBuildingContext );
applyTarget( memberDetails, jaxbManyToOne, manyToOneAnn, sourceModelBuildingContext );
applyCascading( memberDetails, jaxbManyToOne, manyToOneAnn, sourceModelBuildingContext );

return memberDetails;
}
Expand Down Expand Up @@ -117,6 +121,7 @@ private static void applyJoinColumns(
}
}

@SuppressWarnings("unused")
private static void applyNotFound(
MutableMemberDetails memberDetails,
JaxbManyToOneImpl jaxbManyToOne,
Expand All @@ -131,6 +136,7 @@ private static void applyNotFound(
notFoundAnn.setAttributeValue( "action", notFoundAction );
}

@SuppressWarnings("unused")
private static void applyOnDelete(
MutableMemberDetails memberDetails,
JaxbManyToOneImpl jaxbManyToOne,
Expand All @@ -145,6 +151,7 @@ private static void applyOnDelete(
notFoundAnn.setAttributeValue( "action", action );
}

@SuppressWarnings("unused")
private static void applyFetching(
MutableMemberDetails memberDetails,
JaxbManyToOneImpl jaxbManyToOne,
Expand All @@ -160,6 +167,7 @@ private static void applyFetching(
fetchAnn.setAttributeValue( "value", fetchMode );
}

@SuppressWarnings("unused")
private static void applyOptimisticLock(
MutableMemberDetails memberDetails,
JaxbManyToOneImpl jaxbManyToOne,
Expand All @@ -170,6 +178,7 @@ private static void applyOptimisticLock(
optLockAnn.setAttributeValue( "excluded", !includeInOptimisticLock );
}

@SuppressWarnings("unused")
private static void applyTarget(
MutableMemberDetails memberDetails,
JaxbManyToOneImpl jaxbManyToOne,
Expand All @@ -183,4 +192,55 @@ private static void applyTarget(
final MutableAnnotationUsage<Target> targetAnn = makeAnnotation( Target.class, memberDetails );
targetAnn.setAttributeValue( "value", targetEntityName );
}

@SuppressWarnings("unused")
private static void applyCascading(
MutableMemberDetails memberDetails,
JaxbManyToOneImpl jaxbManyToOne,
MutableAnnotationUsage<ManyToOne> manyToOneAnn,
SourceModelBuildingContext sourceModelBuildingContext) {
final JaxbCascadeTypeImpl cascadeContainer = jaxbManyToOne.getCascade();
if ( cascadeContainer == null ) {
return;
}

final EnumSet<CascadeType> cascadeTypes;

if ( cascadeContainer.getCascadeAll() != null ) {
cascadeTypes = EnumSet.allOf( CascadeType.class );
}
else {
cascadeTypes = EnumSet.noneOf( CascadeType.class );
if ( cascadeContainer.getCascadePersist() != null ) {
cascadeTypes.add( CascadeType.PERSIST );
}
if ( cascadeContainer.getCascadeMerge() != null ) {
cascadeTypes.add( CascadeType.MERGE );
}
if ( cascadeContainer.getCascadeRemove() != null ) {
cascadeTypes.add( CascadeType.REMOVE );
}
if ( cascadeContainer.getCascadeLock() != null ) {
cascadeTypes.add( CascadeType.LOCK );
}
if ( cascadeContainer.getCascadeRefresh() != null ) {
cascadeTypes.add( CascadeType.REFRESH );
}
if ( cascadeContainer.getCascadeReplicate() != null ) {
//noinspection deprecation
cascadeTypes.add( CascadeType.REPLICATE );
}
if ( cascadeContainer.getCascadeDetach() != null ) {
cascadeTypes.add( CascadeType.DETACH );
}
}

manyToOneAnn.setAttributeValue( "cascade", asList( cascadeTypes ) );
}

private static <E extends Enum<E>> List<E> asList(EnumSet<E> enums) {
final List<E> list = CollectionHelper.arrayList( enums.size() );
list.addAll( enums );
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
*/
package org.hibernate.models.orm.xml.attr;

import java.util.List;

import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.OptimisticLock;
import org.hibernate.annotations.OptimisticLocking;
import org.hibernate.boot.internal.BootstrapContextImpl;
import org.hibernate.boot.internal.MetadataBuilderImpl;
import org.hibernate.boot.internal.Target;
Expand Down Expand Up @@ -45,6 +47,7 @@
@ServiceRegistry
public class ManyToOneTests {
@Test
@SuppressWarnings("JUnitMalformedDeclaration")
void testSimpleManyToOne(ServiceRegistryScope scope) {
final StandardServiceRegistry serviceRegistry = scope.getRegistry();
final ManagedResources managedResources = new ManagedResourcesImpl.Builder()
Expand Down Expand Up @@ -93,8 +96,13 @@ void testSimpleManyToOne(ServiceRegistryScope scope) {
final AnnotationUsage<Target> targetAnn = parentField.getAnnotationUsage( Target.class );
assertThat( targetAnn ).isNotNull();
assertThat( targetAnn.getString( "value" ) ).isEqualTo( "org.hibernate.models.orm.xml.attr.ManyToOneTests$SimpleEntity" );

final List<CascadeType> cascadeTypes = manyToOneAnn.getList( "cascade" );
assertThat( cascadeTypes ).isNotEmpty();
assertThat( cascadeTypes ).contains( CascadeType.PERSIST, CascadeType.MERGE, CascadeType.LOCK );
}

@SuppressWarnings("unused")
@Entity(name="SimpleEntity")
@Table(name="SimpleEntity")
public static class SimpleEntity {
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/mappings/attr/many-to-one/simple.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
optimistic-lock="false"
not-found="IGNORE">
<join-column name="parent_fk" insertable="false" updatable="false"/>
<cascade>
<cascade-all/>
</cascade>
<on-delete>CASCADE</on-delete>
</many-to-one>
</attributes>
Expand Down

0 comments on commit 8341702

Please sign in to comment.