-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to map a Stack?
PostPosted: Tue Jul 01, 2008 4:22 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi,

I just switched from a java.util.List to a java.util.Stack and getting now this error message:
Quote:
org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements:"



Code:
/** List to keep data about historical changes */
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @IndexColumn(name = "pos", base = 1)
    private Stack<ModificationHistoryElement> history;


Even removing @IndexColumn makes no difference.

Cause Stack implements java.util.Collection, this message makes no sense, or?

What's wrong?

Changing it to
Code:
/** List to keep data about historical changes */
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @IndexColumn(name = "pos", base = 1)
    private Collection<ModificationHistoryElement> history;


Code:
this.history = new Stack<ModificationHistoryElement>();


Solves the problem, but it looses the Stack methods, which forces unnecessary casts, which makes the code quite ugly.
Then I would prefer a normal list and would use list.get(list.size()-1) to get the last element. Also ugly, but at least less vulnerable against ClassCastExceptions.


It seems that hibernate doesn't know neither supports stacks.

Thank you.

Greetings Michael


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 01, 2008 8:51 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi,

so I switched back to List and added a method
Code:
/** Returns the last elemenet or null if history is empty! */
public ModificationHistoryElement peekLastElementOfHistory() {
   if (!this.history.isEmpty()) {
       return this.history.get(this.history.size() - 1);
   }
   return null;
}

to do the stack functionality I need.

Another solution would be to use the Collection declaration from above and to limit the cast to the the getter-function, where it should be quite safe to do so.

Code:
/**
* @return the history
*/
public Stack<ModificationHistoryElement> getHistory() {
   return (Stack<ModificationHistoryElement>) this.history;
}


If you really need a stack, the second solution would be preferable.

Greetings Michael


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