-->
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.  [ 3 posts ] 
Author Message
 Post subject: saving an object does fail, loading objects works? plz hlp
PostPosted: Wed Aug 09, 2006 10:16 am 
Newbie

Joined: Wed Aug 09, 2006 10:05 am
Posts: 2
Hi all,

well, I`ve got two really simple classes which I wanna make objectrelational with a MySQL-database - whilst reading objects ain`t no problem, writing them causes a "NULL" entry for the foreign key. To understand what I`m starting off from, here`s the mapping:

Customer.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="moe.ot.pro.tree.Customer" table="TCUSTOMERS">
<id name="CID" column="CID" type="java.lang.Integer">
<generator class="increment"/>
</id>
<property name="CDESCRIPTION" column="CDESCRIPTION" type="java.lang.String" />
<set name="pvs" table="TGSPROJECT" lazy="true" inverse="true" cascade="all" >
<key column="FK_CUSTOMER"/>
<one-to-many class="moe.ot.pro.tree.ProjectVersion"/>
</set>
</class>
</hibernate-mapping>

ProjectVersion.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="moe.ot.pro.tree.ProjectVersion" table="tgsproject">
<id name="GSPROJECT_ID" column="GSPROJECT_ID" type="java.lang.Integer">
<generator class="increment"/>
</id>
<property name="PERFORCE_PATH" column="PERFORCE_PATH" type="java.lang.String" />
<property name="DISPLAYNAME" column="DISPLAYNAME" type="java.lang.String" />
<property name="FK_CUSTOMER" column="FK_CUSTOMER" type="java.lang.Integer" insert="false" update="false"/>
<set name="otprojects" table="TPROJECTVERSION" lazy="true" inverse="true" cascade="all" >
<key column="FK_GSPROJECT"/>
<one-to-many class="moe.ot.pro.tree.OTProject"/>
</set>
<many-to-one name="customer" cascade="all" class="moe.ot.pro.tree.Customer" column="FK_CUSTOMER" not-null="false" />
</class>
</hibernate-mapping>

When I run this method:

===
private void fuckward() {


Session session = null;
HibernateConnector hc = null;
Transaction tx = null;
getHibernateSettings();
try {
hc = new HibernateConnector(jdbcUser, jdbcPass, jdbcDriver,sqlDialect, sqlURL, false);
session = hc.getSession();
tx = session.beginTransaction();

Customer honk = new Customer("TEST customer !!");
ProjectVersion bla = new ProjectVersion("TEST pro version");

honk.add(bla);

session.save(honk);

tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}finally{
if(session!=null)session.close();
}

}
===

...there will be an entry "TEST customer" in the appropriate table and "TEST pro version" as well - but the foreign key FK_CUSTOMER will be set to NULL. Why that?

Maybe I`m already coding way too long, I dunno, I cannot locate the error. As mentioned before: reading those objects works just perfectly. What`s the catch?

Thnx in advance for your help.

Regards,

Z4C


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 10:36 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
in that add method, are you setting the parent of ProjectVersion to that Customer??

If not, add this somewhere before you save

Code:
bla.setCustomer(honk)


in the long run, it would be better idea to put this in that add method though

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 2:45 am 
Newbie

Joined: Wed Aug 09, 2006 10:05 am
Posts: 2
Yeah, you were absolutely right. Sorry, I`m a total noob. ;) Thank you very much for your help, I appreciate that a lot.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.