-->
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.  [ 4 posts ] 
Author Message
 Post subject: No cascade insert for child with foreign key on parent
PostPosted: Mon Sep 10, 2012 4:05 pm 
Newbie

Joined: Mon Sep 10, 2012 2:26 pm
Posts: 2
I have checked many posts on the internet and none address my problem. Really appreciate if anyone can help!
Hibernate does not cascade insert child (Level2) when one of the child composite keys is the foreign key to the parent (Level1). I specified cascade="all" and no matter the inverse is true or false, the results are the same. There is no exception, and it simply does not insert. Below print out shows Level1 was inserted successfully but Level2 was only selected, no insert.

Hibernate: insert into sst.level_1 (STATUS, NAME) values (?, ?)
Hibernate: select level2x_.LEVEL_1_ID, level2x_.TIMESEGMENT, level2x_.CATEGORY as CATEGORY4_ from sst.level_2 level2x_ where level2x_.LEVEL_1_ID=? and level2x_.TIMESEGMENT=?

The APIs are Hibernate 4.1.6/Spring 3.1.2.

Here is the table definition for mySQL:
Code:
CREATE TABLE level_1
(
ID int NOT NULL PRIMARY KEY AUTO_INCREMENT,
STATUS int,
NAME varchar(255)
);

CREATE TABLE level_2
(
LEVEL_1_ID int,
TIMESEGMENT int,
CATEGORY varchar(2),
PRIMARY KEY (LEVEL_1_ID, TIMESEGMENT),
FOREIGN KEY (LEVEL_1_ID) REFERENCES level_1(ID) ON DELETE CASCADE
);


Here is the test code.
Code:
public class Test {
   
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      doInsert(context);
   }

   private static void doInsert(ApplicationContext context) {
   
      Level1 level1 = new Level1();
      Level2 level2 = new Level2();
      
      level1.setName("LEVEL 1 NAME");
      level1.setStatus(1);
      level1.getLevel2s().add(level2);
      
      level2.setLevel1(level1);
      level2.setCategory("CA");
      level2.setId(new Level2Id(level1.getId(), 10));
      
      Level1DAO level1DAO = (Level1DAO) context.getBean("Level1DAO");
      level1DAO.save(level1);
   }
}


Mapping for Level1:
Code:
<hibernate-mapping>
    <class name="com.jc.hibernate.Level1" table="level_1" catalog="sst">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="identity" />
        </id>
        <property name="status" type="java.lang.Integer">
            <column name="STATUS" />
        </property>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <set name="level2s" inverse="true" cascade="all">
            <key>
                <column name="LEVEL_1_ID" not-null="true" />
            </key>
            <one-to-many class="com.jc.hibernate.Level2" />
        </set>
    </class>
</hibernate-mapping>


Mapping for Level2:
Code:
<hibernate-mapping>
    <class name="com.jc.hibernate.Level2" table="level_2" catalog="sst">
        <composite-id name="id" class="com.jc.hibernate.Level2Id">
            <key-property name="level1Id" type="java.lang.Integer">
                <column name="LEVEL_1_ID" />
            </key-property>
            <key-property name="timesegment" type="java.lang.Integer">
                <column name="TIMESEGMENT" />
            </key-property>
        </composite-id>
        <many-to-one name="level1" class="com.jc.hibernate.Level1" update="false" insert="false" fetch="select">
            <column name="LEVEL_1_ID" not-null="true" />
        </many-to-one>
        <property name="category" type="java.lang.String">
            <column name="CATEGORY" length="2" />
        </property>
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: No cascade insert for child with foreign key on parent
PostPosted: Thu Sep 13, 2012 9:10 am 
Newbie

Joined: Thu Sep 13, 2012 9:06 am
Posts: 1
I have the same exact problem and haven't found a solution yet!


Top
 Profile  
 
 Post subject: Re: No cascade insert for child with foreign key on parent
PostPosted: Sat Sep 15, 2012 12:50 am 
Beginner
Beginner

Joined: Fri Sep 14, 2012 10:41 am
Posts: 20
When you have a complex problem like that, it's a good idea to try to isolate the problem into its simplest form.

I see a few suspicious things that you are doing in your child class. Try commenting out the <many-to-one> association with insert="false". Make the child class really simple with only key declaration and one simple int attribute (make one up if necessary). Then test if you are still experiencing the problem.

Whether your answer is yes or no, you are much further ahead in resolving it!

I would be interested to know the answer.


Top
 Profile  
 
 Post subject: Re: No cascade insert for child with foreign key on parent
PostPosted: Tue Sep 18, 2012 11:40 am 
Newbie

Joined: Mon Sep 10, 2012 2:26 pm
Posts: 2
It does not seem to make difference with the suggestion. The problem is basically the level 1 id was null when it is set to level 2. Hibernate does not re-associate it once it is generated when level 1 is inserted to database, which generate level 1 id when this happened.


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

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.