-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Many-To-Many not saving, but loading fine - typo?
PostPosted: Sat Sep 19, 2009 2:01 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I know I've done a million times, but this many-to-many isn't saving. I'm using hibernate 3.3.1ga with spring 3.0.0m4.

Code:
   @Test
   public void save() {
      Job job = new Job();
      ...
      job.addCategory( categoryDao.find( 1 ) );

      jobDao.save( job );
      clear();

      assertFalse( job.isTransient() );

      jdbcMap = simpleJdbcTemplate.queryForMap( "select * from job where job_id = ?", job.getId() );
      ... // test stuff

      // Spring FAILS here - it cannot find 1 row.
      jdbcMap = simpleJdbcTemplate.queryForMap( "select * from job_to_category where job_id = ?", job.getId() );
      assertEquals( 1, jdbcMap.get( "CATEGORY_ID" ) );
   }


Here is the mapping:
Code:
<hibernate-mapping default-access="field" auto-import="true" default-lazy="true" default-cascade="all-delete-orphan">

    <class name="myproject.domain.job.Job" table="job">
        <id name="id" column="job_id" type="long">
            <generator class="native"/>
        </id>
        <property name="positionName" column="position_name"/>
        <property name="description" type="text"/>
        <property name="excerpt" type="text"/>
        <property name="googleMapUrl" column="google_map_url"/>
        <property name="expiryDate" column="expiry_date"
                  type="org.joda.time.contrib.hibernate.PersistentLocalDate" />
        <property name="views"/>
        <bag name="categories" table="job_to_category">
            <key column="job_id" />
            <many-to-many class="myproject.domain.Category" column="category_id" />
        </bag>
    </class>

    <class name="myproject.domain.Category" table="category">
        <id name="id" column="category_id" type="long">
            <generator class="native"/>
        </id>
        <property name="name" column="name"/>
        <many-to-one name="parent" class="myproject.domain.Category"
                     column="parent_id"/>
        <set name="children" inverse="true">
            <key column="category_id"/>
            <one-to-many class="myproject.domain.Category"/>
        </set>
    </class>

</hibernate-mapping>


I checked the sql output and it appears hibernate is generating an insert on job or category (if it was new)... but it's not inserting any rows into the job_to_category table.

I dunno why this isn't working. I've tried inverse="true" and "false". I'm stumped. And maybe I've been programming for long. Almost 10 hours now :/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.