-->
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.  [ 5 posts ] 
Author Message
 Post subject: Empty foreign key in a child POJO
PostPosted: Thu Sep 02, 2004 4:08 pm 
Newbie

Joined: Thu Sep 02, 2004 11:44 am
Posts: 3
Hibernate version: hibernate-2.1.6

Hi all,
I'm a newbie with hibernate, so probably I've got an error in my configuration or bean.My goal is map two table, first with region's name and second with region's city, these table are connected with a foreign key.I've generate beans with a tool named "hibernate synchronizer" and the mapping file also.All seems ok but when I try to save into database the field of second table that should have ID of first table are empty.I take ID from a sequence during storing and probably, cause I'm storing both table at one time, child object can't populate his field.I can't see where I fail.
My mapping file are these:

<hibernate-mapping package="it.test">
<class name="Regioni" table="REGIONI">
<id column="ID"
name="Id"
type="integer"
unsaved-value="0">
<generator class="native">
<param name="sequence">seq_regioni</param>
</generator>
</id>
<property column="DESCRIZIONE"
length="100"
name="Descrizione"
not-null="false"
type="string" />
<set inverse="true"
lazy="true"
name="province"
table="province"
cascade="all">
<key column="IDREGIONE"/>
<one-to-many class="Province" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="it.rai.tlv">
<class name="Province" table="PROVINCE" dynamic-insert="true" dynamic-update="true">
<id column="ID"
name="Id"
type="integer">
<generator class="native">
<param name="sequence">seq_province</param>
</generator>
</id>
<property column="DESCRIZIONE"
length="100"
name="Descrizione"
not-null="false"
type="string" />
<property column="SIGLA"
length="5"
name="Sigla"
not-null="false"
type="string" />
<many-to-one class="Regioni"
name="Idregione"
not-null="true"
column="IDREGIONE"/>
</class>
</hibernate-mapping>

This is my code from starting session and close it
....
Session session = factory.openSession(con);
Transaction trans = session.beginTransaction();
session.saveOrUpdate(regione);
trans.commit();
....

I retrieve an SQLException cause idregione in province table can't be null

Name and version of the database you are using: Oracle9.2

Thanks a lot for all suggestion

P.S.
I hope that I have been sufficiently comprehensible


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 4:13 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you must call setIdregione somewhere before calling session.save

note: be carefull with java naming convention

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 4:19 pm 
Newbie

Joined: Thu Sep 02, 2004 11:44 am
Posts: 3
Sorry but I was think that storing parent whit cascade option could free to me from any setting in child bean.
it does not work in this way?

Thanks for help


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 4:56 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i cannot asnwer since i don't have any idea how is regione filled


Code:
Session session = factory.openSession(con);
Transaction trans = session.beginTransaction();
session.saveOrUpdate(regione);
trans.commit();

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 5:21 pm 
Newbie

Joined: Thu Sep 02, 2004 11:44 am
Posts: 3
Oops :-s yes of course.
So, I'm populate both bean from an XML with castor and obviously this XML don't have any id because I'm get it from the sequence.
I paste entire code from loading from MXL, hoping that help you to understand what I'm looking for.

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document m1 = builder.parse("Regioni.hbm");
Configuration config = new Configuration().addDocument(m1);
config.addDocument(builder.parse("Province.hbm"));
config.setProperty("hibernate.dialect", "net.sf.hibernate.dialect.Oracle9Dialect");
if(config != null) {
SessionFactory factory = config.buildSessionFactory();
if(factory != null) {
con.setAutoCommit(true);
Session session = factory.openSession(con);
Mapping mapping = new Mapping();
mapping.loadMapping(new InputSource("xmlMapping.xml"));
Unmarshaller loader = new Unmarshaller(mapping);
loader.setIgnoreExtraElements(true);
InputSource in = new InputSource("source.xml");
if(in != null && loader != null) {
Transaction trans = session.beginTransaction();
Container obj = (Container)loader.unmarshal(in);
Vector list = obj.getRegioni();
for(int x = 0; x < list.size(); x++) {
Regioni regione = (Regioni)list.get(x);
try {
session.saveOrUpdate(regione);
session.flush();
} catch(Exception e) {
System.out.println(regione.getId());
}
}
trans.commit();
}
}
}

This is my code ... source.xml is XML with contains data and xmlMapping is castor configuration file for mapping xml into bean.
This is the castor mapping file, if this can halp you

<mapping>
<class name="it.test.Container">
<map-to xml="OctopusContents"/>
<field name="regioni" type="it.test.Regioni" collection="vector">
<bind-xml name="Regione" location="Elenco" node="element"/>
</field>
</class>
<class name="it.test.Regioni">
<field name="descrizione" type="string">
<bind-xml name="ID" node="attribute"/>
</field>
<field name="province" type="it.test.Province" collection="set">
<bind-xml name="Provincia" node="element"/>
</field>
</class>
<class name="it.test.Province">
<field name="sigla" type="string">
<bind-xml name="ID" node="attribute"/>
</field>
</class>
</mapping>

I reallly really hope you can help me to understand where I wrong and then how hibernate works fine, cause I think it's a good product
Thanks


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