Results 1 to 4 of 4

Thread: HibernateTemplate in child-parent situation

  1. #1
    Join Date
    Oct 2004
    Location
    Helsinki, Finland
    Posts
    17

    Default HibernateTemplate in child-parent situation

    Hi,

    I noticed strange behaviour on our system when using HibernateTemplate in Spring - compared to doing same thing with just Hibernate session.

    We have a node-table where child-parent relationship is described and each node has parent_id. Table has also an index column that we would like to have a not null constraint. This works ok in simple testcase when using just hibernate session, however when we use HibernateTemplate I noticed that hibernate behaves differently:

    1) insert into nodes (title, state, parent_id, owner_group_id, id) values (?, ?, ?, ?, ?)

    2) Hibernate: update nodes set parent_id=null, position=null where parent_id=? and position=?

    3) Hibernate: update nodes set parent_id=?, position=? where id=?

    Step two causes constraint violation, since there is an update to null values.

    Package with everything except the database to test:
    www.huima.net/SpringTest.tar

    My post to hibernate forum:
    http://forum.hibernate.org/viewtopic...259&highlight=

    Any ideas or thoughts would be helpfull.

    I will try next to set constraint deferred - so that the transaction will be handled 'till the end before the constraints are checked.

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Can you please post some of the code here.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Aug 2004
    Location
    Warsaw, Poland
    Posts
    33

    Default Re: HibernateTemplate in child-parent situation

    Quote Originally Posted by huima
    Hi,

    I noticed strange behaviour on our system when using HibernateTemplate in Spring - compared to doing same thing with just Hibernate session.

    We have a node-table where child-parent relationship is described and each node has parent_id. Table has also an index column that we would like to have a not null constraint. This works ok in simple testcase when using just hibernate session, however when we use HibernateTemplate I noticed that hibernate behaves differently:

    1) insert into nodes (title, state, parent_id, owner_group_id, id) values (?, ?, ?, ?, ?)

    2) Hibernate: update nodes set parent_id=null, position=null where parent_id=? and position=?

    3) Hibernate: update nodes set parent_id=?, position=? where id=?

    Any ideas or thoughts would be helpfull.
    Looks like You have set inverse="true" (I 99% sure) on wrong side. Have You read:
    http://www.hibernate.org/155.html#A5

    Be 100% You use exactly the same mappings where using pure hibernate and Spring Hibernate Template. It is highly unlikly that there is problem with spring, because spring provides only "tiny" wrapper over hibernate session.

    Artur

  4. #4
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    I took a brief look at your code sample. You appear to be using HibernateTemplate in a completely manual fashion, with no wrapping Spring or JTA transaction. That is, you are just setting up the template as a JavaBean, and feeding it the session factory, but that's about it.

    In this setup, each use of HibernateTemplate is going to create its own Session just for that invocation of the template. Not really what you want if you want to combine multiple operations via the template, and have them operate together on the same session. If you are not going to use HibernateTransactionManager, or HibernateInterceptor, two mechanisms available to automatically have SessionFactory binding on the thread (from which the shared Session usage is keyed), then you would have to manually use SessionFactoryUtils to do this binding...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Saving Parent / Child Object Graph
    By matthewramella in forum Data
    Replies: 3
    Last Post: Nov 28th, 2007, 08:33 AM
  2. Replies: 1
    Last Post: Aug 18th, 2006, 11:04 PM
  3. Hibernate Long Session Per Flow?
    By akw in forum Web Flow
    Replies: 21
    Last Post: Dec 12th, 2005, 08:06 PM
  4. Replies: 3
    Last Post: Jul 27th, 2005, 10:41 AM
  5. Replies: 0
    Last Post: Jun 6th, 2005, 10:29 AM

Posting Permissions

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