• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

CollectionOfElements

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok it makes sense to me now
can we specify inverseJoinColumns in place of @Column annotation?

thanks,
krishan
 
Greenhorn
Posts: 1
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic