No CurrentSessionContext configured!" 异常解决方案

    技术2022-05-11  67

    hibernate 老说没有配方言

    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>这句话明明就写在了配置文件里面,可老是没有写错误如下:Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set

    name改成"hibernate.dialect"也不行

    Hibernate-Version: 3.1.1mysql -Verison: 4.0.16

    用这种方法正确static {

    try {configuration = new Configuration();

    sessionFactory = configuration.configure().buildSessionFactory();} catch (Throwable ex) {

     

    throw new ExceptionInInitializerError(ex);}}

    这种方式会出现异常// static {// try {// // Create a configuration based on the properties file we've put// Configuration config = new Configuration();// config.addClass(Customer.class).addClass(Order.class);// // Get the session factory we can use for persistence// sessionFactory = config.buildSessionFactory();// } catch (Exception e) {// e.printStackTrace();// }// }

    第二种方式是针对使用properties文件配置hiernate的写法,使用hibernate.cfg.xml应使用第一种调用方式

    或者在hibernate.cfg.xml中加入:

     

    < property  name ="current_session_context_class" > thread </ property >

     


    最新回复(0)