使用hibernate在数据库中插入数据

时间:2017-05-25 12:40:56

标签: java spring hibernate

@Entity
@Table(name = "LOCATION")
public class Location {

@Id
@Column(name = "LOC_ID")
protected int locId;

@Column(name = "LOC_NAME")
protected String locName;

@Column(name = "LOC_TYPE")
protected String locType;

@Column(name = "PINCODE")
protected String pincode;

@Column(name = "SHIPPING_TYPE")
protected String shippingType;

@Column(name = "CONTACT")
protected String contact;

@Column(name = "PROCESS_ CODE")
protected String processCode;

@Column(name = "TRANSPORT")
protected String transport;

public Location() {

}

public Location(int locId) {
    this.locId = locId;
}

public int getLocId() {
    return locId;
}

public void setLocId(int locId) {
    this.locId = locId;
}

public String getLocName() {
    return locName;
}

public void setLocName(String locName) {
    this.locName = locName;
}

public String getLocType() {
    return locType;
}

public void setLocType(String locType) {
    this.locType = locType;
}

public String getPincode() {
    return pincode;
}

public void setPincode(String pincode) {
    this.pincode = pincode;
}

public String getShippingType() {
    return shippingType;
}

public void setShippingType(String shippingType) {
    this.shippingType = shippingType;
}

public String getContact() {
    return contact;
}

public void setContact(String contact) {
    this.contact = contact;
}

public String getProcessCode() {
    return processCode;
}

public void setProcessCode(String processCode) {
    this.processCode = processCode;
}

public String getTransport() {
    return transport;
}

public void setTransport(String transport) {
    this.transport = transport;
}

@Override
public String toString() {
    return "Location [locId=" + locId + ", locName=" + locName + ",             locType=" + locType + ", pincode=" + pincode
            + ", shippingType=" + shippingType + ", contact=" + contact +    ", processCode=" + processCode
            + ", transport=" + transport + "]";

控制器

@RequestMapping(value="/insertLoc",method=RequestMethod.POST)
public String saveLocation(@ModelAttribute("location")Location                   loc,ModelMap map){
    int locId=service.saveLocation(loc);
    map.addAttribute("message", "Location added with Location Id "+ locId);
    return "locReg";
}

异常

Hibernate: insert into LOCATION (CONTACT, LOC_NAME, LOC_TYPE, PINCODE, PROCESS_ CODE, SHIPPING_TYPE, TRANSPORT, LOC_ID) values (?, ?, ?, ?, ?, ?, ?, ?)
May 25, 2017 5:35:37 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/VendorApp] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: Could not execute JDBC batch update; SQL [insert into LOCATION (CONTACT, LOC_NAME, LOC_TYPE, PINCODE, PROCESS_ CODE, SHIPPING_TYPE, TRANSPORT, LOC_ID) values (?, ?, ?, ?, ?, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update] with root cause
java.sql.BatchUpdateException: ORA-00917: missing comma

    at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
    at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10657)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:407)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
    at com.vendor.app.dao.impl.LocationDaoImpl.saveLocation(LocationDaoImpl.java:21)
    at com.vendor.app.service.impl.LocationServiceImpl.saveLocation(LocationServiceImpl.java:20)
    at com.vendor.app.controller.LocationController.saveLocation(LocationController.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:2)

例外ORA-00917: missing comma表示您的查询中缺少逗号:

insert into LOCATION (CONTACT, LOC_NAME, LOC_TYPE, PINCODE, PROCESS_ CODE, SHIPPING_TYPE, TRANSPORT, LOC_ID) values (?, ?, ?, ?, ?, ?, ?, ?)

问题主要出在PROCESS_ CODE列之后的_列,因此它在查询中被评估为两个不同的列。

您应该在您应该替换的实体类中修复它:

@Column(name = "PROCESS_ CODE")
protected String processCode;

使用:

@Column(name = "PROCESS_CODE")
protected String processCode;

注意:

您最好将变量声明为private,这样您的代码就会遵循更好的封装规则。