当前位置:编程学习 > JAVA >>

myeclipse连接mysql数据库,hibernate出错?

出错:
严重: Servlet.service() for servlet action threw exception
java.sql.SQLException: Unknown column 'this_.id' in 'field list'
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2901)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1584)
at com.mysql.jdbc.Connection.serverPrepare(Connection.java:4932)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1312)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1284)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1577)
at org.hibernate.loader.Loader.doQuery(Loader.java:696)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2232)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
at org.hibernate.loader.Loader.list(Loader.java:2124)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
at com.qdu.sun.hibernate.UserDAO.findByExample(UserDAO.java:67)
at com.qdu.sun.BO.LoginBO.validate(LoginBO.java:20)
at com.qdu.sun.struts.action.LoginAction.execute(LoginAction.java:43)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.qdu.sun.BO.FormFilter.doFilter(FormFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_ from user this_ where (this_.username=? and this_.password=?)
Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_ from user this_ where (this_.username=? and this_.password=?)
Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_ from user this_ where (this_.username=? and this_.password=?)
Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_ from user this_ where (this_.username=? and this_.password=?)





代码:
LoginBO.java

package com.qdu.sun.BO;

import java.sql.*;
import java.util.List;
import org.hibernate.Transaction;
import com.qdu.sun.hibernate.*;


public class LoginBO {

public boolean validate(String username, String password) {
// 实例化 DAO
UserDAO dao = new UserDAO();
// 打开事务
Transaction tran = dao.getSession().beginTransaction();
// 生成持久化对象
User user = new User();
user.setUsername(username);
user.setPassword(password);
List list = dao.findByExample(user);
        tran.commit();
        
if (list.size() > 0) {

return true;

} else {
return false;

}
}
}



HibernateSessionFactory.java
package com.qdu.sun.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * 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 HibernateSessionFactory {

    /** 
     * 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 static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

static {
     try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
    }
    private HibernateSessionFactory() {
    }

/**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

        return session;
    }

/**
     *  Rebuild hibernate session factory
     *
     */
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

/**
     *  return session factory
     *
     */
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
     *  return session factory
     *
     * session factory will be rebuilded in the next call
     */
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
     *  return hibernate configuration
     *
     */
public static Configuration getConfiguration() {
return configuration;
}

}



User.java
package com.qdu.sun.hibernate;

/**
 * User entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class User implements java.io.Serializable {

// Fields

private Integer id;
private String username;
private String password;

// Constructors

/** default constructor */
public User() {
}

/** full constructor */
public User(String username, String password) {
this.username = username;
this.password = password;
}

// Property accessors

public Integer getId() {
return this.id;
}

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

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

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

public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

}


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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.qdu.sun.hibernate.User" table="user" >
       <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="username" type="java.lang.String">
            <column name="username" length="20" not-null="true" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="20" not-null="true" />
        </property>
    </class>
</hibernate-mapping>



数据库截图:

MySQL Hibernate MyEclipse --------------------编程问答-------------------- findByExample这个函数也看看 --------------------编程问答-------------------- java.sql.SQLException: Unknown column 'this_.id' in 'field list'

这个列不存在this_.id --------------------编程问答-------------------- 为什么??? Unknown column 'this_.id' in 'field list'
findByExample这个函数没问题吧
--------------------编程问答-------------------- user.setPassword(password);
List list = dao.findByExample(user);//这时候数据库应该不存在user表,因为事务还没有提交。报这个错误很正常。
        tran.commit();

比较表名大小写,字段名大小写是否都正确。 --------------------编程问答-------------------- 检查user表没错啊,我之前觉得是属性id这里出错了,重新设置成自动生成的也还是错的 --------------------编程问答-------------------- 解决了这个问题了,不过页面跳转的时候出错了。 --------------------编程问答-------------------- 纠结了那么久,我终于成功了,我做出来了!然后谢谢各位帮我这只菜鸟这么多 --------------------编程问答-------------------- -----问题原因? --------------------编程问答-------------------- 有两个问题:(1)struts-config.xmlz中添加Forward时把fail.jsp写成了failed.jsp (2)没有在MySQL中插入记录,就不能登录

--------------------编程问答--------------------
引用 9 楼 daisy1799 的回复:
有两个问题:(1)struts-config.xmlz中添加Forward时把fail.jsp写成了failed.jsp (2)没有在MySQL中插入记录,就不能登录

如果User表存在,仅仅只是没有数据的话,就报Unknown column 'this_.id' in 'field list'不合逻辑啊,如果表中该column不存在,报Unknown column才合逻辑 --------------------编程问答-------------------- 页面不对,会包404错误,没值会包空指针,你这都不相关。
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,