Announcement Announcement Module
Collapse
No announcement yet.
How to use Spring Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use Spring Hibernate

    I use getHibernateTemplate() to save my objects to database, like this:
    Code:
     public void saveBid(Bid bid) {
      getHibernateTemplate().saveOrUpdate();
    }
    Each bid has a reference to an Item. (pretty much like the Hibernate-in-action example in chapter 3.
    Code:
    public class Bid {
       private Item item;
       // getter/setter for item
    }
    This works find so far, if the relation from Bid to Item is unidirectional many-to-one association.

    However, as soon as I make the relationship between Bit and Item a bi-directional (like the way the book did).

    Here is the code which I build the relationship:
    Code:
    Bid bid = new Bid();
    
    // get the item using query
    Item item = item.getItemById(itemid);
    
    // uni-directional way, it works.
    bid.setItem(Item);
    //bi-directional way , it fails.
    //phone.addBuild(build);
    
    bidManager.saveBid(bid);

    I get this exception:
    Code:
    ERROR [http-8080-Processor23] JDBCExceptionReporter.logExceptions(72) | Cannot add or update a child row: a foreign key constraint fails (`nwebdb/BIDS`, CONSTRAINT `FK756FFBC5831701D8` FOREIGN KEY (`BUILD_ID`) REFERENCES `ITEMS` (`ITEM_ID`))
    [getnwebadmin] ERROR [http-8080-Processor23] AssertionFailure.<init>(22) | an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
    org.hibernate.AssertionFailure: null id in com.testproj.testproj.model.Build entry (don't flush the Session after an exception occurs)
    	at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:55)
    	at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:157)
    	at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)
    	at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
    	at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
    	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
    	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    	at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:558)
    	at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:540)
    	at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:510)
    	at org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionAspectSupport.java:347)
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:111)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:209)
    	at $Proxy12.saveBuild(Unknown Source)
    	at com.testproj.testproj.webapp.action.AddBuildForm.save(AddBuildForm.java:171)
    	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:585)
    	at com.sun.el.parser.AstValue.invoke(AstValue.java:151)
    	at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:283)
    	at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68

  • #2
    You don't need to explictly save the objects. If you have Bid and Item you can just associate both of them and Hibernate will save them for you. You just need to setup your cascade setting correctly.

    Code:
    Item item = item.getItemById(itemid);
    Bid bid = new Bid();
    bid.setItem(item);
    item.getBids().add(bid);
    I would recommend putting this code in a method on the domain object. I would also recommend looking at the hibernate web site as there are lots of examples of this.

    http://www.hibernate.org/hib_docs/v3...-bidirectional
    Last edited by karldmoore; Nov 1st, 2006, 04:03 PM.

    Comment


    • #3
      Thanks for you help. But I don't understand why "If you have Bid and Item you can just associate both of them it Hibernate will save them for you. "

      In my case, the Item object is already in the database.

      Code:
      Item item = item.getItemById(itemid);
      Bid bid = new Bid();
      bid.setItem(item);
      item.getBids().add(bid);
      And if I just do the above, when will the bid object be saved (it is newly created there).

      In the hibernate example, I see it always has
      Code:
      Session session = sessions.openSession();
      Transaction tx= session.beginTransaction();
      
      Item item = session.get(Item.class, new Long(itemid));
      Bid bid = new Bid();
      bid.setItem(item);
      item.getBids().add(bid);
      
      tx.commit();
      session.close();
      But when I use hibernate with Spring, it does not do the session open/close().
      So when will the newly created object be saved?
      And even if I save it, why it fails?

      Comment


      • #4
        The documentation for the two projects will explain it better than I can. Check out these two chapters they should answer all your questions.

        The Hibernate documentation explains transitive persistence.
        http://www.hibernate.org/hib_docs/v3...ate-transitive

        The Spring documentation explains the transaction management.
        http://static.springframework.org/sp...ansaction.html

        Comment

        Working...
        X