-->
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.  [ 8 posts ] 
Author Message
 Post subject: Trying to associate two objects not yet saved
PostPosted: Sat Dec 27, 2003 3:58 pm 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
I have a object called Setor, which is already saved. Now, I want to add an object Quadra to Setor, and many objects Lote to Quadra. Neither Quadra or Lote are saved. The following code shows the way I'm trying to do it (I get a null pointer exception). It may give you an idea of what i want to do.

Code:
Setor set =
            (Setor) session.load(Setor.class, new Long(paf.getId()));
         int k = Integer.parseInt(paf.getQuantLotes());
         Quadra quadra = new Quadra();
         quadra.setNome(paf.getNome());
         quadra.setComentario(paf.getComentario());
         quadra.setSetor(set);
         set.getQuadras().add(quadra);
         Long newId = (Long)session.save(quadra);
         Quadra quad =
                     (Quadra) session.load(Quadra.class, newId);

         for (int i = 0; i <= k; i++){
            Lote lote = new Lote();
            lote.setNome(new Integer(i+1).toString());
            lote.setQuadra(quad);
            lote.setSetor(set);
            lote.setComentario("");
            lote.setLivre(true);
            quad.getLotes().add(lote);
            session.save(lote);   
         }
         session.flush();
         session.close();


The problem is: I'm getting a NullPointerException at line "quad.getLotes().add(lote);" (I can say that beause I'm logging it - I cut the log4j code for readable reasons).
Would anyone tell me what I'm doing wrong?

Thanks a lot

_________________
Ot


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 27, 2003 4:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh - how about initializing the collection field that probably is null and returned by getLotes() ?

It's a good place to do in e.g. a constructor ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 27, 2003 4:57 pm 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
Is it a good idea doing it with an ArrayList() object?

Thanks for the reply.

_________________
Ot


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 27, 2003 5:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh - yes, it it is a List type...

This is normal java stuff, nothing to do with hibernate ;)
Just do whatever you would normally do with fields - initialize them with some good defaults (sometimes null is a good default, but not in this case) - and then use'em ;)

The only thing hibernate wants is that your property type matches whatever you have defined in the hibernate mapping - in this case (i guess) a <bag>, <list> or something.

Note, that when hibernate gets a hold of your object, the set will be "forcefully" initialized/set with a collection that is one of hibernate's own.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 27, 2003 10:18 pm 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
Well, now I'm getting this exception:

java.lang.ClassCastException
at net.sf.hibernate.type.SetType.wrap(SetType.java:27)
at net.sf.hibernate.impl.SessionImpl.wrap(SessionImpl.java:2519)
at net.sf.hibernate.impl.SessionImpl.wrap(SessionImpl.java:2459)
at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2170)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2017)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2004)
at struts.action.CadastraQuadraAction.execute(Unknown Source)

When trying to save data. I'm using java.util.Collection in my java classes, and using <set> statements in my hibernate mapping file.
What might be wrong ?

thanks

_________________
Ot


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 28, 2003 7:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Please go read the documenation about collections !

If the property is mapped as a set, the actual type should of course also be a instance of a set (like a HashSet()).

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 28, 2003 8:38 am 
Regular
Regular

Joined: Thu Dec 25, 2003 12:33 am
Posts: 58
Location: Campina Grande, Brasil
That is correct. Actually, I got this working last night. I have some <set> mappings and some <map> mappings, for which I respectvely use Set and Map interfaces from Java Api.
I have another question. This is the mapping for the <map> collection I'm using:

<map name="lotes" inverse="true" cascase="all">
<key column="id_quadra"/>
<index column="nome" type="string" length="20" />
<one-to-many class="beans.Lote"/>
</map>

How to make this work in ways that it forbids a lotes collection with repeated (id_quadra, nome)? If I try to insert something inside lotes collection with same (id_quadra, nome) of another one already in this collection, it is normaly inserted. that is not what i want. I want something as if (id_quadra, nome) was a key.
Sorry for that, but the docs did not show me too much about this issue.

Thanks for the attention.

_________________
Ot


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 28, 2003 1:00 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Looks like you need to use composite-index


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