Skip to content

Commit 9857f41

Browse files
committed
CLOUDSTACK-9298: Add @MappedSuperClass support for persistence inheritance
1 parent db54b26 commit 9857f41

5 files changed

Lines changed: 27 additions & 27 deletions

File tree

framework/db/src/com/cloud/utils/db/SqlGenerator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
import javax.persistence.EmbeddedId;
3939
import javax.persistence.Entity;
4040
import javax.persistence.FetchType;
41+
import javax.persistence.MappedSuperclass;
4142
import javax.persistence.PrimaryKeyJoinColumn;
4243
import javax.persistence.SecondaryTable;
4344
import javax.persistence.TableGenerator;
4445

46+
import org.apache.commons.lang.ArrayUtils;
4547
import com.cloud.utils.Pair;
4648
import com.cloud.utils.Ternary;
4749
import 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

server/src/com/cloud/api/query/vo/BaseViewWithTagInformationVO.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@
1616
// under the License.
1717
package com.cloud.api.query.vo;
1818

19+
import java.io.Serializable;
1920
import javax.persistence.Column;
2021
import javax.persistence.EnumType;
2122
import javax.persistence.Enumerated;
23+
import javax.persistence.Id;
24+
import javax.persistence.MappedSuperclass;
2225

2326
import com.cloud.server.ResourceTag.ResourceObjectType;
2427

25-
public abstract class BaseViewWithTagInformationVO extends BaseViewVO {
28+
@MappedSuperclass
29+
public abstract class BaseViewWithTagInformationVO extends BaseViewVO implements Serializable {
30+
31+
@Id
32+
@Column(name = "id")
33+
private long id;
2634

2735
@Column(name = "tag_id")
2836
private long tagId;
@@ -168,4 +176,8 @@ public void setTagDomainName(String tagDomainName) {
168176
this.tagDomainName = tagDomainName;
169177
}
170178

179+
public long getId() {
180+
return id;
181+
}
182+
171183
}

server/src/com/cloud/api/query/vo/TemplateJoinVO.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import javax.persistence.Entity;
2323
import javax.persistence.EnumType;
2424
import javax.persistence.Enumerated;
25-
import javax.persistence.Id;
2625
import javax.persistence.Table;
2726
import javax.persistence.Temporal;
2827
import javax.persistence.TemporalType;
@@ -41,10 +40,6 @@
4140
@Table(name = "template_view")
4241
public class TemplateJoinVO extends BaseViewWithTagInformationVO implements ControlledViewEntity {
4342

44-
@Id
45-
@Column(name = "id")
46-
private long id;
47-
4843
@Column(name = "uuid")
4944
private String uuid;
5045

@@ -227,11 +222,6 @@ public class TemplateJoinVO extends BaseViewWithTagInformationVO implements Cont
227222
public TemplateJoinVO() {
228223
}
229224

230-
@Override
231-
public long getId() {
232-
return id;
233-
}
234-
235225
@Override
236226
public String getUuid() {
237227
return uuid;

server/src/com/cloud/api/query/vo/UserVmJoinVO.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Date;
2121
import java.util.Map;
2222

23+
import javax.persistence.AttributeOverride;
2324
import javax.persistence.Column;
2425
import javax.persistence.Entity;
2526
import javax.persistence.EnumType;
@@ -39,6 +40,7 @@
3940

4041
@Entity
4142
@Table(name = "user_vm_view")
43+
@AttributeOverride( name="id", column = @Column(name = "id", updatable = false, nullable = false) )
4244
public class UserVmJoinVO extends BaseViewWithTagInformationVO implements ControlledViewEntity {
4345

4446
@Id
@@ -372,11 +374,6 @@ public class UserVmJoinVO extends BaseViewWithTagInformationVO implements Contro
372374
public UserVmJoinVO() {
373375
}
374376

375-
@Override
376-
public long getId() {
377-
return id;
378-
}
379-
380377
@Override
381378
public String getUuid() {
382379
return uuid;
@@ -793,7 +790,7 @@ public int getJobStatus() {
793790
@Override
794791
public String toString() {
795792
if (toString == null) {
796-
toString = new StringBuilder("VM[").append(id).append("|").append(name).append("]").toString();
793+
toString = new StringBuilder("VM[").append(getId()).append("|").append(name).append("]").toString();
797794
}
798795
return toString;
799796
}

server/src/com/cloud/api/query/vo/VolumeJoinVO.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import javax.persistence.Entity;
2323
import javax.persistence.EnumType;
2424
import javax.persistence.Enumerated;
25-
import javax.persistence.Id;
2625
import javax.persistence.Table;
2726
import javax.persistence.Temporal;
2827
import javax.persistence.TemporalType;
@@ -38,10 +37,6 @@
3837
@Table(name = "volume_view")
3938
public class VolumeJoinVO extends BaseViewWithTagInformationVO implements ControlledViewEntity {
4039

41-
@Id
42-
@Column(name = "id")
43-
private long id;
44-
4540
@Column(name = "uuid")
4641
private String uuid;
4742

@@ -263,11 +258,6 @@ public class VolumeJoinVO extends BaseViewWithTagInformationVO implements Contro
263258
public VolumeJoinVO() {
264259
}
265260

266-
@Override
267-
public long getId() {
268-
return id;
269-
}
270-
271261
@Override
272262
public String getUuid() {
273263
return uuid;

0 commit comments

Comments
 (0)