11package org .hibernate .tool .orm .jbt .internal .factory ;
22
3+ import java .lang .reflect .Field ;
34import java .util .ArrayList ;
45import java .util .Iterator ;
56import java .util .List ;
67
78import org .hibernate .AssertionFailure ;
9+ import org .hibernate .boot .spi .MetadataBuildingContext ;
810import org .hibernate .engine .OptimisticLockStyle ;
9- import org .hibernate .mapping .Join ;
10- import org .hibernate .mapping .JoinedSubclass ;
11- import org .hibernate .mapping .KeyValue ;
12- import org .hibernate .mapping .PersistentClass ;
13- import org .hibernate .mapping .Property ;
14- import org .hibernate .mapping .RootClass ;
15- import org .hibernate .mapping .SingleTableSubclass ;
16- import org .hibernate .mapping .Subclass ;
17- import org .hibernate .mapping .Table ;
18- import org .hibernate .mapping .Value ;
19- import org .hibernate .tool .orm .jbt .api .wrp .JoinWrapper ;
20- import org .hibernate .tool .orm .jbt .api .wrp .PersistentClassWrapper ;
21- import org .hibernate .tool .orm .jbt .api .wrp .PropertyWrapper ;
22- import org .hibernate .tool .orm .jbt .api .wrp .TableWrapper ;
23- import org .hibernate .tool .orm .jbt .api .wrp .ValueWrapper ;
11+ import org .hibernate .mapping .*;
12+ import org .hibernate .tool .orm .jbt .api .wrp .*;
2413import org .hibernate .tool .orm .jbt .internal .util .DummyMetadataBuildingContext ;
2514import org .hibernate .tool .orm .jbt .internal .util .SpecialRootClass ;
2615import org .hibernate .tool .orm .jbt .internal .wrp .AbstractWrapper ;
@@ -43,15 +32,84 @@ public static Object createJoinedTableSubClassWrapper(PersistentClassWrapper per
4332 return createPersistentClassWrapper (js );
4433 }
4534
46- public static Object createSpecialRootClassWrapper (PropertyWrapper propertyWrapper ) {
47- Property p = (Property )propertyWrapper .getWrappedObject ();
48- SpecialRootClass src = new SpecialRootClass (p );
49- return createPersistentClassWrapper (src );
35+ public static PersistentClassWrapper createSpecialRootClassWrapper (PropertyWrapper propertyWrapper ) {
36+ return new SpecialRootClassWrapperImpl (propertyWrapper );
5037 }
5138
5239 public static PersistentClassWrapper createPersistentClassWrapper (PersistentClass wrappedPersistentClass ) {
5340 return new PersistentClassWrapperImpl (wrappedPersistentClass );
5441 }
42+
43+ private static class SpecialRootClassWrapperImpl extends PersistentClassWrapperImpl {
44+
45+ private final PropertyWrapper propertyWrapper ;
46+ private PropertyWrapper parentPropertyWrapper ;
47+
48+ private SpecialRootClassWrapperImpl (PropertyWrapper propertyWrapper ) {
49+ super (new RootClass (getMetadataBuildingContext ((Property )propertyWrapper .getWrappedObject ())));
50+ this .propertyWrapper = propertyWrapper ;
51+ initialize ();
52+ }
53+
54+ @ Override
55+ public PropertyWrapper getProperty () {
56+ return propertyWrapper ;
57+ }
58+
59+ @ Override
60+ public PropertyWrapper getParentProperty () {
61+ return parentPropertyWrapper ;
62+ }
63+
64+ @ Override
65+ public boolean isInstanceOfSpecialRootClass () {
66+ return true ;
67+ }
68+
69+ @ Override
70+ public boolean isRootClass () {
71+ return false ;
72+ }
73+
74+ private void initialize () {
75+ Component component = getComponent ();
76+ if (component != null ) {
77+ setClassName (component .getComponentClassName ());
78+ setEntityName (component .getComponentClassName ());
79+ PersistentClass ownerClass = component .getOwner ();
80+ if (component .getParentProperty () != null ) {
81+ Property parentProperty = new Property ();
82+ parentProperty .setName (component .getParentProperty ());
83+ parentProperty .setPersistentClass (ownerClass );
84+ parentPropertyWrapper = PropertyWrapperFactory .createPropertyWrapper (parentProperty );
85+ }
86+ for (Property property : component .getProperties ()) {
87+ if (property != null ) {
88+ addProperty (PropertyWrapperFactory .createPropertyWrapper (property ));
89+ }
90+ }
91+ }
92+ }
93+
94+ private Component getComponent () {
95+ Component result = null ;
96+ if (propertyWrapper != null ) {
97+ Value v = ((Property )propertyWrapper .getWrappedObject ()).getValue ();
98+ if (v != null ) {
99+ if (v instanceof Wrapper ) {
100+ v = (Value )((Wrapper )v ).getWrappedObject ();
101+ }
102+ if (Collection .class .isAssignableFrom (v .getClass ())) {
103+ v = ((Collection )v ).getElement ();
104+ }
105+ if (v != null && Component .class .isAssignableFrom (v .getClass ())) {
106+ result = (Component )v ;
107+ }
108+ }
109+ }
110+ return result ;
111+ }
112+ }
55113
56114 private static class PersistentClassWrapperImpl
57115 extends AbstractWrapper
@@ -122,8 +180,8 @@ public void setIdentifier(ValueWrapper value) {
122180 if (!isInstanceOfRootClass ()) {
123181 throw new RuntimeException ("Method 'setIdentifier(Value)' can only be called on RootClass instances" );
124182 } else {
183+ ((RootClass )persistentClass ).setIdentifier (value == null ? null : (KeyValue )value .getWrappedObject ());
125184 }
126- ((RootClass )persistentClass ).setIdentifier (value == null ? null : (KeyValue )value .getWrappedObject ());
127185 }
128186
129187 @ Override
@@ -136,7 +194,7 @@ public void setKey(ValueWrapper value) {
136194
137195 @ Override
138196 public boolean isInstanceOfSpecialRootClass () {
139- return SpecialRootClass . class . isAssignableFrom ( persistentClass . getClass ());
197+ return false ;
140198 }
141199
142200 @ Override
@@ -456,17 +514,33 @@ public List<PropertyWrapper> getPropertyClosure() {
456514 }
457515
458516 private static int getOldCode (OptimisticLockStyle ols ) {
459- switch (ols ) {
460- case NONE :
461- return -1 ;
462- case VERSION :
463- return 0 ;
464- case DIRTY :
465- return 1 ;
466- case ALL :
467- return 2 ;
468- default :
469- throw new AssertionFailure ("Unknown OptimisticLockStyle" );
517+ return switch (ols ) {
518+ case NONE -> -1 ;
519+ case VERSION -> 0 ;
520+ case DIRTY -> 1 ;
521+ case ALL -> 2 ;
522+ default -> throw new AssertionFailure ("Unknown OptimisticLockStyle" );
523+ };
524+ }
525+
526+ private static MetadataBuildingContext getMetadataBuildingContext (Property property ) {
527+ MetadataBuildingContext result = DummyMetadataBuildingContext .INSTANCE ;
528+ try {
529+ if (property != null ) {
530+ PersistentClass pc = property .getPersistentClass ();
531+ if (pc != null ) {
532+ Field field = PersistentClass .class .getDeclaredField ("metadataBuildingContext" );
533+ field .setAccessible (true );
534+ result = (MetadataBuildingContext )field .get (pc );
535+ }
536+ }
537+ } catch (NoSuchFieldException |
538+ SecurityException |
539+ IllegalArgumentException |
540+ IllegalAccessException e ) {
541+ throw new RuntimeException ("Problem while trying to retrieve MetadataBuildingContext from field" , e );
470542 }
543+ return result ;
471544 }
545+
472546}
0 commit comments