File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes CollectionOfElements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "CollectionOfElements" Watch "CollectionOfElements" New topic
Author

CollectionOfElements

Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32
Hi All,

I am using hibernate as persistence provider for EJB. So generally I use annotations defined in JPA spec i.e OnetoMany , ManyToMany etc. I have seen in some places people using CollectionOfElements.

can somebody tell me the use of @org.hibernate.annotations.CollectionOfElements?
I am having trouble understanding how it works?

I checked the documentation which simply says Annotation used to mark a collection as a collection of elements or a collection of embedded objects.

under what scenario will we use this annotation?

is this similar to @OneToMany , @ManyToMany annotations?

Where will one go for it instead of OneToMany? Do we need any other meta-data also for CollectionOfElements?


thanks,
krishan

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Kirshan, please click on the My Profile link above and change your display name to meet the JavaRanch Naming Policy of using your real first and real last names.

Members that violate our policy can have accounts suspended or removed.

Thanks for understanding.

Also please don't duplicate post. I closed your dup thread.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Basically, the CollectionOfElements are for objects in a Collection that aren't mapped as Entities. They are dependent or @Embeddable objects.

So for instance, I have a User object and a User has a List of Address objects, but Address is not mapped as an @Entity, but as an @Embeddable. With this in mind, there is no @OneToMany that you can use, you would need to use @CollectionOfElements instead. Sometimes it is also used in the case where the Collection doesn't hold @Entity objects, but say some part of an Object, like a List of Strings where the String is some property of another object. Like I have a List of States, and in my collection that has the @CollectionOfElements it is only storing the State name string instead of a List of State objects.

Hope that helps

@CollectionOfElements is much more rare to need.

Mark
Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32
Thanks Marc for your reply

I have changed my name. Also I wanted to delete the duplicate post myself, couldn't find a way to do that. Sorry about that.

I got your point that we use CollectionOfElements in case of non-entities or embeddable types. So, it can be used in case of enumerated type also.
Also, i have seen in some examples that ppl don't use inverseJoinColumn in JoinTable annotation, instead they use Column annotation
Below is the sample code:



Here Permission is enum. What is the purpose of @Column annotation here?

Also is it used to mark OneToMany relationship or can it be used for all the possible 7 relationships?

Further, if we don't provide any mapping information between Entities, does persistence provider has anyway to figure out the relationship?
Also, if we don't map entities correctly how does persistence provider validates this?


thanks,
krishan


Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

"Also, if we don't map entities correctly how does persistence provider validates this?"

Oh if only it really could at compile time. No it will be a runtime error, either at SessionFactory creation or the first time it tries to be loaded.

So the Column is because in the database they have a field that say has an int, that correlates to the Enum position or value, and that is how it gets set. Or that a "Many" of records relate to Many values in the Enum. Just as an example.

Mark
Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32
Marc,
Thanks for your reply.

permission_id is a column in database table Permission and not in table user. But the code reffered in my previuos post is in User entity.
So, when we specify column annotation on field or getter, persistence provider maps that field to the column in table referred by entity. So, here persistence provider should try to map the field to column permission_id in User table which is not present. isn't it?

why can't we specify inverseJoinColumn attribute here?

Also, when we specify @CollectionOfElements, how does the persistence provider finds out if it a OneToMany or ManyToMany annotation?


thanks,
krishan
tabiul mahmood
Greenhorn

Joined: Sep 17, 2009
Posts: 18
Krishna, I think you did not read Mark's reply properly
So for instance, I have a User object and a User has a List of Address objects, but Address is not mapped as an @Entity, but as an @Embeddable. With this in mind, there is no @OneToMany that you can use, you would need to use @CollectionOfElements instead


So all this saying you will use @CollectionOfElements for cases where your collection is not an Entity, thus no question of @OneToMany or @ManyToOne
Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32
ok it makes sense to me now
can we specify inverseJoinColumns in place of @Column annotation?

thanks,
krishan
Gajendra Mangaraja
Greenhorn

Joined: Jan 12, 2011
Posts: 1

Mark Spritzler wrote:Basically, the CollectionOfElements are for objects in a Collection that aren't mapped as Entities. They are dependent or @Embeddable objects.

So for instance, I have a User object and a User has a List of Address objects, but Address is not mapped as an @Entity, but as an @Embeddable. With this in mind, there is no @OneToMany that you can use, you would need to use @CollectionOfElements instead. Sometimes it is also used in the case where the Collection doesn't hold @Entity objects, but say some part of an Object, like a List of Strings where the String is some property of another object. Like I have a List of States, and in my collection that has the @CollectionOfElements it is only storing the State name string instead of a List of State objects.

Hope that helps

@CollectionOfElements is much more rare to need.

Mark


Is it mandatory to have this annotation @CollectionOfElements
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CollectionOfElements