Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:01:53+00:00 2026-06-12T22:01:53+00:00

I am using Spring Data Commons – 1.4.0.RC1 and Spring Data MongoDB – 1.1.0.RC1,

  • 0

I am using Spring Data Commons – 1.4.0.RC1 and Spring Data MongoDB – 1.1.0.RC1, having a problem with updating a list of embedded documents(of the same class hierarchy) using mongoTemplate.findAndModify(), which does not set the “_class” value as mongoTemplate.save() does, causing problems when reading data from mongodb as the application does not know which concrete class to instantiate for each embedded doc in that list.

For instance, I have a Student class, and a student has enrolled on subjects such as Maths, Physics, Computer Science, etc. This is not the domain I am working with, but should be enough to demonstrate the problem. So I will have a Student class mapped to a “student” collection and a list of subjects as embedded documents and all subjects extends the Subject super class, as below :

@Document
public class Student {

@Id
private String identifier;

private List<Subject> subjcts;
}  

public abstract class Subject {
// common properties and methods;
}

public class Maths  extends Subject {}

public class Physics extends Subject{}

To create a Student and save to database, I do

Student student = new Student();
student.setIdentifier(UUID.random().toString);
student.addSubject(new Maths());
student.addSubject(new Physics());

mongoTemplate.insert(student); or mongoTemplate.save(student);

The above will create a student with subjects maths and physics as embedded documents with an extra field “_class” specifying the concrete class so that it can be mapped to the correct class while fetching from the db.

However, if now i want to update the list of subjects, it is intuitive to do something like mongoTemplate.findAndModify(the query, new Update().set("subject", newSubjects), Student.class);

but findAndModify does not seem to go through the type mapper which sets the “_class” for the subjects, hence causes problems in reading back from db.

Is it a bug in Spring Data ? How do I work around it?

Any pointer will be appreciated.
Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:01:54+00:00Added an answer on June 12, 2026 at 10:01 pm

    This is a known issue of SpringData MongoDB (https://jira.springsource.org/browse/DATAMONGO-392).

    For now the only workaround I found is to create a specific converter for the inner-document class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using java/Spring/Ibatis sqlserver is the database and datasource is org.apache.commons.dbcp.BasicDataSource following is the data
I'm using Apache Commons HttpClient to grab some data from a server. My problem
I'm using Spring data neo4j 2.1.0.BUILD-SNAPSHOT and Neo4j 1.6.1 server. I have a Friendship
Whenever I specify a resource using the Spring Data Hadoop namespace, by application throws
I'm using Spring JdbcTemplate interface for fetching data from a MS SqlServer DB. In
I'm using the OleDbConnection class to retrieve data from an Excel 2000/2003 workbook: string
I'm using spring-data jpa 1.1.0.M1 now we were using a much higher version and
hi i am using spring config.xml to config my embedded database hsqldb. my spring
I am using OpenSessionInViewFilter, Spring, and Hibernate for data access. When I commit a
We are using Spring Data with the PageRequest and hitting a significantly large set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.