Results 1 to 4 of 4

Thread: Problem inserting into database with hibernate in combination to spring services

  1. #1
    Join Date
    Oct 2006
    Posts
    2

    Default Problem inserting into database with hibernate in combination to spring services

    Hello,

    I have problems on using hibernate(EJB 3.0) in combination with spring services.

    I will insert a row in the database (Informix) and I get the following Exception:

    org.springframework.orm.hibernate3.HibernateSystem Exception: Unexpected row count: 0 expected: 1; nested exception is org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
    org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
    at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(N onBatchingBatcher.java:27)
    at org.hibernate.persister.entity.AbstractEntityPersi ster.update(AbstractEntityPersister.java:2268)
    at org.hibernate.persister.entity.AbstractEntityPersi ster.updateOrInsert(AbstractEntityPersister.java:2 180)
    at org.hibernate.persister.entity.AbstractEntityPersi ster.update(AbstractEntityPersister.java:2450)
    at org.hibernate.action.EntityUpdateAction.execute(En tityUpdateAction.java:91)
    at org.hibernate.engine.ActionQueue.execute(ActionQue ue.java:248)
    at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:232)
    at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:140)
    at org.hibernate.event.def.AbstractFlushingEventListe ner.performExecutions(AbstractFlushingEventListene r.java:297)
    at org.hibernate.event.def.DefaultFlushEventListener. onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.j ava:988)
    at org.springframework.orm.hibernate3.HibernateAccess or.flushIfNecessary(HibernateAccessor.java:394)
    at org.springframework.orm.hibernate3.HibernateTempla te.execute(HibernateTemplate.java:357)
    at org.springframework.orm.hibernate3.HibernateTempla te.saveOrUpdate(HibernateTemplate.java:668)
    at de.virtualsolution.core.dao.impl.HibernateDAO2Impl .store(HibernateDAO2Impl.java:84)
    at com.asl.app.etw.service.impl.EtwServiceImpl.saveEt w(EtwServiceImpl.java:102)
    at com.asl.app.etw.sites.EtwStammdatenTest.testStammd aten(EtwStammdatenTest.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at junit.framework.TestCase.runTest(TestCase.java:154 )
    at junit.framework.TestCase.runBare(TestCase.java:127 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit 3TestReference.run(JUnit3TestReference.java:128)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)


    My source is:

    ****************************

    EtwService service = (EtwService) mFactory.getBean("EtwService");

    EtwBabPk etwbabPk = new EtwBabPk();
    etwbabPk.setBabp_art("Test_babp_art");
    etwbabPk.setEtw_id(new Integer(2));
    etwbabPk.setLfdnr((Short) new Integer(1).shortValue());
    etwbabPk.setLvart_grp("TestLvart_grp");

    EtwBabEntity etwbab = new EtwBabEntity();
    etwbab.setAbt("Test_ABT");
    etwbab.setBetr("Test_BETR");
    etwbab.setBez("Test_BEZ");
    etwbab.setEdt(new java.sql.Date(new java.util.Date().getTime()));
    etwbab.setPk(etwbabPk);
    etwbab.setUsr("gfischer");

    EtwEntity etwInsert = new EtwEntity();
    etwInsert.setId(new Integer(2));
    etwInsert.setBez("bez2");
    etwInsert.setGvon(new java.sql.Date(new java.util.Date().getTime()));
    etwInsert.setGbis(new java.sql.Date(new java.util.Date().getTime()));
    etwInsert.setEdt(new java.sql.Date(new java.util.Date().getTime()));
    etwInsert.setUsr("gfischer");

    service.saveEtw(etwInsert);

    ***************************

    And the tables are designed like this:

    Table ETW.

    create table asl01 : gfischer . etw (
    id int not null,
    bez char(30) not null,
    gvon date not null,
    gbis date not null,
    usr char(8) not null,
    edt datetime not null,
    fkz char(1),
    constraint c_etw_pk primary key ( id )
    );

    create unique index 3459_15625 on asl01 : gfischer . etw ( id );

    Table ETW_BAB:

    create table asl01 : gliebrand . etw_bab (
    etw_id int not null,
    lvart_grp char(1) not null,
    babp_art char(20) not null,
    lfdnr int not null,
    betr money(16,2) not null,
    cbkz char(1),
    abt char(10),
    bez char(30),
    usr char(8) not null,
    edt datetime not null,
    fkz char(1)
    );

    alter table asl01 : gliebrand . etw_bab
    add constraint c_etw_bab_01
    foreign key ( etw_id )
    references asl01 : gliebrand . etw ( id )
    on delete restrict
    on update restrict ;
    create unique index i_etw_bab_01 on asl01 : gliebrand . etw_bab ( etw_id , lvart_grp , babp_art , lfdnr );
    create index 3460_15632 on asl01 : gliebrand . etw_bab ( etw_id );

    I can't find the problem. I used this processing with another single table, but that works. Also I can read the 1:n Relation (ETW - ETW_BAB) without any problems.

    I can't understand why I get the Exception.

    I'am going to go very happy, if anyone helps me in this case.

    Thank you!

    ****SORRY, FOR MY POOR ENGLISCH ****

    Best Regards
    gfischer

  2. #2
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    Most probably Hibernate is trying to generate update instead of insert and fails because there is nothign to update. Can you post your service and domain classes?

  3. #3
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    Never mind, I found it:
    Code:
    org.springframework.orm.hibernate3.HibernateTempla te.saveOrUpdate(HibernateTemplate.java:668)
    You are invoking saveOrUpdate and setting the primary key. When primary key is set and a generator defined for the primary key saveOrUpdate will generate update and not insert.

  4. #4
    Join Date
    Oct 2006
    Posts
    2

    Default

    Quote Originally Posted by dejanp View Post
    Never mind, I found it:
    Code:
    org.springframework.orm.hibernate3.HibernateTempla te.saveOrUpdate(HibernateTemplate.java:668)
    You are invoking saveOrUpdate and setting the primary key. When primary key is set and a generator defined for the primary key saveOrUpdate will generate update and not insert.
    Ok, I will try this. Thank you very much.

    Best Regards
    gfischer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •