Topic: 请问hibernate查询数据库错误的问题

  Print this page

1.请问hibernate查询数据库错误的问题 Copy to clipboard
Posted by: nykey
Posted on: 2005-03-06 00:19

这个是AccountBean文件
package org.jteam.cms.dao;

import java.util.Iterator;
import net.sf.hibernate.*;
import org.jteam.cms.domain.Account;

/**
*和Account相关的业务逻辑
*/
public class AccountBean extends HibernateBase
{
  
  public AccountBean()throws HibernateException
  {
    super();
  }
  /**
   *增加一个Account
   */
  public void addAccount(Account st)throws HibernateException
  {
     beginTransaction();
session.save(st);
endTransaction(true);
}

/**
*返回系统中所有的Account
*/
public Iterator getAllAccount()throws HibernateException
{
  String queryString = "select AccountName from Account as account";
beginTransaction();
Query query = session.createQuery(queryString);
Iterator it= query.iterate();
return it;
}

/**
*删除给定ID的Account
*/
public void deleteAccount(String id)throws HibernateException
{
  beginTransaction();   
  Account course=(Account)session.load(Account.class,id);   
  session.delete(course);
  endTransaction(true);
}

/**
*按Account的名字进行模糊查找
*/
public Iterator getSomeAccount(String name)throws HibernateException
{
  String queryString = "select AccountName from Account as a where a.AccountName like :name" ;
beginTransaction();
Query query = session.createQuery(queryString);
query.setString("name", "%"+name+"%");
Iterator it= query.iterate();
return it;
}   
}
这个是HibernateBase文件
package org.jteam.cms.dao;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

public abstract class HibernateBase
{
  protected SessionFactory sessionFactory;//会话工厂,用于创建会话
protected Session session;//hibernate会话
protected Transaction transaction; //hiberante事务

public HibernateBase()throws HibernateException
{
  this.initHibernate();
}
// 帮助方法
protected void initHibernate()
throws HibernateException {

// 装载配置,构造SessionFactory对象
sessionFactory = new Configuration().configure().buildSessionFactory();
}

/**
*开始一个hibernate事务
*/
protected void beginTransaction()
throws HibernateException {

session = sessionFactory.openSession();
transaction = session.beginTransaction();
}

/**
*结束一个hibernate事务。
*/
protected void endTransaction(boolean commit)
throws HibernateException {

if (commit) {
transaction.commit();
} else {
//如果是只读的操作,不需要commit这个事务。
transaction.rollback();
}
session.close();
}
}
hibernate。cfg。xml文件如下
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>
    <property name="connection.datasource">java:comp/env/jdbc/cms</property>
<property name="show_sql">false</property>
    <property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>

<!-- Mapping files -->
<mapping resource="org/jteam/cms/domain/Account.hbm.xml"/>

</session-factory>

</hibernate-configuration>
Account。hbm。xml文件如下
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="org.jteam.cms.domain.Account" table="Account">
<id name="aid" column="AID">
<generator class="increment" />
</id>
<property name="accountname" column="AccountName" />
<property name="password" column="Password" />
</class>
</hibernate-mapping>
连接数据库是通过配置tomcat数据库的连接池来进行连接的
测试代码如下
<%@ page import="java.util.*" errorPage="error.jsp"%>

<jsp:useBean id="account" class="org.jteam.cms.domain.Account" scope="page">

<jsp:setProperty name="account" property="accountname" value="nykey"/>
<jsp:setProperty name="account" property="password" value="lin2001"/>

</jsp:useBean>

<jsp:useBean id="accountBusiness" class="org.jteam.cms.dao.AccountBean" scope="page"/>

<html><body><center>
<br>::增加一个course::<br>

<%

try

{

if(account.getAid()==1);

else {
//  accountBusiness.addAccount(account);
}



%>

成功添加了Course:<br>

name:<%=account.getAccountname()%>

Id:<%=account.getAid()%>

<%

}

catch(Exception e)

{
out.println(e.getMessage());
}

%>

<hr>

::按名字模糊查找::<br>
<%
try{
  accountBusiness.getSomeAccount("nykey");
}
catch(Exception e)
{
  out.print(e.getMessage());
}%>

<hr>

::删除一个Course::<br>
<%
try{
  accountBusiness.deleteAccount("2");
}
catch(Exception e)
{
  out.print(e.getMessage());
}%>

<hr>

<br>
<%
try{
  Iterator it=accountBusiness.getAllAccount();
  while(it.hasNext())
  {
    account=(org.jteam.cms.domain.Account)it.next();
  }
}
catch(Exception e)
{
  out.print(e.getMessage());
}
%>

</body>

</html>
问题是:可以执行添加功能,可一旦进行查找马上就会报错:net.sf.hibernate.QueryException: undefined alias: AccountName [select AccountName from account where aid=1]
按照字面上来理解的意思是说数据库没有这个AccountName这个字段,但是检查一下明明就有,而且如果没有的话又怎么可能插入数据呢?实在是有点想不通
麻烦哪位大侠帮我检查一下,是否哪里出了纰漏,告知一下,在此先谢过了!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923