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: Parent/Child relationship...parent-saved,child-notsaved
PostPosted: Wed Sep 01, 2004 2:16 am 
Newbie

Joined: Wed Sep 01, 2004 1:26 am
Posts: 2
Location: India
Hello,
I am very much new to hibernate. I was writing a small application on JBoss. The application contains only 2 classes Animal and Animal_Cat. Data shall be stored in two different tables namely ParentTable and KidTable. The KidTable has got a foreign key relation with the ParentTable. Data from the Animal POJO shall be stored in the ParentTable and data from the Animal_Cat POJO shall be stored in the KidTable. The mapping and cofig files along with the source code to execute the above process is shown below. My problem now is... I am able to save the Animal POJO in the ParentTable but the child objects (Aminal_Cat objects) are not being saved in the database. I did try out many possibilities with the config files... but could not find the exact approach.. if anyone could help me out of this, thank you... also you can find my POJOs below...


Animal POJO:
private long id;
private String name;
private char sex;
private float weight;
private String subclass;
Set kittens;
public Animal()
{
kittens = new HashSet();
}
public long getId()
{
return id;
}
public void setId( long id )
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
public String getSubclass()
{
return this.subclass;
}
public void setSubclass( String name )
{
this.subclass = name;
}
public char getSex()
{
return sex;
}
public void setSex( char sex )
{
this.sex = sex;
}
public float getWeight()
{
return weight;
}
public void setWeight( float weight )
{
this.weight = weight;
}
public void setKittens( Set coll )
{
this.kittens = coll;
}
public Set getKittens()
{
return this.kittens;
}
public void addKitten( Animal_Cat kitten )
{
kitten.setMotherid( this.getId() );
this.kittens.add( kitten );
}

Animal_Cat POJO:
long kidid;
long motherid;
String kidName;
public void setMotherid( long lMotherid )
{
this.motherid = lMotherid;
}
public long getMotherid()
{
return this.motherid;
}
public void setKidid( long id )
{
this.kidid = id;
}
public long getKidid()
{
return this.kidid;
}
public void setKidName( String name )
{
this.kidName = name;
}
public String getKidName()
{
return this.kidName;
}


please let me know how do i overcome this problem... and what exactly am i missing here....



Hibernate version: 2.1

Mapping documents:
------hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:/SQLServerPool</property>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>

<!-- Mapping files -->
<mapping resource="Animal.hbm.xml"/>
</session-factory>
</hibernate-configuration>


-------Application related mapping file
Animal.hbm.xml

<hibernate-mapping package="mainpage">

<class name="Animal" table="MyTable">
<id name="id" column="ID" type="long" unsaved-value="any">
<generator class="increment"/>
</id>
<property name="subclass" column="subclass" type="string"/>
<property name="name" type="string"/>
<property name="sex" not-null="true" update="false"/>
<property name="weight"/>
<!--many-to-one name="mate" column="mateid" class="Animal"/-->
<set name="kittens" inverse="true" cascade="all">
<key column="motherid"/>
<one-to-many class="Animal_Cat"/>
</set>
</class>

<class name="Animal_Cat" table="KidTable">
<id name="kidid" column="kidid" type="long" unsaved-value="any">
<generator class="increment"/>
</id>
<property name="kidName" column="name" type="string"/>
<!--property name="motherid" column="motherid" type="long"/-->
<many-to-one name="motherid" class="Animal" update="false" insert="false"/>
</class>

</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():

Animal princess = new Animal();
princess.setName( "Princess" );
princess.setSex( 'F' );
princess.setWeight( 7.4f );

/*princess = ( Animal )session.load( Animal.class, id );
session.flush();*/

int nSize = 6;
Set kittens = new HashSet();
/*for( int ii = 0; ii < nSize; ii++ )
{
Animal_Cat kitten = new Animal_Cat();
kitten.setKidName( "Kitten #" + ( ii + 1 ) );
kitten.setMotherid( Long.parseLong( id.toString() ) );
//princess.addKitten( kitten );
System.out.println( "Executing kitten......" + kitten.getKidName() + " : " + kitten.getMotherid() );
kittens.add( kitten );
}*/
princess.setKittens( kittens );
princess.setSubclass( Integer.toString( nSize ) );

Session session = null;
Transaction tx = null;
try
{
session = currentSession();

tx = session.beginTransaction();

Serializable id = session.save( princess );
System.out.println( "The new Id is : " + id );

tx.commit();
}
catch ( SQLException ex )
{
try
{
if ( tx != null )
tx.rollback();
}
catch( HibernateException he )
{
he.printStackTrace();
}
ex.printStackTrace();
}
catch ( HibernateException ex )
{
ex.printStackTrace();
}
finally
{
try
{
closeSession();
}
catch ( SQLException ex )
{
ex.printStackTrace();
}
catch ( HibernateException ex )
{
ex.printStackTrace();
}
}



Full stack trace of any exception that occurs:
No exceptions found....


Name and version of the database you are using:
SQL Server 2000


Debug level Hibernate log excerpt:

11:32:32,671 INFO [NamingHelper] JNDI InitialContext properties:{}
11:32:32,703 INFO [DatasourceConnectionProvider] Using datasource: java:/SQLServerPool
11:32:33,015 INFO [STDOUT] Proceeding to save the Animal object
11:32:33,187 INFO [STDOUT] The new Id is : 36
11:32:33,296 INFO [STDOUT] Closing connection...

_________________
balu
e-marg


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 01, 2004 2:32 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
- remove the unsave-value attributes
- the kittens HashSet is empty uncomment the code block after the new and change it to

Code:
for( int ii = 0; ii < nSize; ii++ )
{
  Animal_Cat kitten = new Animal_Cat();
  kitten.setKidName( "Kitten #" + ( ii + 1 ) );
  kittens.add( kitten );
}

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 01, 2004 2:55 am 
Newbie

Joined: Wed Sep 01, 2004 1:26 am
Posts: 2
Location: India
thanx for your reply ernst...

yup.. did that too.. but still no luck...
also, i tried saving the Parent first, loading it back and saving the child objects... it did not work...
also, tried using different .hbm.xml files for each of the classes.... no use with that too...
i m a bit sceptic about my config files.. may be i m missing something there... not able to find out what and where... please help me on this ernst.

_________________
balu
e-marg


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 01, 2004 7:09 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Please send again your pojo code, mapping files and code between sessionFactory.openSession() and session.close().

Make sure your reduce everything to the bare mimum and leave away the commented out stuff to increase readability.

HTH
Ernst


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.