这是我的Hibernate Statment。它应该告诉我我的订单费用多少 或者我可以使用其他东西来获得成本吗?
public Query sum() {
Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
Query baum = session.createQuery("SELECT SUM(Anz_Normal*Preis_normal+Preis_family*Anz_family) from Bestellung,Pizza p,Kunde where p.Id = pizza and Email = 'a@f'");
transaction.commit();
return baum;
}
我在程序中运行它:
Query cost = bestellungRepository.sum();
jLabel3.setText(cost + " €");
这就是我得到的错误。我已经干了以使用createSQLQuery()但这没有帮助。
Exception in thread "AWT-EventQueue-0" org.hibernate.QueryException: could not resolve property: Id of: pizzahibernate.model.Pizza [SELECT SUM(Anz_Normal*Preis_normal+Preis_family*Anz_family) from pizzahibernate.model.Bestellung,pizzahibernate.model.Pizza p,pizzahibernate.model.Kunde where p.Id = pizza and Email = 'a@f']
at org.hibernate.QueryException.generateQueryException(QueryException.java:137)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
at com.sun.proxy.$Proxy5.createQuery(Unknown Source)
at pizzahibernate.repository.BestellungRepository.sum(BestellungRepository.java:41)
at pizzahibernate.Bestellung2.bestellung(Bestellung2.java:132)
at pizzahibernate.Auswahl.jButton1ActionPerformed(Auswahl.java:433)
at pizzahibernate.Auswahl.access$300(Auswahl.java:18)
at pizzahibernate.Auswahl$5.actionPerformed(Auswahl.java:185)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: org.hibernate.QueryException: could not resolve property: Id of: pizzahibernate.model.Pizza
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1978)
at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:367)
at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:500)
at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:649)
at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:272)
at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:219)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:126)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:121)
at org.hibernate.hql.internal.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:945)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1264)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4639)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4111)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2096)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2021)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:794)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:595)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
... 54
有人能帮助我吗?+
这是我的披萨班:
package pizzahibernate.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Pizza implements Serializable {
private static final long serialVersionUID = 6946049944294004465L;
@Id
private int id;
private String text;
@Column(name="NAME")
private String pName;
private float preis_normal;
private float preis_family;
public Pizza() {
}
public Pizza(final int Id, final String pName, final String Text,final float Preis_normal,final float Preis_family) {
this.preis_normal = Preis_normal;
this.preis_family = Preis_family;
this.id = Id;
this.text = Text;
this.pName = pName;
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = this.id;
}
public String getText() {
return text;
}
public void setText(final String Text) {
this.text = Text;
}
public String getpName() {
return pName;
}
public void setpName(final String pName) {
this.pName = pName;
}
public float getPreis_normal() {
return preis_normal;
}
public void setPreis_normal(final float Preis_normal) {
this.preis_normal = (int) Preis_normal;
}
public float getPreis_family() {
return preis_family;
}
public void setPreis_family(final float Preis_family) {
this.preis_family = (int) Preis_family;
}
}
如果我在Select查询中写了那个id,它就不会给出错误,但是我得到了输出:来自Bestellung,Pizza p,Kunde的QueryImpl(SELECT SUM(Anz_Normal Preis_normal + Preis_family Anz_family)其中p .id =披萨和电子邮件='a @ f'€
答案 0 :(得分:2)
在查询中应该 id 而不是 ID 。
Query baum = session.createQuery("SELECT SUM(Anz_Normal*Preis_normal+Preis_family*Anz_family) from Bestellung,Pizza p,Kunde where p.id = pizza and Email = 'a@f'"