I’m working on a Spring Boot application and I’m facing an issue with handling nested objects within an entity.
I have an entity called TopicEntity, which has a field topicContent defined as a Map<Integer, String>. This field is used to store key-value pairs of topic content, where the key represents the order index and the value represents the content.
Now, I need to extend this functionality to support storing nested objects within topicContent. Each nested object contains a question, correct answer, and incorrect answers. Here’s an example of the desired JSON structure:
json structure
I’ve tried changing the mapping of topicContent to Map<Integer, Object>, but I encountered these errors:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘entityManagerFactory’
Caused by: org.hibernate.MappingException: collection element mapping has wrong number of columns: com.Project.LearningHub.Entity.TopicEntity.topicContent type: object
Here’s my current code:
<code>package com.Project.LearningHub.Entity;
import java.util.HashMap;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonBackReference;
@Table(name = "tblTopic")
public class TopicEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JoinColumn(name = "lesson_id")
private LessonEntity lesson;
@Column(name = "lesson_id", insertable = false, updatable = false)
private String topicTitle;
@CollectionTable(name = "topic_content_mapping", joinColumns = @JoinColumn(name = "topic_id"))
@MapKeyColumn(name = "order_index")
@Column(name = "content")
private Map<Integer, String> topicContent = new HashMap<>();
public TopicEntity(int topicId, LessonEntity lesson, String topicTitle, Map<Integer, String> topicContent) {
this.topicTitle = topicTitle;
this.topicContent = topicContent;
public int getTopicId() {
public void setTopicId(int topicId) {
public LessonEntity getLesson() {
public void setLesson(LessonEntity lesson) {
public String getTopicTitle() {
public void setTopicTitle(String topicTitle) {
this.topicTitle = topicTitle;
public Map<Integer, String> getTopicContent() {
public void setTopicContent(Map<Integer, String> topicContent) {
this.topicContent = topicContent;
public int getLessonId(){
<code>package com.Project.LearningHub.Entity;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonBackReference;
@Entity
@Table(name = "tblTopic")
public class TopicEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int topicId;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "lesson_id")
private LessonEntity lesson;
@Column(name = "lesson_id", insertable = false, updatable = false)
private int lessonId;
private String topicTitle;
@ElementCollection
@CollectionTable(name = "topic_content_mapping", joinColumns = @JoinColumn(name = "topic_id"))
@MapKeyColumn(name = "order_index")
@Column(name = "content")
private Map<Integer, String> topicContent = new HashMap<>();
public TopicEntity() {
super();
}
public TopicEntity(int topicId, LessonEntity lesson, String topicTitle, Map<Integer, String> topicContent) {
this.topicId = topicId;
this.lesson = lesson;
this.topicTitle = topicTitle;
this.topicContent = topicContent;
}
public int getTopicId() {
return topicId;
}
public void setTopicId(int topicId) {
this.topicId = topicId;
}
public LessonEntity getLesson() {
return lesson;
}
public void setLesson(LessonEntity lesson) {
this.lesson = lesson;
}
public String getTopicTitle() {
return topicTitle;
}
public void setTopicTitle(String topicTitle) {
this.topicTitle = topicTitle;
}
public Map<Integer, String> getTopicContent() {
return topicContent;
}
public void setTopicContent(Map<Integer, String> topicContent) {
this.topicContent = topicContent;
}
public int getLessonId(){
return this.lessonId;
}
}
</code>
package com.Project.LearningHub.Entity;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonBackReference;
@Entity
@Table(name = "tblTopic")
public class TopicEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int topicId;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "lesson_id")
private LessonEntity lesson;
@Column(name = "lesson_id", insertable = false, updatable = false)
private int lessonId;
private String topicTitle;
@ElementCollection
@CollectionTable(name = "topic_content_mapping", joinColumns = @JoinColumn(name = "topic_id"))
@MapKeyColumn(name = "order_index")
@Column(name = "content")
private Map<Integer, String> topicContent = new HashMap<>();
public TopicEntity() {
super();
}
public TopicEntity(int topicId, LessonEntity lesson, String topicTitle, Map<Integer, String> topicContent) {
this.topicId = topicId;
this.lesson = lesson;
this.topicTitle = topicTitle;
this.topicContent = topicContent;
}
public int getTopicId() {
return topicId;
}
public void setTopicId(int topicId) {
this.topicId = topicId;
}
public LessonEntity getLesson() {
return lesson;
}
public void setLesson(LessonEntity lesson) {
this.lesson = lesson;
}
public String getTopicTitle() {
return topicTitle;
}
public void setTopicTitle(String topicTitle) {
this.topicTitle = topicTitle;
}
public Map<Integer, String> getTopicContent() {
return topicContent;
}
public void setTopicContent(Map<Integer, String> topicContent) {
this.topicContent = topicContent;
}
public int getLessonId(){
return this.lessonId;
}
}