-->
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.  [ 15 posts ] 
Author Message
 Post subject: List contains objects not Dept objects??
PostPosted: Mon Apr 13, 2009 4:17 am 
Newbie

Joined: Tue Mar 24, 2009 3:47 am
Posts: 9
This is my code

List tempDepartments = new ArrayList<Dept>();
tempDepartments = session.createQuery("from Dept").list();

for(Dept d : tempDepartments)
{
departments.add(new SelectItem(d.getDept_id(),d.getDept_name()));
}

the error that Iam getting is that it says that the line (for(Dept d : tempDepartments)) contains incompatible types

[javac] found : java.lang.Object
[javac] required: com.timekeeper.model.Dept
[javac] for(Dept d : tempDepartments)

okay now Iam using:

List<Dept> tempDepartments = new ArrayList<Dept>();

tempDepartments = session.createQuery("from Dept").list();

for(Dept d : tempDepartments)
{
departments.add(new SelectItem(d.getDept_id(),d.getDept_name()));
}

It works but Iam getting a warning saying that Iam using unsafe constructs??


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 5:22 am 
Beginner
Beginner

Joined: Wed Sep 21, 2005 8:18 am
Posts: 31
Type cast it to List<Dept> will remove warning e..g

tempDepartments = (List<Dept>)session.createQuery("from Dept").list();

_________________
amer sohail


Top
 Profile  
 
 Post subject: warning: [unchecked] unchecked cast
PostPosted: Mon Apr 13, 2009 5:50 am 
Newbie

Joined: Tue Mar 24, 2009 3:47 am
Posts: 9
Now Iam getting

warning: [unchecked] unchecked cast


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 5:04 pm 
Beginner
Beginner

Joined: Wed Aug 11, 2004 3:50 pm
Posts: 26
I noticed that Hibernate doesn't cast collections according to their member types. What you are experiencing is clearly a symptom of this behavior.

For example, when I allowed Hibernate Tools to generate the classes for me, it expressed the relationships with no regard to type. So, if you had a Company class that has a many-to-one relationship to Dept, it would generate it as 'private Set depts' instead of 'private Set<Dept> depts'. That will obviously trigger warnings with Java 5 or 6.

I don't know if there is a good way to change that behavior. I've even considered modifying the generated classes to show the type, but I am concerned that it might interfere with Hibernate's behavior.

For now, I think your best choice is to cast. You can always use the annotation @SuppressWarnings("unchecked") to silence the warning.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 5:08 pm 
Newbie

Joined: Sat Mar 28, 2009 5:47 pm
Posts: 14
tempDepartents should also be:
Code:
List<Dept> tempDepartments = new ArrayList<Dept>();


Then, as mentioned above, this will work without warnings::
Code:
tempDepartments = (List<Dept>)session.createQuery("from Dept").list();


Top
 Profile  
 
 Post subject: Actualy is a Java problem
PostPosted: Mon Apr 13, 2009 5:16 pm 
Newbie

Joined: Mon Apr 13, 2009 4:36 pm
Posts: 2
Thsi is a java problem, not an hibernate problem, the warning is because of the Typer Erasure, you shoul'd (or could) add the annotation @SuppressWarnings("unchecked"), to the method that contains that call, is the cleanest way to solve this problem.


Top
 Profile  
 
 Post subject: It is working now just giving off the warning
PostPosted: Mon Apr 13, 2009 10:26 pm 
Newbie

Joined: Tue Mar 24, 2009 3:47 am
Posts: 9
I will come back to it later. Thanks for the help guys


Top
 Profile  
 
 Post subject: It is working now just giving off the warning
PostPosted: Mon Apr 13, 2009 10:28 pm 
Newbie

Joined: Tue Mar 24, 2009 3:47 am
Posts: 9
I will come back to it later. Thanks for the help guys


Top
 Profile  
 
 Post subject: It is working now just giving off the warning
PostPosted: Mon Apr 13, 2009 10:29 pm 
Newbie

Joined: Tue Mar 24, 2009 3:47 am
Posts: 9
I will come back to it later. Thanks for the help guys


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 11:10 pm 
Newbie

Joined: Mon Apr 13, 2009 11:05 pm
Posts: 1
thank you so much!
dossier de surendettement


Top
 Profile  
 
 Post subject: Re: List contains objects not Dept objects??
PostPosted: Fri May 01, 2009 6:47 pm 
Newbie

Joined: Fri May 01, 2009 6:43 pm
Posts: 1
Hi all,

Thanks a lot for this thread. I found it really interesting and usefull. By the way, I have to say that this forum is amazing!

David LeeSimulation pret immobilier


Top
 Profile  
 
 Post subject: Re: List contains objects not Dept objects??
PostPosted: Wed May 06, 2009 3:59 am 
Newbie

Joined: Wed May 06, 2009 3:57 am
Posts: 1
simulation emprunt immobilier

It is a nice post. I got it very useful. Thanks!


Top
 Profile  
 
 Post subject: Re: List contains objects not Dept objects??
PostPosted: Wed May 06, 2009 7:58 am 
Newbie

Joined: Wed May 06, 2009 7:55 am
Posts: 1
had same problem. thanks hibernate forum.
pret personnel


Top
 Profile  
 
 Post subject: Re: List contains objects not Dept objects??
PostPosted: Sat May 09, 2009 6:18 am 
Newbie

Joined: Sat May 09, 2009 6:14 am
Posts: 4
Thanks for this information. You are very techie. I hope I will become like you are. Currently I am practicing coding.

Simulation pret immobilier


Top
 Profile  
 
 Post subject: Re: List contains objects not Dept objects??
PostPosted: Wed Jul 01, 2009 4:28 am 
Newbie

Joined: Wed Jul 01, 2009 4:18 am
Posts: 1
thanks for your help

surendettement


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