简体   繁体   中英

“java.sql.SQLException The column .. in table .. does not allow null values” although column value is not null

I am getting "java.sql.SQLException: The column ID in table METAL_HAREKET_IPTAL does not allow null values." on Sysbase database. However the value that I want to insert exists (you can see from the Tomcat's log below)

metalhareket id: 450
metalhareketiptal id: 450
org.hibernate.exception.ConstraintViolationException: could not insert: [com.turktrust.iab.ebs.pojos.MetalHareketIptal]
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
        at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
        at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
        at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
        at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
        at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
        at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
        at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
        at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
        at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
        at com.turktrust.iab.ebs.service.VirmanIptalOnayService.virmanIptal(VirmanIptalOnayService.java:239)
        at com.turktrust.iab.ebs.backingbeans.VirmanIptalOnayIslemleriBean.virmanIptal(VirmanIptalOnayIslemleriBean.java:101)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:329)
        at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:342)
        at org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
        at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
        at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
        at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153)
        at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
        at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:772)
        at javax.faces.component.UICommand.broadcast(UICommand.java:300)
        at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
        at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:662)
Caused by: java.sql.SQLException: The column ID in table METAL_HAREKET_IPTAL does not allow null values.

        at com.sybase.jdbc4.jdbc.SybConnection.getAllExceptions(Unknown Source)
        at com.sybase.jdbc4.jdbc.SybStatement.handleSQLE(Unknown Source)
        at com.sybase.jdbc4.jdbc.SybStatement.nextResult(Unknown Source)
        at com.sybase.jdbc4.jdbc.SybStatement.nextResult(Unknown Source)
        at com.sybase.jdbc4.jdbc.SybStatement.updateLoop(Unknown Source)
        at com.sybase.jdbc4.jdbc.SybStatement.executeUpdate(Unknown Source)
        at com.sybase.jdbc4.jdbc.SybPreparedStatement.executeUpdate(Unknown Source)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
        at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
        at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
        ... 48 more

Related code is as follows:

    System.out.println("metalhareket id: " + mh.getId().toString());
    session = HibernateUtil.getSessionFactory().openSession();
    tx = session.beginTransaction();
    mh.setDurum(EBSConstants.DURUM_BOOL_PASIF);

    session.update(mh);
    session.flush();

    Date d = new Date();

    MetalHareketIptal mhi = new MetalHareketIptal();
    mhi.setKullanici(kullanici);
    mhi.setMetalHareket(mh);
    mhi.setNeden(iptalNedeni);
    mhi.setTarih(d);
    mhi.setId(mh.getId());
    System.out.println("metalhareketiptal id: " + mhi.getId().toString());
    session.flush();
    session.save(mhi);

    Takas takas = mh.getTakas();
    BigDecimal geriMiktar = takas.getKalanMiktar().add(mh.getMiktar());
    takas.setKalanMiktar(geriMiktar);
    takas.setTakasDurum(EBSConstants.TAKAS_DURUM_AKTIF);

    session.update(takas);

EDIT: related hibernate code for MetalHareket is:

<hibernate-mapping>
  <class name="com...MetalHareket" table="METAL_HAREKET">
    <id name="id" type="java.lang.Long">
      <column name="ID"/>
      <generator class="identity"/>
    </id>

pojo of MetalHareket:

public class MetalHareket implements java.io.Serializable {

    private Long id;
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

hibernate mapping of metalheraketiptal:

   <hibernate-mapping>
      <class name="com....pojos.MetalHareketIptal" table="METAL_HAREKET_IPTAL">
        <id name="id" type="java.lang.Long">
          <column name="ID"/>
          <generator class="identity"/>
        </id>

pojo of metalhareketiptal:

public class MetalHareketIptal  implements java.io.Serializable {

private Long id;
 public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

The problem with the above code was the hibernate file.
Instead of this:

<generator class="identity"/>

supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.

It should've been this:

<generator class="assigned"/>

In this case element do not generate the primary key and in Java it is required to set the primary key value before calling save() method.

If you want the application to assign identifiers, as opposed to having Hibernate generate them, you can use the assigned generator.

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-id-enhanced-optimizers

http://www.roseindia.net/hibernate/hibernateidgeneratorelement.shtml

I was relying on Sybase to generate the ID column but then I was trying to set the ID element in my application, which of course does not make any sense.

The column ID in table METAL_HAREKET_IPTAL does not allow null values. You might be setting null value check it once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM