I've searched for it everywhere but i couldn't able find a solution that solved my problem, i'm trying to sum all the inputs from the column with JPQL
query and submit in a column called total
and after the insertion display this result in a JTextField, i've tried it in different ways but no one gave alright. Here is my resolution:
public class ViewPedidos extends javax.swing.JInternalFrame {
private DAOManager manager;
private PedidoModel model;
private MySQLPedidoDAO mspdao;
private MySQLClienteDAO mscdao;
private MySQLProdutoDAO msprdao;
private List<Produto> produtos;
private List<Pedido> pedidos;
private Pedido ped;
private Produto pro;
private boolean editavel;
public void inserirDados(Pedido ped) {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
System.out.println(dateFormat.format(date));
{ JTextField sets ...}
/*LINE GIVING THE ERROR UNDER!!! */
//float soma = mspdao.retornaSoma();
Double soma = mspdao.retornaSoma();
ped.setTotal(soma);
} }
MySQLPedidoDAO class:
public class MySQLPedidoDAO implements PedidoDAO {
public ConnectionFactory cf;
public MySQLPedidoDAO(ConnectionFactory cf) {
this.cf = cf;
}
/*
@Override
public float retornaSoma(){
cf.createEm().getTransaction().begin();
Query q = cf.createEm().createQuery("SELECT sum(valorTotal) from pedido");
float soma = (float) q.getSingleResult();
cf.createEm().getTransaction().commit();
cf.close();
return soma;
}*/
public Double retornaSoma(){
cf.createEm().getTransaction().begin();
Query q = cf.createEm().createQuery("SELECT sum(valorTotal) from pedido");
Double soma = (float) q.getSingleResult();
cf.createEm().getTransaction().commit();
cf.close();
return soma;
} }
Pedido class:
@Entity
@Table(name = "pedido")
public class Pedido implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
@Temporal(javax.persistence.TemporalType.DATE)
private Date dataCadastro;
@Column
private String nomeProduto;
@Column
private int numControle;
@Column
private int quantidade;
@Column(precision=15, scale=7)
private float valorUnitario;
@Column
private Integer codCliente;
@Column(precision=15, scale=7)
private float valorTotal;
@Column
//private float total;
private Double total;
public Pedido() {
}
public Pedido(int numControle, Date dataCadastro, String nomeProduto, float valorUnitario, float valorTotal, int quantidade, int codCliente){
this.numControle = numControle;
this.dataCadastro = dataCadastro;
this.nomeProduto = nomeProduto;
this.valorUnitario = valorUnitario;
this.valorTotal = valorTotal;
this.quantidade = quantidade;
this.codCliente = codCliente;
}
{ getters and setters }
@Override
public String toString() {
return "Pedido" + "id=" + id + ", dataCadastro=" + dataCadastro + ", nomeProduto=" + nomeProduto + ", numControle=" + numControle + ", quantidade=" + quantidade + ", valorUnitario=" + valorUnitario + ", codCliente=" + codCliente + ", valorTotal=" + valorTotal + ", total=" + getTotal() + '\n';
}
/**
* @return the total
*/
public float getTotal() {
return total;
}
/**
* @param total the total to set
*/
public void setTotal(float total) {
this.total = total;
} }
Testing it with Main class it gave me this error:
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: pedido is not mapped [SELECT sum(valorTotal) from pedido]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331)
at com.exemplo.repositorio.MySQLPedidoDAO.retornaSoma(MySQLPedidoDAO.java:115)
at com.exemplo.main.Main.retornaSoma(Main.java:117)
at com.exemplo.main.Main.main(Main.java:35)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: pedido is not mapped [SELECT sum(valorTotal) from pedido]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
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 org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
... 3 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: pedido is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:331)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3633)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3522)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)
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)
... 11 more
and Executing at the ViewPedidos form it gave me another StackTrace (after all, which object is missing on it??):
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.exemplo.view.ViewPedidos.inserirDados(ViewPedidos.java:580)
at com.exemplo.view.ViewPedidos.btInserirActionPerformed(ViewPedidos.java:406)
at com.exemplo.view.ViewPedidos.access$400(ViewPedidos.java:46)
at com.exemplo.view.ViewPedidos$5.actionPerformed(ViewPedidos.java:283)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
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:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
I've already tried to declare the retornaSoma() function with List return and put it at the insert event, the function was performed but it didn't submitted.
答案 0 :(得分:0)
现在我意识到了我的错误:
MySQLPedidoDAO:
@Override
public Double retornaSoma(){
cf.createEm().getTransaction().begin();
//Line corrected
Query q = cf.createEm().createQuery("select sum(valorTotal) from Pedido");
Double soma = (Double) q.getSingleResult();
cf.createEm().getTransaction().commit();
cf.close();
return soma;
}
&#13;
Pedido:
/**
* @return the total
*/
//Instead float
public Double getTotal() {
return total;
}
public void setTotal(Double total) {
this.total = total;
}
&#13;
终于工作了!非常感谢!