-->
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: Objects with Objects
PostPosted: Fri Aug 19, 2005 3:43 pm 
Beginner
Beginner

Joined: Mon Apr 18, 2005 10:25 am
Posts: 38
Location: Maryland
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0

Mapping documents:


Code between sessionFactory.openSession() and session.close():
Be gentle with me as I am new, have read the docs and just need clarification.

I have an object: Run this object has a number of params to include several other objects. One of those I call Status. I have a get and set for the status in the Run.java. I define the fields in the Run.hbm.java with <many-to-one name="status" column="stausID" cascade="all"/>

I get a bit confused on the many naming so what I want is that A run has A status but that Status may belong to many other runs. In the Run.java I am trying to add the status like this:

public void addStatus(Status statusObj)
{
this.getStatus().add(statusObj);
}

In my business method:
I do:
Run run = new Run();
Status status = new Status();
status.setStatus("NONE");
run.addStatus(status);

I can't compile b/c if can not resolve the this.getStatus().add(..); Am I traveling down the right path here? What am I missing? I can't find where the method .add() is defined? I found this in an example and it is not explicitly defined anywhere there either. I thought it might be a Java method but can't seem to find one for the Object class? Any help?

On a side note, is there a better way to do this? I have a number of instances where this scenario occur. I have objects with relationships to other objects. Thanks.
Full stack trace of any exception that occurs:
Can't resolve: this.getStatus().add(statusObj);

Name and version of the database you are using:
Sybase ASE 12.5.5

Debug level Hibernate log excerpt:
DEBUG


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 4:08 pm 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
It looks like you're missing a Collection (like Set for instance) called "status". "add()" would generally indicate that kind of thing.

One other note, not that it has anything to do with your posted problem. Don't do this:
Code:
this.getStatus().add(statusObj);


The "this" really shouldn't be there. Just use:
Code:
getStatus().add(statusObj);


Top
 Profile  
 
 Post subject: Re: Objects with Objects
PostPosted: Fri Aug 19, 2005 4:13 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
First, you don't want cascade="all" because when you save or delete a Run object, you don't want to add/delete the Status object.

If a single status can belong to more than 1 Run, then it would be assumed that this reference data already exists.


Second, since there is only 1 Status, you're not adding it, your assigning it.

Code:
public class Run {

   private Status status;

   public Status getStatus() { return status};

   public vode setStatus(Status status) {this.status = status};
}


and your business method looks like this:

Code:
Run run = new Run();
Status status = // look up status from database.
run.setStatus(status);

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 4:31 pm 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
Oh yeah. I saw the .add() call and immediately thought of a Collection. Clearly from the mapping this is not the intention. My mistake there.

I'm sticking by my assertion against using "this.method()" calls though.


Top
 Profile  
 
 Post subject: To pksiv
PostPosted: Tue Aug 23, 2005 1:58 pm 
Beginner
Beginner

Joined: Mon Apr 18, 2005 10:25 am
Posts: 38
Location: Maryland
If I don't want cascade="all" which other option do you suggest? I want the status objects to exist in the db status table but for those to be associated with a run at a particular point. I did get the assignment down though. 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.