I successfully set up a MongoDB Database and my Java program is connecting to it fine. However, when I try to save an instance of this class, I get an error:
@Data
public class NormalUser extends User implements Serializable {
private static final long serialVersionUID = 1324235L;
public NormalUser(final String name, final String surname, final String address,
final String dni, final Gender gender, final String bankAccount) {
super(name, surname, address, dni, gender, bankAccount, Role.NORMAL_USER, false);
}
}
(Please note I am using Lombok @Data annotation and that the class extends another class that uses the same annotation and only has the constructor and the attributes.)
The error I am getting is weird from my point of view, as long as it says the class can´t be serialized even it implements Serializable
:
Exception in thread "main" java.lang.IllegalArgumentException: can't serialize class entities.users.NormalUser
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:270)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:174)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:120)
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
at com.mongodb.OutMessage.putObject(OutMessage.java:289)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:261)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at repositories.UsersRepository.createUser(UsersRepository.java:35)
at repositories.UsersRepository.main(UsersRepository.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
UPDATE
Here is the super class:
@Data
public abstract class User implements Serializable {
static final long serialVersionUID = 12314362564783L;
private String surname;
private String address;
private String dni;
private Gender gender;
private String bankAccount;
private Role role;
private boolean admin;
private String name;
public User(final String name, final String surname, final String address,
final String dni, final Gender gender, final String bankAccount,
final Role role, final boolean admin) {
this.name = name;
this.surname = surname;
this.address = address;
this.dni = dni;
this.gender = gender;
this.bankAccount = bankAccount;
this.role = role;
this.admin = admin;
}
}
UPDATE #2
Is this enough to serialize the enum Gender
?
public enum Gender implements Serializable {
MALE, FEMALE;
}