Skip to content

Commit 2e05d2a

Browse files
tim-aerotfaulkes
andauthored
Allowed classes to have no attributes (#138)
Allowed superclasses to have no attributes An error message was thrown if a class had no immediate or superclass attributes to be saved to the database. This is a valid check on a low level class, but it's entirely fair to have a superclass (typically abstract) which has no attributes, and this error was being thrown here too. The check is safe to remove as it's illegal to save a class with no attributes to the database because there will be no @id field so will throw an exception anyway. Co-authored-by: Tim Faulkes <[email protected]>
1 parent 786b5af commit 2e05d2a

File tree

2 files changed

+129
-4
lines changed

2 files changed

+129
-4
lines changed

src/main/java/com/aerospike/mapper/tools/ClassCacheEntry.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,6 @@ public ClassCacheEntry<T> construct() {
146146
this.loadPropertiesFromClass();
147147
this.superClazz = ClassCache.getInstance().loadClass(this.clazz.getSuperclass(), this.mapper, !this.mapAll);
148148
this.binCount = this.values.size() + (superClazz != null ? superClazz.binCount : 0);
149-
if (this.binCount == 0) {
150-
throw new AerospikeException(String.format("Class %s has no values defined to be stored in the database",
151-
clazz.getSimpleName()));
152-
}
153149
this.formOrdinalsFromValues();
154150
Method factoryConstructorMethod = findConstructorFactoryMethod();
155151
if (factoryConstructorMethod == null) {
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.aerospike.mapper;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import com.aerospike.mapper.annotations.AerospikeBin;
11+
import com.aerospike.mapper.annotations.AerospikeEmbed;
12+
import com.aerospike.mapper.annotations.AerospikeEmbed.EmbedType;
13+
import com.aerospike.mapper.annotations.AerospikeKey;
14+
import com.aerospike.mapper.annotations.AerospikeRecord;
15+
import com.aerospike.mapper.tools.AeroMapper;
16+
17+
import lombok.AllArgsConstructor;
18+
import lombok.Data;
19+
import lombok.NoArgsConstructor;
20+
21+
public class BaseClassWithNoAttributesTest extends AeroMapperBaseTest {
22+
23+
public static class Animal {
24+
}
25+
26+
@Data
27+
@AllArgsConstructor
28+
@AerospikeRecord(namespace = "test", set = "zoo")
29+
public static class Dog extends Animal{
30+
31+
@AerospikeKey
32+
private String dogId;
33+
private String breed;
34+
35+
public Dog(){
36+
dogId = "" + ((int) (Math.random() * 1000));
37+
}
38+
39+
public Dog(String breed) {
40+
this.breed = breed;
41+
dogId = "" + ((int) (Math.random() * 1000));
42+
}
43+
44+
public String getBreed() {
45+
return breed;
46+
}
47+
48+
public String getDogId() {
49+
return dogId;
50+
}
51+
}
52+
53+
@Data
54+
@AllArgsConstructor
55+
@AerospikeRecord(namespace = "test", set = "zoo")
56+
public static class Cat extends Animal {
57+
58+
@AerospikeKey
59+
private String catId;
60+
private String breed;
61+
private String lifeSpan;
62+
63+
public Cat(String breed, String lifeSpan) {
64+
this.breed = breed;
65+
this.lifeSpan = lifeSpan;
66+
67+
catId = "" + ((int) (Math.random() * 1000));
68+
}
69+
70+
public Cat() {
71+
catId = "" + ((int) (Math.random() * 1000));
72+
}
73+
74+
public String getBreed() {
75+
return breed;
76+
}
77+
78+
public String getCatId() {
79+
return catId;
80+
}
81+
82+
public String getLifeSpan() {
83+
return lifeSpan;
84+
}
85+
}
86+
87+
@AerospikeRecord(namespace = "test", set = "zoo", mapAll = false)
88+
@NoArgsConstructor
89+
@Data
90+
public static class Zoo {
91+
92+
@AerospikeBin
93+
@AerospikeKey
94+
private String zooId;
95+
96+
@AerospikeBin
97+
@AerospikeEmbed(type = EmbedType.LIST, elementType = EmbedType.MAP)
98+
private List<Animal> animalsList;
99+
100+
public Zoo(String zooId, List<Animal> animalsList) {
101+
super();
102+
this.zooId = zooId;
103+
this.animalsList = animalsList;
104+
}
105+
}
106+
107+
108+
@Test
109+
public void runTest() {
110+
AeroMapper mapper = new AeroMapper.Builder(client).build();
111+
112+
Zoo zoo = new Zoo();
113+
zoo.setZooId("103");
114+
115+
List<Animal> listOfAnimals = new ArrayList<>();
116+
listOfAnimals.add(new Cat("Persian", "30"));
117+
listOfAnimals.add(new Cat("Indian", "21"));
118+
listOfAnimals.add(new Dog("Lab"));
119+
listOfAnimals.add(new Cat("Korean", "15"));
120+
listOfAnimals.add(new Dog("Desi"));
121+
122+
zoo.setAnimalsList(listOfAnimals);
123+
mapper.save(zoo);
124+
125+
Zoo readZoo = mapper.read(Zoo.class, "103");
126+
assertEquals(zoo.zooId, readZoo.zooId);
127+
assertEquals(zoo, readZoo);
128+
}
129+
}

0 commit comments

Comments
 (0)