2013年10月21日星期一

List Using the error , solution ,

 This post last edited by the erbao_vip on 2013-10-21 21:55:19
My business methods :

@SuppressWarnings("unchecked")
@Override
public List<Goods> getGoodsUseGoodsName(String goodsName) {

System.out.println("get goods with goodsnam");
System.out.println(goodsName);
List<Goods> list = sessionFactory
.getCurrentSession()
.createQuery(
"from t_goods tg where tg.goodsName=" + "'"+goodsName+"'")
.list();

return list;
}


my action:


List<UserInfo> list = userService.isExistUser(username, password);
UserInfo u = (UserInfo) list.get(0);
System.out.println(u.getAdmin());

following error 0.0 . . . . . . . . . . . . . . .


------ Solution ------------------------------------ --------
select * from t_UserInfo u where u.username =? and u.password =? ") returns a bunch of list array
not correspond to the set of classes
should be from t_UserInfo u where u.username =? and u.password =? " Obviously Cuoa
---- - Solution --------------------------------------------
createSQLQuery original ecological sql statement is a query , the query should be inside the table name . The result is an array object .
createQuery is hql query , the query is inside the object name . The result is an array of entities .

tell you type conversion mistake.
------ For reference only -------------------------------------- -
isExistUser method returns a value to reflect removed. . .
------ For reference only -------------------------------------- -
generics. . . . . . Wrong
------ For reference only ----------------------------------- ----
UserLogin
------ For reference only --------------------- ------------------
you list.get (0) how will the return type is UserInfo, not the Goods Well
------ For reference only ---------------------------------------

posted wrong . This is ==

@Override
public List<UserInfo> isExistUser(String username, String password) {

System.out.println("get user with name");

Query query = sessionFactory
.openSession()
.createSQLQuery(
"select * from t_UserInfo u where u.username=?  and u.password=?");
query.setParameter(0, username);
query.setParameter(1, password);

@SuppressWarnings("unchecked")
List<UserInfo> list = query.list();
System.out.println(list.size());

return list;
}

------ For reference only ----------------------------------- ----
generics. . . . . . Wrong  

did not understand -
------ For reference only ---------------------------------------


amount ? ? ? ?
------ For reference only ---------------------------------------
UserAction code issues look

------ For reference only ---------------------------------- -----
I said you put generics removed, and then print it look okay baby ?
mistake in your generics on . . . . ! !
------ For reference only -------------------------------------- -
@ SuppressWarnings ("unchecked")
do not add this , we write the code cleared the red dot of light is not enough , the yellow dot description is not standardized , but also needs to change .
As for your question, you do an experiment
@ SuppressWarnings ("unchecked") / / this removed, do not
List list = query.list ();
System.out.println (list.size ());
this section , you enter
query.list () ;/ / then mouse over ; behind
while pressing
Alt + Shift + l
automatically generate a return type object, and you look at the return type is not UserInfo type. .

------ For reference only ---------------------------------- -----
type conversion errors.
------ For reference only -------------------------------------- -
s Bangding
- ----- For reference only ---------------------------------------
the above two methods are right.
1. 12 F method
2. removed @ SuppressWarnings ("unchecked"), and all the list followed by the generic
------ For reference only ---- -----------------------------------
String sql = "select * from base_menu where resourceid in (select menu_id from BASE_MENU_ROLE where role_id in (select role_res_id from BASE_USER_ROLE where user_res_id = '"+ user.getResourceId ( ) + "' ) ) " ;
Session session = super.getSession ();
List list = session.createSQLQuery (sql). list ();
------ For reference only ----------- ----------------------------

UserInfo here with Object type.
------ For reference only -------------------------------------- -
support 12th floor . Native sql hql returned list and the return is not the same .
------ For reference only -------------------------------------- -
select * from t_UserInfo u where u.username =? and u.password =?
changed to select u from t_UserInfo u where u.username =? and u.password =?
or from t_UserInfo u where u.username =? and u.password =?
your original type is detected List , a record of all the variables that you object array collection
------ For reference only --------- ------------------------------
supports 12 floor.
------ For reference only -------------------------------------- -
is for this reason

没有评论:

发表评论