zatial som prisiel na toto, mam 3 tabulky : item, sub_item, bid. 4
triedy : Item, SubItem, Bid a triedy pre vysledky : ResultItem.
v triede Item je namapovana kolekcia subitems a bids s pouzitim
@OneToMany a @JoinColumn.
private List<SubItem> subItems;
private List<Bid> bids;
pouzijem new ResultItem(...) a k tomu prislusny konstruktor.
ale neviem preco mi tam funguju len jednoduche typy, ako napr. Integer,
Date, ak pouzijem:
return getHibernateTemplate().find("select new com.app.model.ResultItem(" +
"item.createDate, item.publishDate, item.finishDate, " +
"size(item.bids), " +
"(select max(bid.bidPrice) as max from item.bids bid) " +
") from Item item where item.owner = ? " +
"group by item.id, item.createDate, item.publishDate,
item.finishDate", owner);
}
a k tomu konstruktor:
public ResultItem(Date createDate, Date publishDate, Date finishDate,
Integer numberOfBids, Double maxBid) {
this.createDate = createDate;
this.publishDate = publishDate;
this.finishDate = finishDate;
this.numberOfBids = numberOfBids;
this.maxBid = maxBid;
}
toto je vsetko ok. ale ak tam chcem doplnit dalsiu property ktoru by som
chcel mat collection (List),
return getHibernateTemplate().find("select new com.app.model.ResultItem(" +
"item.createDate, item.publishDate, item.finishDate, " +
"item.subItems, " +
"size(item.bids), " +
"(select max(bid.bidPrice) as max from item.bids bid) " +
") from Item item where item.owner = ? " +
"group by item.id, item.createDate, item.publishDate,
item.finishDate", owner);
}
public FreeOfferFreightSearchResultItem(Date createDate, Date
publishDate, Date finishDate, List subItems,
Integer numberOfBids, Double maxBid) {
this.createDate = createDate;
this.publishDate = publishDate;
this.finishDate = finishDate;
this.numberOfBids = numberOfBids;
this.maxBid = maxBid;
this.subItems = subItems;
}
toto nefunguje a vracia chybu:
org.springframework.orm.hibernate3.HibernateQueryException: Unable to
locate appropriate constructor on class [com.app.model.ResultItem]
dakujem
Ivan
jDenis wrot
Lukas Barton wrote / napísal(a):
IMHO Hibernate takto neumi pracovat s kolekcema, musel by tam dat
nejakou proxy na kolekci a nebylo by napr. snadno definovatelne, jak
se ma inicializovat apod.
Ono to ani nema velky smysl, lepsi je vratit rovnou objekt Item.
Lukas
Ivan Polák napsal(a):
dakujem, to je ono, new MyResultBean(...) a k tomu prislusny
konstruktor.
ale neviem preco mi tam funguju len jednoduche typy, ako napr.
Integer, Date.
napr. konstruktor:
MyResultBean(Integer i1, Date d1) a pouzijem:
select new com.aplikacia.model.MyResultBean(item.c, item.createDate)
je vsetko OK, ale ak pouzijem
MyResultBean(Integer i1, Date d1, List<Bid> bids)
a
select new com.aplikacia.model.MyResultBean(item.c, item.createDate,
item.bids)
vypisuje Unable to locate appropriate constructor on class
[com.aplikacia.model.MyResultBean] napriek tomu ze dany konstruktor mam.
Ivan
Lukas Barton wrote / napísal(a):
Ahoj,
select* new cz.java.ItemStatBean(*item.createDate,
size(item.bids), (select max(bid.bidPrice) from item.bids bid)*)*
from Item ...
Lukas
Ivan Polák napsal(a):
dakujem za rady, da sa povedat ze vsetky som pouzil :-)
vysledok:
getHibernateTemplate().find("select item.createDate,
size(item.bids), " +
"(select max(bid.bidPrice) from item.bids bid) " +
"from Item item where item.owner = ? group by
item.id, item.createDate", owner);
teda ako radil Lukas spravil som manapovanu kolekciu bids v item.
mam ale doplnujucu otazku, ako potom takyto vysledok namapovavate
na bean, ako sa by sa spravne mal namapovat vysledok kde sa vracia
napr. max, min, avg a podobne.
vsetkym dakujem!
Ivan
Lukas Barton wrote / napísal(a):
Napr.:
select max(bid.bidPrice),count(bid.id) from Bid bid group by
bid.item.id
Zajimavejsi zapis bude, kdyz ma Item mapovanou kolekci bids (ale
neznam vase UC, tak nevim, zda je to rozumne):
a) select max(bid.bidPrice), count(bid.id) from Item item join
item.bids bid group by item.id
b) select sizeof(item.bids), (select max(bid.bidPrice) from
item.bids bid) from Item item
Dalsi moznosti vymyslet podle
http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html
sam.
Lukas
Ivan Polák napsal(a):
Zdravim konferenciu,
mozete mi pomoct prepisat tento select do hibernate query:
select max(b.bid_price), count(*)
from item f
join bid b on b.fk_item_id = f.id
where f.id = 10
group by f.id
triedy su Item a Bid, Item ma : id a Bid ma id, bidPrice a itemId
(pre fk_item_id).
dakujem
Ivan