![](/img/trans.png)
[英]Eclipse Hibernate.cfg.xml is linking “sys” database from MySQL
[英]Error to create new table in mysql using hibernate cfg,xml in eclipse
我在 src/main/resources 下收到此錯誤和 Log4j2.xml 以及 hibernate.cfg.xml,但我不確定為什么我會收到以下錯誤:
INFO: Server startup in 6322 ms
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
Hibernate: drop table if exists USERS_NEW
Hibernate: create table USERS_NEW (ID integer not null auto_increment, NAME varchar(255), TOTAL integer, GOAL integer, primary key (ID)) type=MyISAM
Hibernate: insert into USERS_NEW (NAME, TOTAL, GOAL) values (?, ?, ?)
12:06:53.426 [http-nio-8080-exec-2] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Table 'studentdb.users_new' doesn't exist
Feb 08, 2021 12:06:53 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [InitHibernate] in context with path [/protein-tracker-xml] threw exception
org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:200)
at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3200)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3806)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:84)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:645)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:282)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:263)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:317)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:330)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:123)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:179)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
這是servlet代碼
package com.simplilearn.workshop;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.simplilearn.workshop.model.User;
import com.simplilearn.workshop.util.HibernateUtil;
//@WebServlet("/init")
public class InitHibernate extends HttpServlet {
private static final long serialVersionUID = 1L;
public InitHibernate() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
PrintWriter out = response.getWriter();
out.println("<html><body>");
SessionFactory factory = HibernateUtil.getSessionFactory();
//retrieve a hibernate session
Session session = factory.openSession();
out.println("Hibernate Session opened.<br>");
out.println("Saving User Data..... .<br>");
//begin new transaction
session.beginTransaction();
User user = new User();
//user.setId(2);
user.setName("max");
user.setGoal(290);
user.setTotal(250);
//execute database operation
session.save(user);
// commit transaction
session.getTransaction().commit();
out.println("User Data is Saved.<br>");
// flush and close session
session.close();
out.println("Hibernate Session closed.<br>");
//session.beginTransaction();
//User loadedUser = (User)session.get(User.class, 1);
//out.println("Name : " + loadedUser.getName() + "<br/>");
// out.println("Goal : " + loadedUser.getGoal() + "<br/>");
//manipulate the loaded user
//loadedUser.setTotal(loadedUser.getTotal() + 50);
//session.getTransaction().commit(); // auto updating
//Flush and Close Session
out.println("</body></html>");
} catch (Exception ex) {
throw ex;
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
在.xml 或.properties 文件中更改設置時清理項目。 在 eclipse 環境中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.