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>

運行成功結果圖。

 

 

 

 

 

 

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