-->
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.  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Using native arrays
PostPosted: Wed Oct 22, 2003 6:48 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
Hi,

All my persistent classes have either direct references or native arrays. No lists, bags or sets are allowed (due the fact that I need to expose these objects via web services consumed by non-Java clients.)

E.g:

class Cat
{
Cat mommy;
Toy[] toys;

--- getters/setters
}

Now... how can I use Hibernate with this model; e.g. how to add a new toy to the Cat object?

Should I convert the Toy[] array returned from getToys() into a, say, Vector, add the Toy, convert it back to a native array and the call setToys(...) again?

What do you suggest?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 11:32 am 
Newbie

Joined: Mon Oct 20, 2003 3:03 pm
Posts: 13
I would definitely seperate my published (hard to change) web services from persistent objects ,but it looks like hibernate will work with arrays:

Collections are declared by the <set>, <list>, <map>, <bag>, <array> and <primitive-array> elements.

I've never done it.


Top
 Profile  
 
 Post subject: Arrays
PostPosted: Wed Oct 22, 2003 12:26 pm 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
I know Hibernate supports arrays. My question was how to use it. Add objects, etc...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 12:29 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
public Toy[] getToys();
public void setToys(Toy[] toys);

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Elaborate, please
PostPosted: Wed Oct 22, 2003 4:26 pm 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
"public Toy[] getToys();
public void setToys(Toy[] toys);"

How does that answer my question?

Are you saying that if I call setToys(...), then Hibernate will correctly add any new items in the array, not all of them? How? Can you elaborate?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 4:35 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Handling your array is no different with or without Hibernate.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Arrays
PostPosted: Wed Oct 22, 2003 4:46 pm 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
Christian... I'm new to Hibernate and my impression is that when I use lists or sets, Hibernate provides special implementation of these. Does it do the same "magic" for arrays?

Say I load a domain object having an array of other domain objects. Then I add another domain object to that array and save() it.

Will hibernate reckognize the new item or will it add the entire array? I don't know how to ask more explicit than this...

Thanks for any info.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 4:50 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
What do you mean by "add the whole array". Sorry, it is so much easier if you just go ahead and try it! It will do what you expect! :)

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Array
PostPosted: Wed Oct 22, 2003 5:17 pm 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
Sorry, but you are very vague.

My question is simple: What happends when I call:

myobj.setToys(toysArray);

after adding a new object to the toys array?

The documentation is thin as air when it comes to arrays, it seems.


Top
 Profile  
 
 Post subject: Wow! Sombody else using hibernate with web services
PostPosted: Wed Oct 22, 2003 7:44 pm 
Newbie

Joined: Wed Oct 08, 2003 12:00 am
Posts: 7
Wow! Sombody else using hibernate with web services!

I'm implementing a web services wrapper for a legacy DB with with Hibernate, apache Axis, and Tomcat. I would imagine that we will run across most of the same problems. I've already ran into the exact same problem with arrays.

Basically what I found was that hibernate arrays are pretty useless for what we need. This is because to use arrays your table actually needs a column which provides the index of your object in the array. The issue is discussed at:
http://www.hibernate.org/117.html#A5

What I basically had to do was have two sets of methods. A private getter and setter which took lists and were mapped via hibernate. And a public getter and setter which would be used by the web services. Using your example:

Code:
public class Cat
{
   private Cat mommy;
   private List toyList;

   private List getToyList()
   { .... }
   private void setToyList(List toyList)
   { ....}

   public Toy[] getToys()
   {
      return (Toy[])toyList.toArray(new Toy[0]);
   }

   public void setToys(Toy[] newToys)
   {
      toyList = Arrays.asList(newToys);
   }

}


Then map toyList as a bag. You can then get and set cats however you want in the public interface of your webservice, and eveything will work fine.

- Luke


Top
 Profile  
 
 Post subject: Whoa
PostPosted: Thu Oct 23, 2003 2:05 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
This is a big hole in Hibernate... I certainly don't like the idiom you present and I'd rather try something else if this is all Hibernate can do with arrays.

Thanks for the suggestion, though.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 2:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I've been following this thread in complete puzzlement. I mean ... theres nothing much to arrays ... you just use them.

Of course, arrays are naturally ineffficient, 'cos even at the bytecode level there is absolutely no way to proxy them so it is impossible to implement lazy initialization of an array in Java. This is why not many people use arrays in Hibernate.

So I dunno, its really not clear exactly what your problem is....


Top
 Profile  
 
 Post subject: Native arrays
PostPosted: Thu Oct 23, 2003 3:36 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
Gavin, I guess I can understand you puzzlement.

My problem is... due to WebServices my persistence classes can only set and return arrays, rather than lists, sets, etc.

My question is, what is the right way to add/remove items from the arrays?

How does Hibernate handle a addToy(Toy[] toys) method, when the toys array contains a new item, or one is deleted, etc. Does it go through the entire array, check the ID on each item... please elaborate.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2003 3:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ummmm ... yes?

why don't you just try it out and see what happens?

If you are using an array for an association, you might want to edit hibernate-mapping-2.0.dtd to allow the inverse attribute for the <array> element. I probably should make this change to the DTD, but I'm trying to discourage people using arrays for associations.


Top
 Profile  
 
 Post subject: Arrays
PostPosted: Thu Oct 23, 2003 4:21 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
", but I'm trying to discourage people using arrays for associations."

Ahh - please put that discouragement in the docs and explain why. That would be helpful.

Thanks.


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