3838import javax .persistence .EmbeddedId ;
3939import javax .persistence .Entity ;
4040import javax .persistence .FetchType ;
41+ import javax .persistence .MappedSuperclass ;
4142import javax .persistence .PrimaryKeyJoinColumn ;
4243import javax .persistence .SecondaryTable ;
4344import javax .persistence .TableGenerator ;
4445
46+ import org .apache .commons .lang .ArrayUtils ;
4547import com .cloud .utils .Pair ;
4648import com .cloud .utils .Ternary ;
4749import com .cloud .utils .db .Attribute .Flag ;
@@ -54,6 +56,7 @@ public class SqlGenerator {
5456 LinkedHashMap <String , List <Attribute >> _ids ;
5557 HashMap <String , TableGenerator > _generators ;
5658 ArrayList <Attribute > _ecAttrs ;
59+ Field [] _mappedSuperclassFields ;
5760
5861 public SqlGenerator (Class <?> clazz ) {
5962 _clazz = clazz ;
@@ -91,6 +94,12 @@ protected boolean checkMethods(Class<?> clazz, Map<String, Attribute> attrs) {
9194
9295 protected void buildAttributes (Class <?> clazz , String tableName , AttributeOverride [] overrides , boolean embedded , boolean isId ) {
9396 if (!embedded && clazz .getAnnotation (Entity .class ) == null ) {
97+ // A class designated with the MappedSuperclass annotation can be mapped in the same way as an entity
98+ // except that the mappings will apply only to its subclasses since no table exists for the mapped superclass itself
99+ if (clazz .getAnnotation (MappedSuperclass .class ) != null ){
100+ Field [] declaredFields = clazz .getDeclaredFields ();
101+ _mappedSuperclassFields = (Field []) ArrayUtils .addAll (_mappedSuperclassFields , declaredFields );
102+ }
94103 return ;
95104 }
96105
@@ -105,6 +114,8 @@ protected void buildAttributes(Class<?> clazz, String tableName, AttributeOverri
105114 }
106115
107116 Field [] fields = clazz .getDeclaredFields ();
117+ fields = (Field []) ArrayUtils .addAll (fields , _mappedSuperclassFields );
118+ _mappedSuperclassFields = null ;
108119 for (Field field : fields ) {
109120 field .setAccessible (true );
110121
0 commit comments