-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problem setting object array
PostPosted: Fri Jun 09, 2006 3:38 am 
Beginner
Beginner

Joined: Mon May 22, 2006 12:12 am
Posts: 23
Hi All,


With you all guys' help and explanations, I'm "almost" done to my basic needs on setting one-to-many relationships.


My Class ....
public class LegalPerson
{

private Address[] _addresses;

public virtual Address[] addresses
{
get { return _addresses; }
set { _addresses = value; }
}

}


Implementation code of the class..............The error is at the line : "Address[] addrs = new Address[]{ad1, ad2};":

LegalPerson lp = new LegalPerson();
lp.name = "LegalPersonFact";

Address ad1 = new Address();
ad1.bldg = "Build1";
Address ad2 = new Address();
ad2.bldg = "Build2";

sess.Save(ad1);
sess.Save(ad2);

Address[] addrs = new Address[]{ad1, ad2};
lp.addresses = addrs;

sess.Save(lp);
.............................

I'm not familiar with C#. Should I set value to an object array like this?

Maybe is it necessary that I need to follow some other book's getter/setter approach like :

public Address[] getAddresses() {
return addresses;
}

public Address[] setAddresses(Address[] addresses) {
this.addresses = addresses;
}

Is it a must in my case? Even so, I don't know my NHibernate seems not working when I using the that approach (vs. my approach just to assign values to the properties themselves)


Thanks a lot!
jL


Top
 Profile  
 
 Post subject: Re: Problem setting object array
PostPosted: Fri Jun 09, 2006 3:56 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
fly2moon2 wrote:
Hi All,


With you all guys' help and explanations, I'm "almost" done to my basic needs on setting one-to-many relationships.


My Class ....
public class LegalPerson
{

private Address[] _addresses;

public virtual Address[] addresses
{
get { return _addresses; }
set { _addresses = value; }
}

}



NHibernate requires the property to be from "correct" type in case of association, depending on the mapping, ILIst, ICollection, Iesi.Collection.ISet, IDictionary, ... (maybe I missed something)

Anyway, it is not adviseable to to use array proeprties like You do. In followng code

Address[] addrs = person.addresses;
addr[0] = new Address();

the content of person.addresses is changed.

You might want to run fxcop on Your assembly and see what it thinks about different constructs.

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 9:57 am 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
You also shouldnt use a setter for the collection property (almost always). Use a read-only property and use access="nosetter.camelcase" (for example) in your mapping file.


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