@@ -29,7 +29,7 @@ public class TableInfo {
2929 private final Class <? extends Model > modelClass ;
3030 private String primaryKey ;
3131 private final List <String > uniqueKeys = new ArrayList <>();
32- private final Constructor <?> constructor ;
32+ private Constructor <?> constructor ;
3333 private String relationField ;
3434 private final Map <String , String > filterable = new HashMap <>();
3535 private final List <String > searchable = new ArrayList <>();
@@ -42,6 +42,36 @@ public class TableInfo {
4242 public TableInfo (Class <? extends Model > model , ORMConfig config ) throws ORMConfigurationException {
4343 this .config = config ;
4444 this .modelClass = model ;
45+
46+ Stack <Class <?>> superClasses = Helper .getSuperClassesTill (model , Model .class );
47+ while (!superClasses .isEmpty ()) {
48+ Class <? extends Model > superClass = (Class <? extends Model >) superClasses .pop ();
49+ if (Modifier .isAbstract (superClass .getModifiers ())) {
50+ analyzeColumns (superClass );
51+ } else {
52+ throw new ORMConfigurationException ("The parent model has to be abstract!" );
53+ }
54+ }
55+
56+ constructInfo (model );
57+ }
58+
59+ private void constructInfo (Class <? extends Model > model ) throws ORMConfigurationException {
60+ analyzeColumns (model );
61+ analyzeTable (model );
62+
63+ if (!fields .containsKey (idField ))
64+ idField = "uuid" ;
65+ if (!fields .containsKey (idField ))
66+ throw new ORMConfigurationException ("No id field found!" );
67+
68+ if (config .isIdPrimaryKey ()) {
69+ if (primaryKey == null )
70+ primaryKey = idField ;
71+ }
72+ }
73+
74+ private void analyzeTable (Class <? extends Model > model ) throws ORMConfigurationException {
4575 if (model .isAnnotationPresent (Table .class )) {
4676 Table table = model .getDeclaredAnnotationsByType (Table .class )[0 ];
4777 tableName = table .value ();
@@ -59,6 +89,26 @@ public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfi
5989 } catch (NoSuchMethodException e ) {
6090 throw new ORMConfigurationException ("The model class has no empty constructor!" );
6191 }
92+ if (model .isAnnotationPresent (RelationField .class )) {
93+ relationField = model .getDeclaredAnnotationsByType (RelationField .class )[0 ].value ();
94+ } else {
95+ relationField = Helper .pascalToCamelCase (model .getSimpleName ()) + ((getIdType ().equals (UUID .class ) && !idField .equalsIgnoreCase ("id" )) ? "UUID" : "Id" );
96+ }
97+ if (model .isAnnotationPresent (SoftDelete .class )) {
98+ softDelete = model .getDeclaredAnnotationsByType (SoftDelete .class )[0 ];
99+ if (!fields .containsKey (softDelete .value ()))
100+ throw new ORMConfigurationException ("Missing soft-delete field '" + softDelete .value () + "'" );
101+ }
102+ if (model .isAnnotationPresent (Dates .class )) {
103+ dates = model .getDeclaredAnnotationsByType (Dates .class )[0 ];
104+ if (!fields .containsKey (dates .create ()))
105+ throw new ORMConfigurationException ("Missing dates field '" + dates .create () + "'" );
106+ if (!fields .containsKey (dates .update ()))
107+ throw new ORMConfigurationException ("Missing dates field '" + dates .update () + "'" );
108+ }
109+ }
110+
111+ private void analyzeColumns (Class <? extends Model > model ) throws ORMConfigurationException {
62112 for (Field field : model .getDeclaredFields ()) {
63113 if (Modifier .isStatic (field .getModifiers ()))
64114 continue ;
@@ -82,7 +132,7 @@ public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfi
82132 else
83133 fieldSize = fieldConfig .size ();
84134
85- SQLType sqlType = config .getType (field .getType (), fieldSize );
135+ SQLType sqlType = config .getType (field .getType (), fieldSize );
86136 if (sqlType != null ) {
87137 sqlTypes .put (fieldName , sqlType );
88138 sqlTypeParameters .put (fieldName , config .getTypeParameters (field .getType (), fieldSize ));
@@ -106,31 +156,6 @@ public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfi
106156 if (field .isAnnotationPresent (Searchable .class ))
107157 this .searchable .add (fieldName );
108158 }
109- if (!fields .containsKey (idField ))
110- idField = "uuid" ;
111- if (!fields .containsKey (idField ))
112- throw new ORMConfigurationException ("No id field found!" );
113- if (model .isAnnotationPresent (RelationField .class )) {
114- relationField = model .getDeclaredAnnotationsByType (RelationField .class )[0 ].value ();
115- } else {
116- relationField = Helper .pascalToCamelCase (model .getSimpleName ()) + ((getIdType ().equals (UUID .class ) && !idField .equalsIgnoreCase ("id" )) ? "UUID" : "Id" );
117- }
118- if (config .isIdPrimaryKey ()) {
119- if (primaryKey == null )
120- primaryKey = idField ;
121- }
122- if (model .isAnnotationPresent (SoftDelete .class )) {
123- softDelete = model .getDeclaredAnnotationsByType (SoftDelete .class )[0 ];
124- if (!fields .containsKey (softDelete .value ()))
125- throw new ORMConfigurationException ("Missing soft-delete field '" + softDelete .value () + "'" );
126- }
127- if (model .isAnnotationPresent (Dates .class )) {
128- dates = model .getDeclaredAnnotationsByType (Dates .class )[0 ];
129- if (!fields .containsKey (dates .create ()))
130- throw new ORMConfigurationException ("Missing dates field '" + dates .create () + "'" );
131- if (!fields .containsKey (dates .update ()))
132- throw new ORMConfigurationException ("Missing dates field '" + dates .update () + "'" );
133- }
134159 }
135160
136161 public boolean isSoftDelete () {
0 commit comments