-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problems with Update with a Set
PostPosted: Sun Mar 12, 2006 9:48 am 
Newbie

Joined: Mon Oct 10, 2005 1:26 pm
Posts: 2
Hello every one. In my application i've the following mapping for a one-to-many relationship between 2 tables.
<hibernate-mapping package="com.mccalv.dbObject">

<class name="Contenuti" table="contenuti">
<id name="idcontenuti" column="idContenuti" type="integer">
<generator class="native"/>
</id>

<property name="titolo" column="titolo" type="string" not-null="true" />
<property name="idautore" column="idAutore" type="integer" not-null="true" />
<property name="dataimmissione" column="dataImmissione" type="timestamp" not-null="true" />
<property name="datapubblicazione" column="dataPubblicazione" type="timestamp" not-null="true" />
<property name="approvato" column="approvato" type="integer" not-null="true" />
<property name="datariferimento" column="dataRiferimento" type="timestamp" not-null="true" />
<property name="idschede" column="idSchede" type="integer" not-null="true" />
<property name="idcategoria" column="idCategoria" type="integer" not-null="true" />

<set name="contenuticampi" cascade="all" order-by="ordine ASC">
<key column="idContenuti"></key>
<one-to-many class="Contenuticampi"></one-to-many>
</set>


</class>

</hibernate-mapping>
And..

hibernate-mapping package="com.mccalv.dbObject">

<class name="Contenuticampi" table="contenuticampi">
<id name="idcontenuticampi" column="idContenutiCampi" type="integer">
<generator class="native"/>
</id>

<property name="nomecampo" column="nomeCampo" type="string" />
<property name="value" column="value" type="string" />


<property name="ordine" column="ordine" type="integer" not-null="true" />
</class>

</hibernate-mapping>
Every works fine for INSERT AND DELETE.
When i update a Contenuti Hibernate writes the data twice, first time with the rigth association and second time with the foreign key as "null".
The code i use for update is the following.
java.util.Date date=new java.util.Date();
contenuti.setTitolo(manageContenutiForm.getTitolo());
contenuti.setIdautore(Integer.parseInt(""+request.getSession().getAttribute("idutenti")));
contenuti.setIdcategoria(manageContenutiForm.getIdcategoria());
contenuti.setIdschede(manageContenutiForm.getIdschede());
contenuti.setDataimmissione(date);
contenuti.setDatapubblicazione(date);


contenuti.setDatariferimento(date);

contenuti.setApprovato(0);
int fieldNumber= Integer.parseInt(""+request.getParameter("len"));
String field = null;
String name=null;

Set contentFields= new HashSet();

if (request.getParameter("len")!=null){
for (int i=1;i<=fieldNumber;i++){

field =request.getParameter("field"+i);
name= request.getParameter("field"+i+"[name]");

Contenuticampi contenuticampi=new Contenuticampi();
contenuticampi.setNomecampo(name);
contenuticampi.setOrdine(i);
contenuticampi.setValue(field);
}
contenuti.setContenuticampi(contentFields);
}



try{

s.update(contenuti);
....


Top
 Profile  
 
 Post subject: Re: Problems with Update with a Set
PostPosted: Sun Mar 12, 2006 4:07 pm 
Regular
Regular

Joined: Wed Feb 22, 2006 11:28 am
Posts: 65
Location: Santiago, Chile
Hello My Friend:

After read your code, i saw a point that is the reason which cause you that problem.

If you see your code:

[code] Contenuticampi contenuticampi=new Contenuticampi()[/code]
contenuticampi.setNomecampo(name);
contenuticampi.setOrdine(i);
contenuticampi.setValue(field);

You are instancing a "new" Object without its original Id. So, when you want to update it, it`s natural say you "id is null...", and so on.

I suggest you obtain the ID for that Object, then you can execute update job.

request for comments.

Neketsu Shonen[/code][/quote]


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