在hibernate中更新包含列表的对象
在我的web应用程序中,我尝试更新包含实体列表[Projet]的对象[Chefprojet];[Chefprojet]和[Projet]之间存在一对多关系。我尝试了下面的代码,但出现以下错误:在hibernate中更新包含列表的对象,hibernate,servlets,Hibernate,Servlets,在我的web应用程序中,我尝试更新包含实体列表[Projet]的对象[Chefprojet];[Chefprojet]和[Projet]之间存在一对多关系。我尝试了下面的代码,但出现以下错误: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Sourc
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at dao.Chefprojet2.reaffecterchef(Chefprojet2.java:27)
at controller.Chefprojet2ControllerServlet.doPost(Chefprojet2ControllerServlet.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at javax.servlet.http.HttpServlet.service(HttpServlet.ja
Chefprojet2.java
public void reaffecterchef(String matricule, String getHiddenValue ){
try {
Session session4=HibernateUtil.getSessionFactory().getCurrentSession();
session4.beginTransaction();
Query query= session4.createQuery("select p.id from Projet p where p.libelle='"+getHiddenValue+"'");
List list1 = query.list();
int idp= (int) list1.get(0);
Projet projet=(Projet) session4.load(Projet.class,idp);
Query qry = session4.createQuery("select chef.id_chef from Chefprojet chef WHERE chef.matricule='" +matricule+ "'");
List list = qry.list();
int idm= (int) list.get(0);
Chefprojet chp=(Chefprojet) session4.load(Chefprojet.class,idm);
chp.getProjets().add(projet);
projet.setChefprojet(chp);
session4.persist(projet);
session4.getTransaction().commit();
System.out.println("\n\n Details Added \n");
} catch (HibernateException e) {
System.out.println(e.getMessage());
System.out.println("error");
}
}
Chefprojet2ControllerServlet.java
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String matricule=request.getParameter("matricule");
String getHiddenValue=request.getParameter("hidden");
System.out.println(matricule+" Hidden field Value is :"+getHiddenValue);
try {
Chefprojet2 cprojet2 = new Chefprojet2();
cprojet2.reaffecterchef(matricule, getHiddenValue);
} catch (Exception e) {
e.printStackTrace();
}
}
请帮帮我 您的查询(就在第27行之前)返回空结果集,因此
List list = query.list();
是emtpy
接下来,您将访问此空列表的第一个元素:
int id = (int) list.get(0);
获取java.lang.IndexOutOfBoundsException:Index:0,Size:0
从ArrayList
实现
首先检查列表是否为空:list.isEmpty()
或类似的内容