-->
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: Saving mulitple children elements
PostPosted: Fri Nov 01, 2013 7:42 am 
Newbie

Joined: Fri Nov 01, 2013 7:22 am
Posts: 1
I have a situation, which may be very simple to solve for you, but I am stalled with.
A have the following two mappings:
1. The parent class Tanulo, which has a set of Tantargy instances
Code:
<hibernate-mapping>
    <class name="com.minthaka.Tanulo" table="TANULO">
        <id name="id" type="java.lang.Long">
            <column name="ID" />
            <generator class="native" />
        </id>
        <version name="version" column="VERSION"/>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <set name="jegyek" table="TANTARGY" inverse="true" lazy="true" cascade="all">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="com.minthaka.Tantargy" />
        </set>
    </class>
</hibernate-mapping>

2. The child class Tantargy, which has backward reference to its Tanulo:
Code:
<hibernate-mapping>
    <class name="com.minthaka.Tantargy" table="TANTARGY">
        <id name="id" type="java.lang.Long">
            <column name="ID" />
            <generator class="native" />
        </id>
        <version name="version" column="VERSION"/>
       
        <property name="neve" type="java.lang.String">
            <column name="NEVE" />
        </property>
       
        <many-to-one name="tulajdonosa" class="com.minthaka.Tanulo" fetch="join">
            <column name="TULAJDONOSA" />
        </many-to-one>
       
        <property name="osztalyzat" type="int">
            <column name="OSZTALYZAT" />
        </property>
    </class>
</hibernate-mapping>


I am implementing the insertion into the database using the following method:
Code:
public boolean createTanulo(String neve,Set<String> tantargyak){
   boolean success=false;
   Session ss=sf.openSession(); 
     ss.beginTransaction();
     Tanulo t=new Tanulo();
      Tantargy tt=null;
      t.setName(neve);
      ss.save(t);
      tt=new Tantargy();
      tt.setNeve("Maoas");
      tt.setTulajdonosa(t);      
      ss.save(tt);    
           ss.getTransaction().commit(); 
           ss.close();
   return success;
}


I have no problems with this method if I use it as is (which is not solving the addition of multiple children, just of one). But I cannot figure how would I be able to do it in one session, since the following code:
Code:
public boolean createTanulo(String neve,Set<String> tantargyak){
   boolean success=false;
   Session ss=sf.openSession(); 
     ss.beginTransaction();
     Tanulo t=new Tanulo();
      Tantargy tt=null;
      t.setName(neve);
      ss.save(t);
             for(String x: tantargyak)
      tt=new Tantargy();
      tt.setNeve(x);
      tt.setTulajdonosa(t);   
               ss.save(tt);    
               }
           ss.getTransaction().commit(); 
           ss.close();
   return success;
}


is throwing me error: Cannot add or update a child row: a foreign key constraint fails (`Aureus`.`TANTARGY`, CONSTRAINT `FK_ovvs88dknnqwaf3p5xy920rj5` FOREIGN KEY (`ID`) REFERENCES `TANULO` (`ID`))

I would really appreciate any solution for this problem. Thanks


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.