1.
There is no Action mapped for namespace [/] and action name [insertMess] associated with context path [/NeverForget].
解决:
注意namespace命名规则
:我一开始命名的为 /message/
然后根据Struts action 寻找规则, 其内部规则会根据路径分层查找:
比如:http://localhost:8080/NeverForget/message/insertMess.action
这个请求,就会先找namespace为/message的包空间,然后再找action
如果action未找到,则会找根目录默认命名空间:/
再比如http://localhost:8080/NeverForget/message/abc/insertMess.action
则会先找为: /message/abc的命名空间,如果有再从里面找action
如果action未找到,则会找/message中的action
如果还没有action,则会找/目录下的action.
2.
Unable to process Jar entry [module-info.class] from Jar [file:/F:/java5/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/NeverForget/WEB-INF/lib/txw2-2.3.1.jar] for annotations
org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:97)
解决:tomcat版本过低,对jar包解析不识别.
出现问题时的tomcat版本为8.5,而hibernate版本为最新的hibernate-release-5.4.30.Final. 版本不匹配,无法解析最新的东西.
将tomcat版本升至9.0,问题解决.
3.
Unable to perform unmarshalling at line number 0 and column 0 in FILE C:\Users\change\AppData\Local\Temp[#document: null]8205384379655241849cfg.xml. Message: null
如图:
这是因为对应hibernate.cfg.xml文件中有:
这类的注释
解析异常的东西,一般不会影响运行,如果把这些注释清除,就可以解决问题.
常见hibernate.cfg.xml配置:
<!--数据库连接池的大小-->
<property name="hibernate.connection.pool.size">20 </property>
<!--是否在后台显示Hibernate用到的SQL语句,开发时设置为true,便于差错,程序运行时可以在Eclipse的控制台显示Hibernate的执行Sql语句。项目部署后可以设置为false,提高运行效率-->
<property name="hibernate.show_sql">true </property>
<!--jdbc.fetch_size是指Hibernate每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢-->
<property name="jdbc.fetch_size">50 </property>
<!--jdbc.batch_size是指Hibernate批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大-->
<property name="jdbc.batch_size">23 </property>
<!--jdbc.use_scrollable_resultset是否允许Hibernate用JDBC的可滚动的结果集。对分页的结果集。对分页时的设置非常有帮助-->
<property name="jdbc.use_scrollable_resultset">false </property>
<!--connection.useUnicode连接数据库时是否使用Unicode编码-->
<property name="Connection.useUnicode">true </property>
<!--connection.characterEncoding连接数据库时数据的传输字符集编码方式,最好设置为gbk,用gb2312有的字符不全-->
<property name="connection.characterEncoding">gbk </property>
<!-- 这样配置是本地jdbc事务配置,你通过getCurrentSession创建的session会绑定到当前线程 -->
<property name="hibernate.current_session_context_class">thread</property>
<!--hibernate.dialect 只是Hibernate使用的数据库方言,就是要用Hibernate连接那种类型的数据库服务器。-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property>
<!--指定映射文件为“包名/文件.xml”注意路径-->
<mapping resource="com/k/daoS/Message.hbm.xml" />
4.
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: message is not mapped [from message where recipientId=?]
5.
常用HibernateUtil类
package com.k.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
//为保证线程安全,将Seeeion放到ThreadLocal中管理。这样就避免了Session的多线程共享数据的问题
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static SessionFactory sessionFactory = null;//SessionFactory对象
//静态块(在类被加载时执行,且生命周期内只执行一次)
static {
try {
// 加载Hibernate配置文件
Configuration cfg = new Configuration().configure();
// 创建会话工厂
// hibernate4.0版本前这样获取sessionFactory = configuration.buildSessionFactory();
// hibernate5以后规定,所有的配置或服务,要生效,必须配置或服务注册到一个服务注册类(服务构建器-->服务注册器)
ServiceRegistry serviceRegistry = cfg.getStandardServiceRegistryBuilder().build();
// 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
} catch (Exception e) {
System.err.println("创建会话工厂失败");
e.printStackTrace();
}
}
/**
* 获取Session
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); //获取ThreadLocal中当前线程共享变量的值。
if (session == null || !session.isOpen()) {
if (sessionFactory == null) { //如果会话工厂创建失败为空就在重新创建一次
rebuildSessionFactory();
}
//创建Sqlsession数据库会话
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
//设置ThreadLocal中当前线程共享变量的值。
threadLocal.set(session);
}
return session;
}
/**
* 重建会话工厂
*/
public static void rebuildSessionFactory() {
try {
// 加载Hibernate配置文件
Configuration cfg = new Configuration().configure();
// 创建会话工厂
// hibernate4.0版本前这样获取sessionFactory = configuration.buildSessionFactory();
// hibernate5以后规定,所有的配置或服务,要生效,必须配置或服务注册到一个服务注册类(服务构建器-->服务注册器)
ServiceRegistry serviceRegistry = cfg.getStandardServiceRegistryBuilder().build();
// 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
} catch (Exception e) {
System.err.println("创建会话工厂失败");
e.printStackTrace();
}
}
/**
* 获取SessionFactory对象
* @return SessionFactory对象
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* 关闭Session
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
//使用set(null)来回收ThreadLocal设置的值.
threadLocal.set(null);
if (session != null) {
session.close();//关闭Session
}
}
}