Hi,
I got this exception while I insert records.
org.springframework.dao.DataIntegrityViolationException: Hibernate flushing: Could not execute JDBC batch update; SQL [insert into emsis.tcom (COM_MAT_NAME,CLIENT, COM_MAT_ID) values (?, ?, ? )]; Field 'BRAND_ID' doesn't have a default value; nested exception is java.sql.BatchUpdateException: Field 'BRAND_ID' doesn't have a default valueat org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:249)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.orm.hibernate3.HibernateTransactionManager.convertJdbcAccessException(HibernateTransactionManager.java:806)
at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:792)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:665)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy131.add(Unknown Source)
at org.clss.emsis.controller.CaptionDetailsController.saveCaption(CaptionDetailsController.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
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.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.sql.BatchUpdateException: Field 'BRAND_ID' doesn't have a default value at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2007)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1443)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeBatch(DelegatingPreparedStatement.java:231)
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.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:657)
... 42 more
Here my POJO class
Code:
import java.util.Date;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Tcom generated by hbm2java
*/
@Entity
@Table(name="tcom"
,catalog="emsis"
)
public class Tcom implements java.io.Serializable {
private TcomId id;
private Tbrand tbrand;
private String comMatName;
public Tcom() {
}
public Tcom(TcomId id, Tbrand tbrand) {
this.id = id;
this.tbrand = tbrand;
}
public Tcom(TcomId id, Tbrand tbrand, String comMatName) {
this.id = id;
this.tbrand = tbrand;
this.comMatName = comMatName;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name="client", column=@Column(name="CLIENT", nullable=false) ),
@AttributeOverride(name="comMatId", column=@Column(name="COM_MAT_ID", nullable=false) ) } )
public TcomId getId() {
return this.id;
}
public void setId(TcomId id) {
this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns( {
@JoinColumn(name="BRAND_ID", referencedColumnName="BRAND_ID", nullable=false, insertable=false, updatable=false),
@JoinColumn(name="CLIENT", referencedColumnName="CLIENT", nullable=false, insertable=false, updatable=false) } )
public Tbrand getTbrand() {
return this.tbrand;
}
public void setTbrand(Tbrand tbrand) {
this.tbrand = tbrand;
}
@Column(name="COM_MAT_NAME", length=80)
public String getComMatName() {
return this.comMatName;
}
public void setComMatName(String comMatName) {
this.comMatName = comMatName;
}
}
import javax.persistence.Column;
import javax.persistence.Embeddable;
/**
* TcomId generated by hbm2java
*/
@Embeddable
public class TcomId implements java.io.Serializable {
private int client;
private int comMatId;
public TcomId() {
}
public TcomId(int client, int comMatId) {
this.client = client;
this.comMatId = comMatId;
}
@Column(name="CLIENT", nullable=false)
public int getClient() {
return this.client;
}
public void setClient(int client) {
this.client = client;
}
@Column(name="COM_MAT_ID", nullable=false)
public int getComMatId() {
return this.comMatId;
}
public void setComMatId(int comMatId) {
this.comMatId = comMatId;
}
public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof TcomId) ) return false;
TcomId castOther = ( TcomId ) other;
return (this.getClient()==castOther.getClient())
&& (this.getComMatId()==castOther.getComMatId());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getClient();
result = 37 * result + this.getComMatId();
return result;
}
}
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Tbrand generated by hbm2java
*/
@Entity
@Table(name="tbrand"
,catalog="emsis"
)
public class Tbrand implements java.io.Serializable {
private TbrandId id;
private String brandName;
private int catGrpId;
private Set<Tcom> tcoms = new HashSet<Tcom>(0);
public Tbrand() {
}
public Tbrand(TbrandId id, int catGrpId) {
this.id = id;
this.catGrpId = catGrpId;
}
public Tbrand(TbrandId id, String brandName, int catGrpId, Set<Tcom> tcoms) {
this.id = id;
this.brandName = brandName;
this.catGrpId = catGrpId;
this.tcoms = tcoms;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name="brandId", column=@Column(name="BRAND_ID", nullable=false) ),
@AttributeOverride(name="client", column=@Column(name="CLIENT", nullable=false) ) } )
public TbrandId getId() {
return this.id;
}
public void setId(TbrandId id) {
this.id = id;
}
@Column(name="BRAND_NAME", length=80)
public String getBrandName() {
return this.brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
@Column(name="CAT_GRP_ID", nullable=false)
public int getCatGrpId() {
return this.catGrpId;
}
public void setCatGrpId(int catGrpId) {
this.catGrpId = catGrpId;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="tbrand")
public Set<Tcom> getTcoms() {
return this.tcoms;
}
public void setTcoms(Set<Tcom> tcoms) {
this.tcoms = tcoms;
}
}
import javax.persistence.Column;
import javax.persistence.Embeddable;
/**
* TbrandId generated by hbm2java
*/
@Embeddable
public class TbrandId implements java.io.Serializable {
private int brandId;
private int client;
public TbrandId() {
}
public TbrandId(int brandId, int client) {
this.brandId = brandId;
this.client = client;
}
@Column(name="BRAND_ID", nullable=false)
public int getBrandId() {
return this.brandId;
}
public void setBrandId(int brandId) {
this.brandId = brandId;
}
@Column(name="CLIENT", nullable=false)
public int getClient() {
return this.client;
}
public void setClient(int client) {
this.client = client;
}
public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof TbrandId) ) return false;
TbrandId castOther = ( TbrandId ) other;
return (this.getBrandId()==castOther.getBrandId())
&& (this.getClient()==castOther.getClient());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getBrandId();
result = 37 * result + this.getClient();
return result;
}
}
and my controller method
Code:
@RequestMapping("/saveCaption")
public @ResponseBody
String saveCaption(@RequestParam(value = "caption_name") String caption_name,
@RequestParam(value = "brand_id") String brand_id) {
String ret = "E";
try {
List<Tbrand> list = tbrandDao.findAll();
Tbrand tbrand = new Tbrand();
for (Iterator<Tbrand> it = list.iterator(); it.hasNext();) {
Tbrand tbrand1 = it.next();
if (tbrand1.getId().getBrandId() == Integer.parseInt(brand_id)) {
tbrand.setId(tbrand1.getId());
}
}
Tcom tcom = new Tcom();
TcomId tcomId = new TcomId(800, 1);
tcom.setId(tcomId);
tcom.setComMatName(caption_name);
tcom.setTbrand(tbrand);
tcomDao.add(tcom);
ret = "S";
} catch (Exception ex) {
ex.printStackTrace();
} finally {
return ret;
}
}
Field 'BRAND_ID' doesn't have a default value