-->
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.  [ 11 posts ] 
Author Message
 Post subject: How to use Collection<String>s with annotations?
PostPosted: Thu Jan 27, 2005 6:21 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
Hibernate version: hibernate-3.0beta2, hibernate-annotations-3.0alpha2

I'm wondering how to use Collections of Strings and EJB 3.0 annotations. I have a class like the following, where I have an object that contains a List and/or Collection of Strings rather than complex objects.

Code:
import java.util.Collection;

@Entity
public class Person {
   private Collection<String> names;

   public Collection<String> getNames() {
      return names;
   }

   public void setNames(Collection<String> names) {
      this.names = names;
   }
}


I was able to use a mapping like the following using an XML configuration file to do what I want. I'm wondering if there is a way to do the same thing using annotations?

Code:
<bag name="names"
   table="name"
   lazy="true"
   order-by="name ASC">
   <key column="personid"/>
   <element column="name" type="string"/>
</bag>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 10:08 pm 
Newbie

Joined: Thu Jan 20, 2005 11:01 am
Posts: 15
My guess is that you'd need to create the appropriate Names class like so:

Code:
@Entity
public class Name implements Serializable
{
     // you'll also need an ID generator or natural composite or something
    private Person person;
    private String name;

    @ManyToOne
    public Person getPerson() { return person; }
    public void setPerson(final Person person) { this.person = person }

    public String getName() { return name; }
    public void setName(final String name{ this.this  = name }
}


Then, in your person class the following should do the trick

Code:
@Entity
public class Person {
   private Collection<Name> names;

   @OneToMany
   @JoinColumn(...)
   public Collection<Name> getNames() {
      return names;
   }

   public void setNames(Collection<Name> names) {
      this.names = names;
   }
}


If you want to maintain proper relational integrity, you'll have to use this model since the names table would need to be joined appropriately to get the names.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 11:14 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
Thanks. Yeah, that's what I was thinking. I like the other way better though. To me it seems excessive to have to define a full-blown class to do it. What you can do with the mapping file is a lot more elegant IMHO. I would think that domain classes that contain collections of primitive objects would be a pretty common occurrence.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 11:20 pm 
Newbie

Joined: Thu Jan 20, 2005 11:01 am
Posts: 15
I agree -- I've been using annotations now for a couple of weeks (Hibernate 3) and while Emmanuel's done a great job, it seems like some things will remain more "elegant" with XML mappings. But, Emmanuel may surprise us and make the annotations even more elegant as he gets more work done.

For me, I would go ahead and define the new class to track names because that would allow the relational data model to remain "sound" and useful outside of Java.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 11:30 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
Actually, I think I prefer annotations over XML in general, it's just the one case that I haven't figured out how to do yet. I have to send a shout out to Emanuel and the rest of the Hibernate team. I only recently started using Hibernate and I consider it one of the best new software products that I've used in a long time.

I always seem to have a heck of a time getting my XML files and hibernate.properties files to be found correctly. I think I almost prefer configuring things programatically.

One thing that I wish is that it would be possible to put the hibernate jar files into a shared directory such as jre/lib/ext and have them work correctly. I have several Web applications that use Hibernate and it slows down the deploy process having to copy up a larger .war file each time containing the hibernate jar files.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 31, 2005 6:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You can add this feature to JIRA but we're not keen of collection of primitive types.
You will almost always have to break it on day or another to make it a proper Entity. In your sample a name is not a simple String, it may and will for sure be more complex as soon as your app will grow up.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 31, 2005 6:20 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
But I would certainly consider a patch from you guys ;-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 2:55 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I'd like to help, but, I wouldn't know where to start. I'm guessing that you would need a new annotation for it? I'm not sure what it would even be called. I guess it would be equivalent to the "element" element in the XML configuration file...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 6:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I haven't think about it yet, but what you only need is something to describe your collection table + the column holding the primary type.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 4:50 pm 
Beginner
Beginner

Joined: Mon Aug 02, 2004 1:08 pm
Posts: 42
I'm currently porting HyperJAXB onto Hibernate 3 and JAXB 2.
The thing is that JAXB generates quite a plenty of primitive collections, which are quite naturally mapped by <element>s. It's a pity and a big problem for me that annotations do not currently support primitive collections.

I have tried to implement primitive collections by myself, but it seem to be quite uneasy.

I'd be greateful if you decide to implement this feature. It's currently a big blocker for me.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 22, 2005 5:13 pm 
Newbie

Joined: Tue Jun 14, 2005 1:57 pm
Posts: 6
I've just run into this limitation too and I'm pretty bummed out since I"ve already invested quite a bit of effort in moving our app from hibernate 2 with xdoclet tags to 3 using annotations. Guess I should have carefully gone over all the mappings we currently use to see if they were implemented as annotations, but I assumed the basics would be in there by now.

I've voted for the issue on Jira, but I guess we'll have to drop annotations for now and keep on using xdoclet. I'm pretty reluctant to convert all the uses of String valued collections to first class entities since, in our case, these simple string really aren't likely to need to "grow" as the app evolves.


Adrian


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