"'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
现在可以运行了:)
我的程序是这样的,有个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
现在可以运行了:)
相关文章推荐
- 如何解决“不是可以识别的 函数名“的问题
- Xcode的"Could not insert new outlet connection"问题解决方法
- 解决Xcode 4.3.2的"Could not insert new outlet connection"问题
- oracle 临时表 解决 "表 *** 发生了变化,触发器/函数不能读"的问题
- ios开发---解决Xcode的"Could not insert new outlet connection"问题
- 解决Xcode 4.3.2的"Could not insert new outlet connection"问题
- 面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用就可以了;面向对象是把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的行为(转)
- 解决Xcode 4.3.2的"Could not insert new outlet connection"问题
- 解决Xcode 4.3.2的"Could not insert new outlet connection"问题
- "tabhost requires a tabwidget with id..."问题解决
- "error: failed to attach to process ID 0"问题解决
- last_insert_id使用及其问题解决方案
- 解决Xcode 的"Could not insert new outlet connection"问题
- "error: failed to attach to process ID 0"问题解决
- SQLserver不是可以识别的内置函数名称"的错误,通过set statistics time on能得到SQL语句的执行时间精确到毫秒.
- SQLserver不是可以识别的内置函数名称"的错误,通过set statistics time on能得到SQL语句的执行时间精确到毫秒.
- 独立安装SharePoint 2013碰到的"SDDL"问题及解决方法
- "The MathType DLL cannot be found.Please reinstall MathType"问题解决
- "备份/恢复"面临的问题与解决途径
- "Base SDK Missing"问题的解决