我的实体课是

@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 + " ]";
}

}


我的控制器功能是

   @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);
}


我想通过使用HashMap设置setHashmap()并想将实体持久存储在数据库中。当我想持久存储实体的其他属性但hashmap属性包含BLOB对象时,这是一种适当的方式导致的。

如何持久保存原始类型的哈希图?

最佳答案

JPA使用@ MapKey,@ MapKeyJoinColumn ...批注支持Mappersistencse
有关详细信息,请参阅以下文章:

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

关于java - 数据库中的Java哈希图映射,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22292393/

10-10 13:43