我想问一下如何插入或更新我的课程中包含的hashmap 他们是我的班级设备:
package com.project.beans;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
@Entity
@Inheritance( strategy = InheritanceType.JOINED )
public class Equipement {
@Id
@GeneratedValue( strategy = GenerationType.TABLE )
private int idEquip;
private String reference;
private static final int HT = 744;
@ElementCollection
Map<String, String> HM;
@ManyToMany( fetch = FetchType.EAGER )
private Collection<Systeme> systemes = new ArrayList<Systeme>();
public Equipement() {
HM = new HashMap<String, String>();
}
public Equipement( String ref, ArrayList<Systeme> systemes ) {
this.reference = ref;
this.systemes = systemes;
HM = new HashMap<String, String>();
}
public Collection<Systeme> getSystemes() {
return systemes;
}
public void setSystemes( Collection<Systeme> systemes ) {
this.systemes = systemes;
}
public String getReference() {
return reference;
}
public void setReference( String reference ) {
this.reference = reference;
}
public Map<String, String> getHM() {
return HM;
}
public void setHM( Map<String, String> hM ) {
HM = hM;
}
public int getIdEquip() {
return idEquip;
}
public static int getHt() {
return HT;
}
}
我的班级服务:
public class EquipementService2 {
private Session session;
private Transaction tx;
public boolean update( Equipement o ) {
try {
session = HibernateUtil.getSessionFactory().openSession();
tx = session.beginTransaction();
session.update( o );
tx.commit();
return true;
} catch ( Exception e ) {
if ( tx != null )
tx.rollback();
return false;
}
}
public Equipement findById( int id ) {
Equipement equipement = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
tx = session.beginTransaction();
equipement = (Equipement) session.get( Equipement.class, id );
tx.commit();
return equipement;
} catch ( Exception e ) {
if ( tx != null )
tx.rollback();
return null;
}
}}
我的servlet:
public class Validation_hm extends HttpServlet {
public static final String VUE = "/WEB-INF/entretientDra.jsp";
public static final String VUE2 = "/WEB-INF/ajoutHM.jsp";
public static final String VUE9 = "/accueil.jsp";
public static final String ATT_1 = "form";
public static final String ATT_2 = "mar";
public static final String ATT_3 = "key";
protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
IOException {
HeurM_GST hg = new HeurM_GST();
EquipementService2 es = new EquipementService2();
Equipement equipement = hg.creeEquipement( request );
int id = hg.idEquip( request );
request.setAttribute( ATT_3, hg.key( request ) );
request.setAttribute( ATT_2, equipement );
request.setAttribute( ATT_1, hg );
request.setAttribute( "idequip", id );
if ( hg.getErreurs().isEmpty() ) {
es.update( equipement );
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
}
} else {
this.getServletContext().getRequestDispatcher( VUE2 ).forward( request, response );
}
}}
类HeurM_GST
public class HeurM_GST {
private static final String CHAMP_DATE = "date";
private static final String CHAMP_HM = "hm";
private static final String CHAMP_ID = "idequip";
private static final String CHAMP_KEY = "key";
// .....
//......
private static String getValeurChamp( HttpServletRequest
request, String nomChamp ) {
String valeur = request.getParameter( nomChamp );
if ( valeur == null || valeur.trim().length() == 0 ) {
return null;
} else {
return valeur;
}
}
public Equipement creeEquipement( HttpServletRequest request ) {
String date = getValeurChamp( request, CHAMP_DATE );
String hm = getValeurChamp( request, CHAMP_HM );
EquipementService2 es = new EquipementService2();
Equipement equipement = es.findById( this.idEquip( request ) );
// date HM
try {
validationDate( date );
} catch ( Exception e ) {
setErreur( CHAMP_DATE, e.getMessage() );
}
// nbr heures
try {
validationHM( hm );
} catch ( Exception e ) {
setErreur(CHAMP_HM, e.getMessage() );
}
// HM
equipement.getHM().put( date, hm );
return equipement;
}
}
我没有错误..但我的hashmap没有更新!
答案 0 :(得分:0)
对我而言,它有效......是您在事务中的本机查询吗?...但更好的方法是使用该对象。
在您的情况下,您可以修改哈希图,而不是将修改后的对象用于更新方法。
会话示例:
您的代码中有Equipement-Object。使用它并删除或添加一些东西到hashmap。在您的服务中调用您的方法public boolean update( Equipement o ) {
。数据库应该更新它。
仅限方法:
// Add to the entity.
public void addToEntity(EntityManager em, Long id, String value, String key){
em.getTransaction().begin();
Equipement eq = em.find(Equipement.class, id);
eq.getHM().put(key, value);
em.getTransaction().commit();
}
// Remove from the entity.
public void removeFromEntity(EntityManager em, Long id, String key){
em.getTransaction().begin();
Equipement eq= em.find(Equipement.class, id);
eq.getHM().remove(key);
em.getTransaction().commit();
}
// Merge entity with modified hashmap to database
public Equipement mergeEntity(EntityManager em, Equipement eq){
em.getTransaction().begin();
eq= em.merge(eq);
em.getTransaction().commit();
return eq;
}