关于用了Hibernate里面实体对象问题

long331 2009-08-23 07:54:46
在工程里面有个业务对象·是根据业务而结合在一起的一个对象,这个类并没有映射关系到数据库的表中·

例如:Collection<Item> list= service.getItems("xxx");

这个的Item就是一个业务对象·但现在 查出来的结果集 里面 每一项 都是一个 Object,并没有转换成Item这个类

在没有Hibernate之前并没有发现这样的问题

请问各位怎么解决??
...全文
132 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
long331 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 long331 的回复:]
引用 8 楼 woming66 的回复:
明白了
list = (Collection <CarItem>)query.list(); //list的每一个元素是object[]


List <Item> items = new ArrayList <Item>();
for(int i=0; i <list.size(); i++) {
  Object[] o = (Object[])list.get(i);
  Item item = new Item();
  item.setXxxx(o[0]);//按照b.isbn, b.name,b.picture, b.price, s.count顺序
 
  items.add(item);
}

return items;


这回明白了吧!!!


行了现在·

其实这里有个关键地方··

List <Item> items = new ArrayList <Item>(); 就是这里

返回的是Obj数组· 改回

List <Item> items = new ArrayList <Item>();

之后你的方法就没问题了·
[/Quote]

写错了··改回List <Object> items = new ArrayList <Object>();
long331 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 woming66 的回复:]
明白了
list = (Collection <CarItem>)query.list(); //list的每一个元素是object[]


List <Item> items = new ArrayList <Item>();
for(int i=0; i <list.size(); i++) {
  Object[] o = (Object[])list.get(i);
  Item item = new Item();
  item.setXxxx(o[0]);//按照b.isbn, b.name,b.picture, b.price, s.count顺序
 
  items.add(item);
}

return items;


这回明白了吧!!!
[/Quote]

行了现在·

其实这里有个关键地方··

List <Item> items = new ArrayList <Item>(); 就是这里

返回的是Obj数组· 改回

List <Item> items = new ArrayList <Item>();

之后你的方法就没问题了·
woming66 2009-08-24
  • 打赏
  • 举报
回复
还有问题吗?
woming66 2009-08-23
  • 打赏
  • 举报
回复
Class c = Item.class;
Field[] field = c.getDeclaredFields();
for(int i=0; i<field.length; i++) {
System.out.println(field[i].getType().getName());
}
run一下 你在看一看你的Item的每个属性都是什么类型
woming66 2009-08-23
  • 打赏
  • 举报
回复
是Object[] o = (Object[])list.get(i); 这地方没出现异常吧!


item.setXxxx(o[0]);//是这个地方不对,是表明item.xxx这个属性的类型和数据库取出的类型不匹配


for(int i=0;i<o.length;i++) {

System.out.println(o[i].class.getName());
//b.isbn, b.name,b.picture, b.price, s.count 看一看这些都是什么类型!
}
woming66 2009-08-23
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 soulx 的回复:]
Java codepublic Collection<Item> getItems(String userName){
Collection<Item> list=null;

Collection<Item> rtnList=null;try{
Session session= DataBase.getSession();
Query query= session.createQuery("select b.isbn, b.name,b.picture, b.price, s.count from Books b,ShopCar s where b.isbn = s.isbn and s.userName=?");
query.setString(0, userName);
list= query.list();for(Object obj:list){
rtnList.add((Item)obj)
}
}finally{
DataBase.closeSession();
}return rtnList;
}

LZ这样试下
[/Quote]

大哥 你写得更不对!select b.isbn, b.name,b.picture, b.price, s.count from Books
能返回Item你的HQL得写成这样“select new Item(b.isbn, b.name,b.picture, b.price, s.count) from Books ”并且Item还要有对应的Constructor方法。哎 最可惜的是 Item还不是实体类!
long331 2009-08-23
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 woming66 的回复:]
是Object[] o = (Object[])list.get(i); 出现的异常
还是item.setXxxx(o[0]);出现的异常?

[/Quote]

对的·

o[0] 这样是拿不到任何数据的·
woming66 2009-08-23
  • 打赏
  • 举报
回复
是Object[] o = (Object[])list.get(i); 出现的异常
还是item.setXxxx(o[0]);出现的异常?
soulx 2009-08-23
  • 打赏
  • 举报
回复

public Collection<Item> getItems(String userName){
Collection<Item> list = null;

Collection<Item> rtnList = null;
try{
Session session = DataBase.getSession();
Query query = session.createQuery("select b.isbn, b.name,b.picture, b.price, s.count from Books b,ShopCar s where b.isbn = s.isbn and s.userName=?");
query.setString(0, userName);
list = query.list();

for(Object obj:list){
rtnList.add((Item)obj)
}
}finally{
DataBase.closeSession();
}
return rtnList;
}



LZ这样试下
woming66 2009-08-23
  • 打赏
  • 举报
回复
不好意思 我要休息了 如果还有问题就发过来 明天早上会及时的为你解答!
long331 2009-08-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 feishare 的回复:]
引用 8 楼 woming66 的回复:
明白了
list = (Collection <CarItem>)query.list(); //list的每一个元素是object[]


List <Item> items = new ArrayList <Item>();
for(int i=0; i <list.size(); i++) {
  Object[] o = (Object[])list.get(i);
  Item item = new Item();
  item.setXxxx(o[0]);//按照b.isbn, b.name,b.picture, b.price, s.count顺序
 
  items.add(item);
}

return items;


这回明白了吧!!!


直接cast不行的话,上面的方法是最直接得了
[/Quote]

Object[] o = (Object[])list.get(i);
Item item = new Item();
item.setXxxx(o[0]);//按照b.isbn, b.name,b.picture, b.price, s.count顺序

o[0] 这样是拿不出来的·

还是这样报错 java.lang.ClassCastException: [Ljava.lang.Object;
woming66 2009-08-23
  • 打赏
  • 举报
回复
如果你试过了 出现错误 请你把异常信息发过来!
woming66 2009-08-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 long331 的回复:]
Object[] o = (Object[])list.get(i);

这样是转不了的·
[/Quote]

你的hql都写成这样了 select b.isbn, b.name,b.picture, b.price, s.count from Books
它返回的就是Object[]
你试过了吗?
feishare 2009-08-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 woming66 的回复:]
明白了
list = (Collection <CarItem>)query.list(); //list的每一个元素是object[]


List <Item> items = new ArrayList <Item>();
for(int i=0; i <list.size(); i++) {
  Object[] o = (Object[])list.get(i);
  Item item = new Item();
  item.setXxxx(o[0]);//按照b.isbn, b.name,b.picture, b.price, s.count顺序
 
  items.add(item);
}

return items;


这回明白了吧!!!
[/Quote]

直接cast不行的话,上面的方法是最直接得了
long331 2009-08-23
  • 打赏
  • 举报
回复
Object[] o = (Object[])list.get(i);

这样是转不了的·
woming66 2009-08-23
  • 打赏
  • 举报
回复
明白了
list = (Collection<CarItem>)query.list(); //list的每一个元素是object[]


List<Item> items = new ArrayList<Item>();
for(int i=0; i<list.size(); i++) {
Object[] o = (Object[])list.get(i);
Item item = new Item();
item.setXxxx(o[0]);//按照b.isbn, b.name,b.picture, b.price, s.count顺序

items.add(item);
}

return items;



这回明白了吧!!!
long331 2009-08-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 woming66 的回复:]
service.getItems("xxx") 是从哪得到的Collection? 是数据库吗?还是从什么地方?

Hibernate query.list();方法返回的List都是object型的

你把代码发过来吧!!!
[/Quote]

是从数据库拿出来的

public Collection<Item> getItems(String userName){
Collection<Item> list = null;
try{
Session session = DataBase.getSession();
Query query = session.createQuery("select b.isbn, b.name,b.picture, b.price, s.count from Books b,ShopCar s where b.isbn = s.isbn and s.userName=?");
query.setString(0, userName);
list = (Collection<CarItem>)query.list();
}finally{
DataBase.closeSession();
}
return list;
}


当遍历这个List的时候 想拿里面的item都变成Object了·
要怎么才能转回来?
lcj_up 2009-08-23
  • 打赏
  • 举报
回复
需要cast
long331 2009-08-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 daisycool 的回复:]
在service.getItems里返回的时候需要cast:

public Collection <Item> getItems(...){
  return (Collection <Item>) myService.getItemsByName("name");
}
[/Quote]

你这样转换我试过是不行的
daisycool 2009-08-23
  • 打赏
  • 举报
回复
在service.getItems里返回的时候需要cast:

public Collection<Item> getItems(...){
return (Collection<Item>) myService.getItemsByName("name");
}

加载更多回复(3)

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧