Skip to content

Commit b2290a5

Browse files
committed
Added parent class support for models.
1 parent e316723 commit b2290a5

3 files changed

Lines changed: 66 additions & 3 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<properties>
88
<maven.compiler.source>8</maven.compiler.source>
99
<maven.compiler.target>8</maven.compiler.target>
10-
<buildVersion>1.0.2-SNAPSHOT</buildVersion>
10+
<buildVersion>1.0.3-SNAPSHOT</buildVersion>
1111
</properties>
1212

1313
<groupId>org.javawebstack</groupId>

src/main/java/org/javawebstack/orm/TableInfo.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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,18 @@ public class TableInfo {
4242
public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfigurationException {
4343
this.config = config;
4444
this.modelClass = model;
45+
if (model.getSuperclass() != Model.class) {
46+
Class<? extends Model> superModel = (Class<? extends Model>) model.getSuperclass();
47+
if (Modifier.isAbstract(superModel.getModifiers())) {
48+
constructInfo(superModel);
49+
} else {
50+
throw new ORMConfigurationException("The parent model has to be abstract!");
51+
}
52+
}
53+
constructInfo(model);
54+
}
55+
56+
private void constructInfo (Class<? extends Model> model) throws ORMConfigurationException {
4557
if (model.isAnnotationPresent(Table.class)) {
4658
Table table = model.getDeclaredAnnotationsByType(Table.class)[0];
4759
tableName = table.value();
@@ -82,7 +94,7 @@ public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfi
8294
else
8395
fieldSize = fieldConfig.size();
8496

85-
SQLType sqlType = config.getType(field.getType(), fieldSize);
97+
SQLType sqlType = config.getType(field.getType(), fieldSize);
8698
if (sqlType != null) {
8799
sqlTypes.put(fieldName, sqlType);
88100
sqlTypeParameters.put(fieldName, config.getTypeParameters(field.getType(), fieldSize));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.javawebstack.orm.test;
2+
3+
import org.javawebstack.orm.Model;
4+
import org.javawebstack.orm.ORM;
5+
import org.javawebstack.orm.ORMConfig;
6+
import org.javawebstack.orm.Repo;
7+
import org.javawebstack.orm.annotation.Column;
8+
import org.javawebstack.orm.exception.ORMConfigurationException;
9+
import org.javawebstack.orm.util.KeyType;
10+
import org.junit.jupiter.api.Test;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
public class TableInfoTest extends ORMTestCase {
15+
@Test
16+
public void testParentTableInfo () throws ORMConfigurationException {
17+
ORMConfig config = new ORMConfig()
18+
.setDefaultSize(255);
19+
ORM.register(Child.class, sql(), config);
20+
21+
assertEquals(Repo.get(Child.class).getInfo().getFields().size(), 2);
22+
}
23+
24+
@Test
25+
public void testParentTableMigration () throws ORMConfigurationException {
26+
ORMConfig config = new ORMConfig()
27+
.setDefaultSize(255);
28+
ORM.register(Child.class, sql(), config);
29+
ORM.autoMigrate(true);
30+
31+
Child child = new Child();
32+
child.id = 1337;
33+
child.content = "Hello World!";
34+
child.save();
35+
36+
Child fetchedChild = Repo.get(Child.class).get(1337);
37+
assertEquals(fetchedChild.id, child.id);
38+
assertEquals(fetchedChild.content, child.content);
39+
}
40+
41+
42+
public static abstract class Parent extends Model {
43+
@Column(ai = true, key = KeyType.PRIMARY)
44+
public int id;
45+
}
46+
47+
public static class Child extends Parent {
48+
@Column
49+
public String content;
50+
}
51+
}

0 commit comments

Comments
 (0)