简体   繁体   中英

Java hashmap mapping in database

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM