I have a base class Person and 2 classes that extends from it. When I add insert these classes into the database, it keeps all the info about the extended classes as shown below.
Below is the mongo config, code used to insert, get and remove the documents as well as the classes I am trying to map to.
people.json
[{
"_id": "d54d1113-2e00-46ef-8128-c6bc42220a6f",
"name": "Jo",
"_class": "person"
},
{
"_id": "3187d2a6-08da-4055-9080-2bfb7481a4a2",
"jobTitle": "Manager",
"name": "Josh",
"_class": "employee"
},
{
"_id": "80c25ae9-a402-469c-9873-5c093e093733",
"major": "Art",
"name": "John",
"_class": "student"
}]
However, when I grab the documents from the database, it only maps to the base Person class, losing the major and jobTitle field. I have tired adding a type alias to the classes, but it has not changed how the documents are mapped.
MongoConfig.java
package com.example.demo;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {
@Override
protected String getDatabaseName() {
return "test";
}
@Override
public MongoClient mongoClient() {
ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/example");
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.build();
return MongoClients.create(mongoClientSettings);
}
}
Test.java
package com.example.demo;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {
@Override
protected String getDatabaseName() {
return "test";
}
@Override
public MongoClient mongoClient() {
ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/example");
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.build();
return MongoClients.create(mongoClientSettings);
}
}
Person.java
package com.example.demo;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.UUID;
@TypeAlias("person")
@Document(collection = "person")
public class Person {
String id;
String name;
public Person() {
}
public Person(String name) {
this.id = UUID.randomUUID().toString();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + ''' +
", name='" + name + ''' +
'}';
}
}
Employee.java
package com.example.demo;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mongodb.core.mapping.Document;
@TypeAlias("employee")
@Document(collection = "person")
public class Employee extends Person{
String jobTitle;
public Employee(){
}
public Employee(String jobTitle) {
this.jobTitle = jobTitle;
}
public Employee(String name, String jobTitle) {
super(name);
this.jobTitle = jobTitle;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
@Override
public String toString() {
return "Employee{" +
"jobTitle='" + jobTitle + ''' +
", id='" + id + ''' +
", name='" + name + ''' +
'}';
}
}
Student.java
package com.example.demo;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mongodb.core.mapping.Document;
@TypeAlias("student")
@Document(collection = "person")
public class Student extends Person {
String major;
public Student(String name, String major) {
super(name);
this.major = major;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public String toString() {
return "Student{" +
"major='" + major + ''' +
", id='" + id + ''' +
", name='" + name + ''' +
'}';
}
}