État HTTP 500 – 内部服务器错误
Type Rapport d''exception
message org.hibernate.exception.ConstraintViolationException: could not execute statement
description Le serveur a rencontré une erreur interne qui l''a empêché de satisfaire la requête.
exception
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:780)
org.hibernate.internal.SessionImpl.persist(SessionImpl.java:758)
ma.axa.HibernateDAO.TachesDAO.saveData(TachesDAO.java:26)
ma.axa.Servlets.FrontServlet.doPost(FrontServlet.java:50)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
cause mère
org.hibernate.exception.ConstraintViolationException: could not execute statement
org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57)
org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2907)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3478)
org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:623)
org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:277)
org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:258)
org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:303)
org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:318)
org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:275)
org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)
org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113)
org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:773)
org.hibernate.internal.SessionImpl.persist(SessionImpl.java:758)
ma.axa.HibernateDAO.TachesDAO.saveData(TachesDAO.java:26)
ma.axa.Servlets.FrontServlet.doPost(FrontServlet.java:50)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
cause mère
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'nom_tache' cannot be null
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
com.mysql.jdbc.Util.getInstance(Util.java:387)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:934)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57)
org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2907)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3478)
org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:623)
org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:277)
org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:258)
org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:303)
org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:318)
org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:275)
org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)
org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113)
org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:773)
org.hibernate.internal.SessionImpl.persist(SessionImpl.java:758)
ma.axa.HibernateDAO.TachesDAO.saveData(TachesDAO.java:26)
ma.axa.Servlets.FrontServlet.doPost(FrontServlet.java:50)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note La trace complète de la cause mère de cette erreur est disponible dans les fichiers journaux de ce serveur.
阿帕奇雄猫/8.5.16
该项目的结构是:
这是塔奇斯类.java
public class Taches {
private Integer id_tache;
private String nom_tache;
private String description_tache;
//les constructeurs
public Taches()
{
super();
}
public Taches(Integer id_tache, String nom_tache, String description_tache) {
super();
this.id_tache = id_tache;
this.nom_tache = nom_tache;
this.description_tache = description_tache;
}
//getters and setters
public Integer getId_tache() {
return id_tache;
}
public void setId_tache(Integer id_tache) {
this.id_tache = id_tache;
}
public String getNom_tache() {
return nom_tache;
}
public void setNom_tache(String nom_tache) {
this.nom_tache = nom_tache;
}
public String getDescription_tache() {
return description_tache;
}
public void setDescription_tache(String description_tache) {
this.description_tache = description_tache;
}
这是TacheDAO类.java
public class TachesDAO {
Session session;
private Session setSession(){
Session session=HibernateUtil.getSessionFactory().openSession();
return session;
}
//enregistrer des donnees dans la table categories
public void saveData(Taches tache){
session=setSession();
try{
session.beginTransaction();
session.persist(tache);
session.getTransaction().commit();
}catch(HibernateException he){
he.printStackTrace();
if(session.getTransaction()!=null){
session.getTransaction().rollback();
}
}finally{
if(session!=null){
session.close();
}
}
}
//selectionner la liste des donnees
public List<Taches> selectAllData(){
List<Taches> ltaches=new ArrayList();
session=setSession();
try{
session.beginTransaction();
Query<Taches> query=session.createQuery("From Taches");
ltaches=query.list();
session.getTransaction().commit();
return ltaches;
}catch(HibernateException he){
he.printStackTrace();
if(session.getTransaction()!=null){
session.getTransaction().rollback();
}
return null;
}finally{
if(session!=null){
session.close();
}
}
}
public Taches getTachesById(Integer id){
Taches tache=null;
Session session=setSession();
try{
session.beginTransaction();
tache=(Taches)session.get(Taches.class, id);
session.getTransaction().commit();
}catch(HibernateException he){
he.printStackTrace();
if(session.getTransaction()!=null){
session.getTransaction().rollback();
}
}finally{
if(session!=null){
session.close();
}
}
return tache;
}
这是文件休眠的实用工具.java
public class HibernateUtil {
private static final SessionFactory sessionFactory=buildSessionFactory();
private static SessionFactory buildSessionFactory(){
try {
// Create SessionFactory with hibernate.cfg.xml configure+build
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex)
{
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
这是 servletFrontSERVLET
public class FrontServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.getRequestDispatcher("/WEB-INF/backOffice/index.jsp").forward(req, resp);
Taches tache=new Taches();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Taches tache=new Taches();
TachesDAO TDAO=new TachesDAO();
String titre=req.getParameter("nom_tache");
String description=req.getParameter("description_tache");
tache.setNom_tache(titre);
tache.setDescription_tache(description);
TDAO.saveData(tache);
}
这是我的页面索引.jsp
<div class="ajouter">
<a>Ajouter</a>
<form method="post" enctype="multipart/form-data" boundary="boundary">
Nom de la tache: <input type="text" name="module"><br>
Description: <input type="text" name="description"><br>
<input type="submit" value="Ajouter">
</form>
</div>
您有一个命名问题:(请注意名称为module
(
Nom de la tache: <input type="text" name="module"><br>
但你读为
String titre=req.getParameter("nom_tache");
所以nom_tache字段变为空,所以你得到
....MySQLIntegrityConstraintViolationException: Column 'nom_tache' cannot be null ....
希望这有帮助...
在你的类Taches
中, 您忘记了注释,这是告诉休眠在哪里插入数据的最重要的信息
@Entity
public class Taches {
@Id
@Column
private Integer id_tache;
@Column
private String nom_tache;
@Column
private String description_tache;
//Constructor //Getters and setters}