我的DAO层代码如下:
package com.app.dao;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.app.pojo.InterviewerPojo;
@Repository("interviewer_details_dao")
public class InterviewerDetailsDaoImpl implements InterviewerDetailsDao {
@Resource(name="sessionFactory")
private SessionFactory factory;
/*private HibernateTemplate template;
public void setSessionFactory(){
template = new HibernateTemplate(factory);
}*/
/*private HibernateTemplate template = new HibernateTemplate(sessionFactory);*/
@Override
public void saveInterviewDetails(InterviewerPojo interviewerPojo) {
/* Session sess = factory.getCurrentSession();
try{
sess.getTransaction().wasCommitted();
Transaction trans = sess.beginTransaction();
System.out.println("a");
sess.saveOrUpdate(interviewerPojo);
sess.getTransaction().commit();}
catch(Exception e){
throw e;
}*/
System.out.println("b");
factory.getCurrentSession().save(interviewerPojo);
/* System.out.println(interviewerPojo);
template.saveOrUpdate(interviewerPojo);
System.out.println("b");*/
System.out.println("done");
}
}
如果我按以下方式从数据库中获取值,则运行与上面相同的代码 sessionFactory.getCurrentSession().createQuery("select m from emp m).list; 但是当我编写另一个页面来插入值时,它就有问题 *如您所见,我使用了相同的概念,我通过上面的查询* 从数据库中获取的另一个模型(pojo)填充了模型(POJO)类,现在调试到“template.saveOrUpdate”并抛出如下异常
org.hibernate.exception.SQLGrammarException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 org.hibernate.engine 的 org.hibernate.exception.internal.SQLStateConverter.convert(SQLStateConverter.java:100) 的第 1 行的“休眠序列的值”附近使用正确的语法.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) 在 org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) 在 org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler .continueInvocation(AbstractStatementProxyHandler.java:129) at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) at $Proxy24.executeQuery(Unknown Source) at org.hibernate.id.SequenceGenerator。407) 在 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 在 org.apache.tomcat.util.net 的 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) .JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread。 run(Unknown Source) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; 检查与您的 MySQL 服务器版本相对应的手册,以在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 的 sun.reflect 的第 1 行的“hibernate_sequence 的值”附近使用正确的语法。
POJO源代码:
package com.app.pojo;
import java.io.Serializable;
import javax.persistence.*;
@SuppressWarnings("serial")
@Entity @Table(name="interviewer")
public class InterviewerPojo implements Serializable {
private int eid;
private int vid;
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
private int i_eid;
private String i_name;
private String password;
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
}
休眠配置:
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.formatsql">true</prop>
<prop key="hibernate.showsql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>