“java.sql.SQLException表中的列..不允许空值”,尽管列值不为空

时间:2012-04-19 14:31:39

标签: java hibernate sybase

我得到" java.sql.SQLException:表METAL_HAREKET_IPTAL中的列ID不允许空值。"在Sysbase数据库上。但是我想要插入的值存在(您可以从下面的Tomcat' s日志中看到)

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

相关代码如下:

    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);

编辑: MetalHareket的相关hibernate代码是:

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

MetalHareket的pojo:

public class MetalHareket implements java.io.Serializable {

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

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

metalheraketiptal的hibernate映射:

   <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>

metalhareketiptal的pojo:

public class MetalHareketIptal  implements java.io.Serializable {

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

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

2 个答案:

答案 0 :(得分:1)

上面代码的问题是hibernate文件 而不是:

<generator class="identity"/>
  

支持DB2,MySQL,MS SQL Server,Sybase和中的标识列   HypersonicSQL。返回的标识符的类型为long,short或int。

应该是这样的:

<generator class="assigned"/>
  

在这种情况下,元素不会生成主键和   在Java中,需要在调用之前设置主键值   save()方法。

     

如果您希望应用程序分配标识符,而不是   让Hibernate生成它们,你可以使用指定的生成器。

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

我依靠Sybase来生成ID列,但后来我试图在我的应用程序中设置ID元素,这当然没有任何意义。

答案 1 :(得分:0)

表METAL_HAREKET_IPTAL中的列ID不允许空值。您可能正在设置空值检查一次。