Announcement Announcement Module
Collapse
No announcement yet.
HibernateTemplate in child-parent situation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Can you please post some of the code here.

    Comment


    • #3
      Re: HibernateTemplate in child-parent situation

      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

      Comment


      • #4
        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...

        Comment

        Working...
        X