Hibernate开发常见问题总结

1.

There is no Action mapped for namespace [/] and action name [insertMess] associated with context path [/NeverForget].
Hibernate开发常见问题总结_第1张图片

解决:
Hibernate开发常见问题总结_第2张图片
注意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)
Hibernate开发常见问题总结_第3张图片
解决: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>          
           
        <property name="hibernate.show_sql">true property>   
           
        <property name="jdbc.fetch_size">50 property>   
           
        <property name="jdbc.batch_size">23 property>   
           
        <property name="jdbc.use_scrollable_resultset">false property>   
           
        <property name="Connection.useUnicode">true property>   
           
    <property name="connection.characterEncoding">gbk property>        
        
<property name="hibernate.current_session_context_class">threadproperty>
           
          <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect property>   
                  
          <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=?]
Hibernate开发常见问题总结_第4张图片

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
	        }
	    }
}

你可能感兴趣的:(java,hibernate,数据库,mysql)