无法写内容:懒得初始化角色集合,无法初始化代理 - 没有会话通过引用链

时间:2018-01-25 13:16:19

标签: postgresql hibernate spring-mvc

我收到此错误。我不知道这个错误是什么以及如何解决。

错误: -

Could not write content: failed to lazily initialize a collection of role: com.neostencil.modules.testmanagement.model.entities.question.Question.options, could not initialize proxy - no Session (through reference chain: com.neostencil.modules.testmanagement.responses.question.FetchQuestionById[\"question\"]->com.neostencil.modules.testmanagement.model.entities.question.Question[\"options\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.neostencil.modules.testmanagement.model.entities.question.Question.options, could not initialize proxy - no Session (through reference chain: com.neostencil.modules.testmanagement.responses.question.FetchQuestionById[\"question\"]->com.neostencil.modules.testmanagement.model.entities.question.Question[\"options\"])"

我正在使用@ElementCollection来存储string的列表。这是我的实体模型

@Entity
@Table(name = "Question")
public class Question extends DomainObject {

@GeneratedValue(strategy = GenerationType.TABLE, generator = "question_gen")
@TableGenerator(name = "question_gen", table = "ns_question_id_gen", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "QuestionId_Gen", initialValue = 10000, allocationSize = 100)
@Id
@Column(name = "question_id")
private int questionId;

@Column(name = "question_text")
private String text;

@Column(name = "question_type")
@Enumerated(EnumType.STRING)
private QuestionType type;

@Column(name = "question_timeout")
private Double timeout;

@Column(name = "marks")
private Double marks;

@Column(name = "negative_mark")
private Double negativeMark;

@Column(name = "question_position")
private Double position;

@ElementCollection()
@CollectionTable(name = "ns_question_option_items",joinColumns = @JoinColumn(name = "question_id"))
@Column(name = "question_options")
public List<String> options = new ArrayList<>();
public List<String> getOptions() {
    return options;
}
public void setOptions(List<String> options) {
    this.options = options;
}

@Column(name = "subject")
private String subject;

@Column(name = "test_id")
private BigInteger testId;

public BigInteger getTestId() {
    return testId;
}


public void setTestId(BigInteger testId) {
    this.testId = testId;
}

public Question() {
    super();
}

public Question(int questionId, String questionText, QuestionType questionType, Double questionTimeout, Double marks, Double negativeMark, Double questionPosition, List<String> questionOptions, String subject, Answer answer) {
    super();
    this.questionId = questionId;
    this.text = questionText;
    this.type = questionType;
    this.timeout = questionTimeout;
    this.marks = marks;
    this.negativeMark = negativeMark;
    this.position = questionPosition;
    this.options = questionOptions;
    this.subject = subject;


}

public Question(String createdBy, Timestamp creationDate, int version, Timestamp lastModifiedDate,
                String lastModifiedBy, RecordStatus recordStatus) {
    super(createdBy, creationDate, version, lastModifiedDate, lastModifiedBy, recordStatus);
}

public int getQuestionId() {
    return questionId;
}

public void setQuestionId(int questionId) {
    this.questionId = questionId;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public QuestionType getType() {
    return type;
}

public void setType(QuestionType type) {
    this.type = type;
}

public Double getTimeout() {
    return timeout;
}

public void setTimeout(Double timeout) {
    this.timeout = timeout;
}

public Double getMarks() {
    return marks;
}

public void setMarks(Double marks) {
    this.marks = marks;
}

public Double getNegativeMark() {
    return negativeMark;
}

public void setNegativeMark(Double negativeMark) {
    this.negativeMark = negativeMark;
}

public Double getPosition() {
    return position;
}

public void setPosition(Double position) {
    this.position = position;
}





public String getSubject() {
    return subject;
}

public void setSubject(String subject) {
    this.subject = subject;
}
}

我在使用问题ID从表中提取问题模型时收到此错误。这是我提取问题的函数

public Question fetchQuestionById(int id) {
    SessionFactory sessionFactory = createSessionFactory(configuration);
    Session session = sessionFactory.openSession();

    Question question = session.get(Question.class, new Integer(id));
    List<String> options = question.getOptions();

    question.setOptions(options);
    session.close();
    return question;
}

我正在使用Postgres数据库。我想在数据库中存储一个字符串列表,以便我使用@ElementCollection注释。我在互联网上搜索但没有发现任何东西。这里有什么问题?在数据库中存储字符串列表的正确方法是什么?如果没有,请告诉我以上问题的解决方案。

0 个答案:

没有答案