休眠异常无法加载类“org.slf4j.impl.StaticLoggerBinder”
Posted
技术标签:
【中文标题】休眠异常无法加载类“org.slf4j.impl.StaticLoggerBinder”【英文标题】:Hibernate Exception Failed to load class "org.slf4j.impl.StaticLoggerBinder" 【发布时间】:2021-07-24 07:53:48 【问题描述】:我想在我的 oracle 数据库中执行更新查询。但我收到以下错误:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.dom4j.io.SAXContentHandler (file:/C:/hibernate/lib/required/dom4j-1.6.1.jar) to method com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$LocatorProxy.getEncoding()
WARNING: Please consider reporting this to the maintainers of org.dom4j.io.SAXContentHandler
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Hibernate: update ëmp1 set ename=?, esal=?, eaddr=? where eno=?
Employee updation failure
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.durgasoft.test.Test.main(Test.java:33)
Caused by: java.sql.BatchUpdateException: ORA-00942: table or view does not exist
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10296)
at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:216)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 8 more
项目中的文件:
Pojo 类代码 Employee.java
package com.MyApp.pojo;
public class Employee
private int eno;
private String ename;
private float esal;
private String eaddr;
// getter and setters
主要方法 Test.java
package com.durgasoft.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.MyApp.pojo.Employee;
public class Test
public static void main(String[] args)
Configuration config=null;
SessionFactory sessionFactory=null;
Session session=null;
Transaction tx=null;
try
config = new Configuration();
config.configure();
sessionFactory=config.buildSessionFactory();
session=sessionFactory.openSession();
tx=session.getTransaction();
tx.begin();
Employee emp=new Employee();
emp.setEno(111);
emp.setEname("XXX");
emp.setEsal(1000);
emp.setEaddr("ABCD");
session.update(emp);
tx.commit();
System.out.println("Employee Updated Successfully");
catch(Exception e)
tx.rollback();
System.out.println("Employee updation failure");
e.printStackTrace();
finally
session.close();
sessionFactory.close();
休眠配置文件 hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="org.hibernate.driver_Class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
休眠映射文件 Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.durgasoft.pojo.Employee" table="ëmp1">
<id name="eno"/>
<property name="ename"/>
<property name="esal"/>
<property name="eaddr"/>
</class>
</hibernate-mapping>
【问题讨论】:
【参考方案1】:不确定您是否已在数据库中创建表。试试这两个选项:
-
在 DB 中添加表 emp1 并重新运行您的应用程序。
在您的休眠配置文件中添加以下配置。
<property name="hbm2ddl.auto">update</property>
您可以参考此链接了解您可以使用的各种值。
https://***.com/a/23678638/8148637
编辑:
您正在使用session.update(emp)
更新一个空表。改为使用:
session.saveOrUpdate(emp)
【讨论】:
``` SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。 SLF4J:默认为无操作 (NOP) 记录器实现 SLF4J:有关详细信息,请参阅 slf4j.org/codes.html#StaticLoggerBinder。警告:发生了非法反射访问操作 警告:org.dom4j.io.SAXContentHandler(文件:/C:/hibernate/lib/required/dom4j-1.6.1.jar)对方法 com.sun.org 的非法反射访问.apache.xerces.internal.parsers.AbstractSAXParser$LocatorProxy.getEncoding() 警告:请考虑将此报告给 org.dom4j.io.SAXContentHandler 的维护者 ``` 警告:使用 --illegal-access=warn 启用对进一步非法反射访问操作的警告警告:所有非法访问操作将在未来版本中被拒绝 Hibernate:更新 emp set ename=?, esal =?, eaddr=?埃诺=?员工更新成功。但桌子是空的。 你做了什么改变,它现在正在更新表格。表名改了吗? 是的,更改表名后,它开始显示此消息。 您的表中没有记录。您可以使用 session.save 插入新记录,使用 session.update 更新该记录。在这里,您的表已经是空的,并且您正在使用更新。尝试在您的情况下使用 session.saveOrUpdate(emp) 。如果它不存在,它将插入员工。如果存在,则更新它。以上是关于休眠异常无法加载类“org.slf4j.impl.StaticLoggerBinder”的主要内容,如果未能解决你的问题,请参考以下文章
SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。错误
SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”
Play/Scala 2.5 SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”
添加javax.servlet-api依赖项抛出org.slf4j.impl.StaticLoggerBinder无法加载但我不使用sl4j的异常,我使用百万美元