您的位置:首页 > 编程语言 > Java开发

"'last_insert_id' 不是可以识别的 函数名"的问题的解决

2007-01-22 09:51 525 查看
昨天好郁闷,碰到了个流氓问题, "'last_insert_id' 不是可以识别的 函数名",改了很多代码,都不行;

我的程序是这样的,有个Student类,对应的ADO是StudentADO,当用户一登陆系统,便向SQL Server2000数据库插入一个用户;

Student类:

package hqq.db;

import org.apache.struts.validator.ValidatorActionForm;

 

/**
 * Student generated by MyEclipse - Hibernate Tools
 */

public class Student extends ValidatorActionForm implements java.io.Serializable {

    // Fields   

     private Integer stdId;
     private String stdName;
     private String stdPwd;
    
 /** default constructor */
    public Student() {
    }

   
    /** full constructor */
    public Student(String stdName, String stdPwd) {
        this.stdName = stdName;
        this.stdPwd = stdPwd;
    }

  
    // Property accessors

    public Integer getStdId() {
        return this.stdId;
    }
   
    public void setStdId(Integer stdId) {
        this.stdId = stdId;
    }

    public String getStdName() {
        return this.stdName;
    }
   
    public void setStdName(String stdName) {
        this.stdName = stdName;
    }

    public String getStdPwd() {
        return this.stdPwd;
    }
   
    public void setStdPwd(String stdPwd) {
        this.stdPwd = stdPwd;
    }
  

StudentADO:

package hqq.db;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Example;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Data access object (DAO) for domain model class Student.
 * @see hqq.db.Student
 * @author MyEclipse - Hibernate Tools
 */
public class StudentDAO extends HibernateDaoSupport {

    private static final Log log = LogFactory.getLog(StudentDAO.class);

    public static String cheshi = "成功调用StudentDAO";
   
  
    private SessionFactory sessionFactory;
   
 protected void initDao() {
  //do nothing
 }
   
    public void save(Student stu) {//主要调用这个方法
        log.debug("saving Student instance");
        try {
         System.out.println("do save student");
         sessionFactory = this.getSessionFactory();
            Session session = sessionFactory.openSession();
            Transaction tx=session.beginTransaction();
   session.save(stu);
   tx.commit();
   session.close();
            log.debug("save Student successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
   
 public void delete(Student persistentInstance) {
        log.debug("deleting Student instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
   
    public Student findById( java.lang.Integer id) {
        log.debug("getting Student instance with id: " + id);
        try {
            Student instance = (Student) getHibernateTemplate()
                    .get("hqq.db.Student", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
   
   
    public List findByExample(Student instance) {
        log.debug("finding Student instance by example");
        try {
            List results = getSession()
                    .createCriteria("hqq.db.Student")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }   
   
    public List findStudent(Student std) {
        log.debug("finding Student instance by example");
        try {
         System.out.println("do findStudent");
         Session session = getSession();
         System.out.println("connect:"+session.isConnected());
         
         Transaction tx=session.beginTransaction();
         
         Query query = session.createQuery("from Student student where student.stdName=:name and student.stdPwd=:pwd");
            //Query query = session.createQuery("from Student student where student.stdName='"+std.getStdName()+"' and student.stdPwd='"+std.getStdPwd()+"'");
            query.setString("name",std.getStdName());
            query.setString("pwd",std.getStdPwd());
            Iterator it = query.iterate();
      while(it.hasNext()){
       Student stud = (Student)it.next();
       System.out.println(stud.getStdName());
      }
           
            List results = query.list();
           
//            Student stu1 = new Student();
//      stu1.setStdName("zhanghsan1");
//      stu1.setStdPwd("zhangshan1");
//      session.save(stu1);
//      
      tx.commit();
            System.out.println("size:"+results.size());
            session.close();
            return results;
        } catch (RuntimeException re) {
           System.out.println(re.getMessage());
            throw re;
        }
    }   
   
    public Student merge(Student detachedInstance) {
        log.debug("mergi
4000
ng Student instance");
        try {
            Student result = (Student) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Student instance) {
        log.debug("attaching dirty Student instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
   
    public void attachClean(Student instance) {
        log.debug("attaching clean Student instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

 public static StudentDAO getFromApplicationContext(ApplicationContext ctx) {
     return (StudentDAO) ctx.getBean("StudentDAO");
 }
}

 

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName">
   <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
  </property>
  <property name="url">
   <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ssh</value>
  </property>
  <property name="username">
   <value>aa</value>
  </property>
  <property name="password">
   <value>aa</value>
  </property>
 </bean>
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>hqq/db/Course.hbm.xml</value>
    <value>hqq/db/Student.hbm.xml</value>
   </list>
  </property>
 </bean>
 
 <bean id="CourseDAO" class="hqq.db.CourseDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <bean id="StudentDAO" class="hqq.db.StudentDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 
 
 <bean name="/login" class="hqq.struts.action.LoginAction">
  <property name="studao">
   <ref bean="StudentDAO" />
  </property>
 </bean>
</beans>

原来我使用的数据库的方言不对,应该是org.hibernate.dialect.SQLServerDialect

 

现在可以运行了:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐