-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Inserting an object in a Collection
PostPosted: Thu Dec 25, 2003 12:48 am 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
I have a bean which has three attributes, where one of them is a Collection. the other two are name and info. the name and info attributes hardly ever change, but the third attribute (the collectio) is always receiving new values to it. the xml mappings are correct, and i can make it work if i hard-code the collection id with its foreign key in the bean. the question is: how to do this just by adding a new object to the collection, and updating the bean ?

for instance:

SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
Session session;
session = sessionFactory.openSession();
Setor setor = (Setor) session.load(Setor.class, id);
Collection quadras = set.getQuadras();
Quadra obj = new Quadra();
quadra.setNome("whatever");
quadras.add(quadra);
setor.setQuadras(quadra);
session.flush();

I'm using mysql 4.0.16

Thanks to all.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 25, 2003 1:04 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
What is "collection id" ? Probably, I do not understand your question. But if you are talking about <key> property of the collection item, it is assigned automatically when you add an item to the collection.

Btw,
Code:
setor.setQuadras(quadra);


is not necessary - even if you modify collection obtained with getQuadras(), Hibernate will reflect changes to the database.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 10:58 am 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
Well, after setting the changes to the collection, what to do? save? flush? saveorupdate?
thanks a lot for the first post ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 11:00 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
flush.

And commit if you really need your changes were saved into database :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 11:53 am 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
Well, i still can't get it working. the code i'm using is as follows:

Session session;
session = sessionFactory.openSession();
Transaction trans;
trans = session.beginTransaction();
logger.info("Iniciada a Transa


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 12:00 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Are you using bi-directional association between Setor and Quatra? (When quatras set is mapped with inverse="true" and Quatra has property "setor")

In this case you need to call quatra.setSetor(...) in addition to set.getQuadras().add(quadra);

I suggest you paste your mapping here. It will be much easier to find your problem then.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 12:09 pm 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
I think i got the point.
Quadra mapping is as follows:
<class name="beans.Quadra" table="quadras">

<id name="id" column="id" type="java.lang.Long" unsaved-value="null" >
<generator class="native"/>
</id>

<property name="comentario">
<column name="comentario" sql-type="varchar(200)" not-null="true"/>
</property>

<property name="nome">
<column name="nome" not-null="true" />
</property>

<property name="livre">
<column name="livre" not-null="true"/>
</property>

<set name="lotes" inverse="true" >
<key column="id_quadra"/>
<one-to-many class="beans.Lote"/>
</set>


<many-to-one name="setor" column="id_setor"/>
</class>

Setor mapping now:

<class name="beans.Setor" table="setores">

<id name="id" column="id" type="java.lang.Long" unsaved-value="null" >
<generator class="native"/>
</id>

<property name="comentario">
<column name="comentario" sql-type="varchar(200)" not-null="false"/>
</property>

<property name="nome">
<column name="nome" not-null="true" unique="true"/>
</property>

<property name="livre">
<column name="livre" not-null="true"/>
</property>


<set name="lotes" inverse="true" >
<key column="id_lote"/>
<one-to-many class="beans.Lote"/>
</set>


<set name="quadras" inverse="true" >
<key column="id_quadra"/>
<one-to-many class="beans.Quadra"/>
</set>
</class>

This is it.
Again, thanks a lot.

Ot


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 12:36 pm 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
And this is my java code:

Setor set =
(Setor) session.load(Setor.class, new Long(paf.getId()));
logger.info("O valor do id eh :"+ paf.getId());


Quadra quadra = new Quadra();
quadra.setNome(paf.getNome());
logger.info("O nome da quadra nova: "+paf.getNome());
quadra.setComentario(paf.getComentario());
logger.info("O coment


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 12:40 pm 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
I forgot adding it is not working yet.

Ot


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 1:13 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Add cascade="all" to "quadras" set


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 1:23 pm 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
Well, i got it woking now. the databased is beiing updated. i haven't yet uset your last sugestion (adding cascade="all"), but i've already added a

<hibernate-mapping default-cascade="save-update">

at the beggining of the mapping.
I had to make it this way:

Setor set =
(Setor) session.load(Setor.class, new Long(paf.getId()));
logger.info("O valor do id eh :"+ paf.getId());
Quadra quadra = new Quadra();
quadra.setNome(paf.getNome());
logger.info("O nome da quadra nova: "+paf.getNome());
quadra.setComentario(paf.getComentario());
logger.info("O coment


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 2:10 pm 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
now i face another issue:

by using this code
Setor set =
(Setor) session.load(Setor.class, new Long(paf.getId()));
logger.info("O valor do id eh :"+ paf.getId());


Quadra quadra = new Quadra();
quadra.setNome(paf.getNome());
logger.info("O nome da quadra nova: "+paf.getNome());
quadra.setComentario(paf.getComentario());
logger.info("O coment


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 2:21 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Quote:
The fact is: the data is in the database, but if i call the getQuadras method, nothing is returned


Are you sure it returns normally? It can throw an lazy initialization exception if session is already closed - easy to get if you are trying to call getQuatras from a view.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 2:26 pm 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
i'm sure about that cause i turned lazy initialiation off in the mapping file. I had the lazy instantiation problem before, which didn't let me even load the page when calling the getQuadras method the struts tags way. After turning lazy instantiation off, the page loads fine, but i istill can't get the collection printed.
may i contact u via icq?

Thanks a lot.

Ot


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 2:30 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Hey, Setor's

Code:
<key column="id_quadra"/>


must be

Code:
<key column="id_setor"/>


--------------------------------

Also, few notes not related to the problem itself:

1.
trans.commit();
session.connection().commit();
session.flush();
session.close();

an overkill! trans.commit() flushesh the session and commits connection. So

trans.commit();
session.close();

would be enough.


2. add not-null="true" to the Quadra's <many-to-one name="setor"

3. Try to use "code" button when you are composing replies with source code/mapping. This will make them much easier to read


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.