hibernate web工程,运行报错 NoSuchMethodError CoreMessageLogger.debugf

在java工程中使用hibernate.cfg.xml文件和User.hbm.xml文件,测试运行没有问题,可以写入到数据库表中,但是在web工程中,使用hibernate就报错,运行提示:

java.lang.NoSuchMethodError: org.hibernate.internal.CoreMessageLogger.debugf(Ljava/lang/String;I)V     at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:34)     at org.hibernate.engine.jdbc.connections.internal.Po

1、调整了jar包的配置,也不行。

2、还提示HttpServletRequest无法识别。

3、关闭工程,删除了web-inf/lib下jsr 303中关于hibernate验证框架的jar包。然后重新引用servlet api的jar包,运行成功。

MyEclipse中出现“HttpServletRequest cannot be resolved to a type”的错误

2014年03月02日 13:13:45 hpyon 阅读数:5126

出现这种问题主要是项目中少了servlet包,全名为servlet-api.jar,这个包是可以在tomcat中找到的,所以

1.右击项目,单击Build Path

2.选中add external archives,找到安装放tomcat的文件夹,

   并且找到其中的lib文件夹,servlet-api.jar就在里面,选中确认就可以啦

 

估计是jsr303中的hibernate验证框架与引用的myeclipse自带的hibernate 5.1的包冲突造成。

 

package com.jxq.test;

import org.hibernate.Session;
import org.junit.Test;

import com.jxq.common.HibernateUtils;
import com.jxq.model.User;

public class test01 {

    @Test  //使用junit 将该方法当做java工程运行
    public void aa(){
        System.out.println("jalsdjflajsdfl;a");
        Session ss=null;
        try {
            ss=HibernateUtils.openSession();
            ss.beginTransaction();
            User u=new User();
            u.setUsername("ljlj");
            u.setUserpass("123");
            u.setNickname("捡垃圾");
            u.setEmail("[email protected]");
            ss.save(u);
            ss.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            ss.getTransaction().rollback();
        }finally{
            if(ss!=null) ss.close();
        }
    }
}
 

 

package com.jxq.common;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.cfgxml.spi.CfgXmlAccessService;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html }.
 */
public class HibernateUtils {

    /** 
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses  
     * #resourceAsStream style lookup for its configuration file. 
     * The default classpath location of the hibernate config file is 
     * in the default package. Use #setConfigFile() to update 
     * the location of the configuration file for the current session.   
     */
    private final static SessionFactory sf=buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        System.out.println("create hibernate factory...............");
        try {
/*            //hibernate 4.3之前用这种方式创建sessionfactory,  未进行实际验证,好像没有StandardServiceRegistryBuilder对象
            Configuration cfg=new Configuration().configure("/hibernate.cfg.xml");
            System.out.println("create hibernate factory   111   ...............");
            ServiceRegistry svr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
            System.out.println("create hibernate factory   2222   ...............");
            SessionFactory factory=cfg.buildSessionFactory(svr);
            System.out.println("create hibernate factory   success   ...............");
            return factory;*/

            //4.3之后,用这种方式创建sessionfactory,否则用上面的方式创建,运行时也能创建成功,但是保存时候会提示org.hibernate.MappingException: Unknown entity: com.jxq.model.User
            return new Configuration().configure().buildSessionFactory();

        } catch (Exception e) {
            System.out.println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
        }
        return null;
    }

    public static Session openSession(){
        return sf.openSession(); 
    }

    public static void closeSession(Session session) throws HibernateException {
        if (session != null) session.close();
    }
}

hibernate jar包用5.1版本的,但是创建sessionfactory用4.3之前的,也能提示创建成功,但是调用save方法的时候,提示找不到实体类。如下图。

package com.jxq.model;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import com.sun.xml.internal.bind.v2.model.core.ID;

public class User {
    private int id;   //忘记该属性了,报mapping 错误,Unknown entity:
    
    //@NotEmpty(message="用户名不能为空")   测试时,暂时注释掉
    private String username;
    
    //@Length(min=6,message="密码不低于6位")
    private String userpass;
    
    private String nickname;
    
    //@Email(message="邮箱格式不正确")
    private String email;
    
    public User() {
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserpass() {
        return userpass;
    }
    public void setUserpass(String userpass) {
        this.userpass = userpass;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    
    
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public User(String username, String userpass, String nickname, String email) {
        super();
        this.username = username;
        this.userpass = userpass;
        this.nickname = nickname;
        this.email = email;
    }
    
}
 

 

User.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-mapping package="com.jxq.model">
    <class name="User" table="t_user"> 
        <id name="id" column="id">
            <generator class="native"></generator>
        </id>
        <property name="username"  />
        <property name="userpass"  />
        <property name="nickname"  />

<property name="email"  />
    </class>    
    
</hibernate-mapping>

 

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">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myhib</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        
        <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
        
        <mapping resource="com/jxq/model/User.hbm.xml" />
    </session-factory>

</hibernate-configuration>

运行成功结果图。

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章