1

我收到“java.sql.SQLException:表 METAL_HAREKET_IPTAL 中的列 ID 不允许空值”。在 Sysbase 数据库上。但是我要插入的值存在(您可以从下面的 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

相关代码如下:

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

metalharektiptal 的 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;
}
4

2 回答 2

1

上面代码的问题是休眠文件。
而不是这个:

<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 元素,这当然没有任何意义。

于 2012-04-24T11:12:16.813 回答
0

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

于 2012-04-19T16:42:31.920 回答