1

My intention for my project is to allow users to take a test and answer every multiple choice question. I assume the best idea is to create a HashMap inside the User object-model class. I wanted the Key to be a Question object, and the Value to be either an Integer for the multiple choice answer, or an "Answer" object.

This question is unique because I'm asking how to properly map a HashMap into a database using Hibernate. This HashMap in particular uses two different Objects for the Key and the Value. I don't get this issue mapping Array Lists, but I do with Hashmaps, and I think it has to do with the way I name the annotations?

When saving data into the database I get:

"could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement] with root cause java.sql.SQLException: Field 'question1_id' doesn't have a default value"

package com.example.scrennerMVC.models;

import javax.persistence.*;
import java.util.Map;

 @Entity
 public class User1 {

     @Id
     @GeneratedValue
     private long id;

     private String email;

     @OneToMany(mappedBy="user")
     @MapKeyJoinColumn(name="QUESTION1_ID")
     private Map<Question1, Answer> answer; 
}

@Entity
public class Question1 {
    @Id
    @GeneratedValue
    private long id;

    @Basic
    private String question;
}



@Entity
public class Answer {

    @Id
    @GeneratedValue
    private long id;

    @ManyToOne
    private User user;

    private int answer;

}
William Toscano
  • 209
  • 2
  • 10
  • Possible duplicate of [Hibernate: "Field 'id' doesn't have a default value"](https://stackoverflow.com/questions/804514/hibernate-field-id-doesnt-have-a-default-value) – Justin Albano Apr 05 '18 at 02:11

0 Answers0