There is no cascading insert for a child with a foreign key on the parent

I checked a lot of posts on the Internet, and none solves my problem. Really appreciate if anyone can help! I have OneToMany parenting. Hibernate does not cascade a child element (level 2) if one of the child compound keys is a foreign key for the parent element (level 1). I specified cascade = "all", and regardless of whether the converse is true or false, the results are the same. There are no exceptions, and it just does not get inserted. The printout below shows that Level1 was successfully inserted, but Level2 was selected only without insertion.

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=?

Hibernate 4.1.6 / Spring 3.1.2 APIs

Here is the table definition for MySQL:

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.

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);
   }
}

Matching for level 1:

<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>

2:

<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>
+6
2

.

:

level2.setId(new Level2Id(level1.getId(), 10));

, 1 , , . level_1_id , .

Integer generatedId = (Integer) Session.save(level1)

. Lever2Id

level2.setId(new Level2Id(generatedId  , 10));

MySql .

level1,

Column 'LEVEL_1_ID' cannot be null

UPDATE

, , , , . , .

, Child , , Hibernate , . .

" IdentifierGenerator . .

hibernate, , - org.hibernate.mapping.Component( , , ..), "", , , CompositeUserType ( ) , . .

, . "none", Hibernate. , .

, ( ).

 Parent parent = new Parent();
    parent.setStatus( 1  );
    parent.setName( "Parent" );  
    ChildId childId = new ChildId();    
    Child child = new Child(childId);    
    child.setCategory( "TH" );    
    parent.addChild( child );
    child.getId().setTimesegment( 10 );

addChild , .

 public void addChild(Child child){
    if (child !=null){
      getChildrens().add( child );
      child.setParent( this );
    }

  }

, DAO, Child as, , - ( - DAO)

public void save(Child child){

     //get Session 
    Session session =.......
    Transaction tx = session.beginTransaction();
    Integer generatedId = (Integer)session.save( child.getParent() );
    ChildId childId = child.getId();
    childId.setChildId( generatedId );     
    session1.save( child );   
    tx1.commit();
    HibernateUtil.closeSession();
}

, .

+1

. . . = "". . , .

CREATE TABLE level_11 (
ID int NOT NULL PRIMARY KEY AUTO_INCREMENT, 
STATUS int, NAME varchar(255) );

CREATE TABLE level_22 ( ID int NOT NULL PRIMARY KEY AUTO_INCREMENT, 
LEVEL_11_ID int NOT NULL, 
TIMESEGMENT int NOT NULL, 
FOREIGN KEY (LEVEL_11_ID) REFERENCES level_11(ID) ON DELETE CASCADE, 
CONSTRAINT UQ_LEVEL2 UNIQUE (LEVEL_11_ID, TIMESEGMENT) );
0

All Articles