简体   繁体   English

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

[英]“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." 我收到“ java.sql.SQLException:表METAL_HAREKET_IPTAL中的列ID不允许为空值”。 on Sysbase database. 在Sysbase数据库上。 However the value that I want to insert exists (you can see from the Tomcat's log below) 但是,我要插入的值存在(您可以从下面的Tomcat日志中看到)

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: 编辑:MetalHareket的相关休眠代码是:

<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: 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;
    }

hibernate mapping of metalheraketiptal: 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: 金属的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;
}

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. 支持DB2,MySQL,MS SQL Server,Sybase和HypersonicSQL中的标识列。 The returned identifier is of type long, short or int. 返回的标识符的类型为long,short或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. 在这种情况下,元素不会生成主键,并且在Java中,需要在调用save()方法之前设置主键值。

If you want the application to assign identifiers, as opposed to having Hibernate generate them, you can use the assigned generator. 如果您希望应用程序分配标识符,而不是让Hibernate生成标识符,则可以使用分配的生成器。

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-id-enhanced-optimizers 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 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. 我当时依靠Sybase来生成ID列,但随后尝试在应用程序中设置ID元素,这当然没有任何意义。

The column ID in table METAL_HAREKET_IPTAL does not allow null values. 表METAL_HAREKET_IPTAL中的列ID不允许为空值。 You might be setting null value check it once. 您可能设置了空值,请检查一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.sql.SQLException:无法将值NULL插入列 - java.sql.SQLException: Cannot insert the value NULL into column Hibernate/SQL SERVER java.sql.SQLException:无法将 NULL 值插入具有默认值的列 - Hibernate/SQL SERVER java.sql.SQLException: Cannot insert NULL values into column that has default value java.sql.SQLException:没有这样的列 - java.sql.SQLException: no such column java.sql.SQLException:连接为空 - java.sql.SQLException: Connection is null SQL 状态 [null]; 错误代码 [17004]; 列类型无效; 嵌套异常是 java.sql.SQLException: Invalid column type - SQL state [null]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type java.sql.SQLException:没有此类值与我尝试插入的值 - java.sql.SQLException: no such column with the value I'm trying to insert java.sql.SQLException:表“ CONNECTIONS”没有自动生成的名为“ connection_id”的列 - java.sql.SQLException: Table 'CONNECTIONS' does not have an auto-generated column named 'connection_id' java.sql.sqlException:没有这样的列:employeeID - java.sql.sqlException:no such column: employID java.sql.SQLException:找不到列错误? - java.sql.SQLException: Column not found Error? 找不到java.sql.sqlexception列 - java.sql.sqlexception column not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM