0

My entity class is

@Entity
public class Student_enroll implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private int level_num;
private int term;
private String student_session;
private HashMap<String,Integer>mark_value;

@OneToOne
private Student student;

public String getStudent_session() {
    return student_session;
}

public void setStudent_session(String student_session) {
    this.student_session = student_session;
}



public int getLevel_num() {
    return level_num;
}

public void setLevel_num(int level_num) {
    this.level_num = level_num;
}

public int getTerm() {
    return term;
}

public void setTerm(int term) {
    this.term = term;
}

public Student getStudent() {
    return student;
}

public void setStudent(Student student) {
    this.student = student;
}

public HashMap<String, Integer> getMark_value() {
    return mark_value;
}

public void setMark_value(HashMap<String, Integer> mark_value) {
    this.mark_value = mark_value;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Student_enroll)) {
        return false;
    }
    Student_enroll other = (Student_enroll) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.domain.Student_enroll[ id=" + id + " ]";
}

}

and my controller function is

   @RequestMapping(value="/add_mark",method = RequestMethod.POST)
   public void add_mark(HttpServletRequest req){

   HashMap<String,Integer>map=new HashMap<String,Integer>();

   int level=Integer.parseInt(req.getParameter("level"));

   int term=Integer.parseInt(req.getParameter("term"));

   Student_enroll enroll=student_service.get_student_enroll(level, term);

   List<Course>list_course=course_service.list_course(level, term);

   Iterator<Course>itr=list_course.iterator();

    while(itr.hasNext()){

      enroll.put(itr.next().getCourse_Code(),75);
    }

    enroll.setMark_value(map); // Set hashmap 

   student_service.update_student_enroll(enroll);
}

I want to set the HashMap by using setHashmap() and want to persist the entity in database.is it an appropriate way cause when I want to persist it other attribute of entity is persisted but the hashmap attribute contains a BLOB object.

How to persist a hashmap of primitive type?

5
  • Can you let us know what is the column type in database for your Entity field private HashMap<String,Integer>mark_value ? Commented Mar 10, 2014 at 5:34
  • i am using hibernate when i persist a enttity hibernate create the column type according to data type in entity.for example a String data type will be converted to varchar column type.i don't know about hashmap.i mean what column type hibernate will create for hashmap i am not sure. Commented Mar 10, 2014 at 5:53
  • See, relational databases doesnt have a column type HashMap. You can consider hashmap itself as a table, with two columns....and add that to your entity as join columns. Commented Mar 10, 2014 at 6:06
  • that's absolutely right,but in my case a mark column field is created which value is a BLOB object.I was expecting a table with Student_enroll id and two field coursecode and mark,but that has not happened Commented Mar 10, 2014 at 6:12
  • In that case can you change the column type in entity as byte[]. You will have to convert the hashmap data into byte[] yourself though in some pre-processing step. Commented Mar 10, 2014 at 7:52

1 Answer 1

1

JPA supports Map persistencse using @MapKey, @MapKeyJoinColumn ... annotations Refer to following article for details:

http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Maps

Sign up to request clarification or add additional context in comments.

2 Comments

mavarazy,thanks for your answer.but those example usese a object in value field of a hashmap.i want to use a prmitive type in the value field of a hashmap.
Autoboxing will automatically convert primitive to appropriate object and vise versa. Maps were designed in Java to handle objects as keys, I would use autoboxing, ignoring performance concerns, that you might have, pursing performance in this case, can have opposite effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.