如何在netbeans生成的jpa查询上传递参数



以下是netbeans在使用netbeans gui编辑器绑定jtable时生成的代码。

serverDetailsQuery = java.beans.Beans.isDesignTime() ? null : F1SoftSMSCPUEntityManager.createQuery("SELECT s FROM ServerDetails s ");    

我通过编辑更改了查询

SELECT s FROM ServerDetails s order by id desc

这很好用。现在我想通过ShortCode将查询参数传递给过滤记录,比如:

SELECT s FROM ServerDetails s where s.shortCode : filterShortCode order by id desc

短代码是文本字段,有一个按钮,如果用户键入shortCode并单击该按钮,它将过滤记录。

相关代码

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {
    bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
    F1SoftSMSCPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("F1SoftSMSCPU").createEntityManager();
        serverDetailsQuery = java.beans.Beans.isDesignTime() ? null : F1SoftSMSCPUEntityManager.createQuery("SELECT s FROM ServerDetails s order by id desc");
    serverDetailsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : serverDetailsQuery.getResultList();
    jPanel1 = new javax.swing.JPanel();
    serverDetailsPanel = new javax.swing.JPanel();
    bindButton = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    bindingDetailsTable = new javax.swing.JTable();
    blockButtonToBindingDetails = new javax.swing.JButton();
    smscBindingDetailsFilterPanel = new javax.swing.JPanel();
    filterByShortCodeTextField = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    smscBindingFilterButton = new javax.swing.JButton();

    }// Variables declaration - do not modify                     
private javax.persistence.EntityManager F1SoftSMSCPUEntityManager;
private javax.swing.JButton bindButton;
private javax.swing.JTable bindingDetailsTable;
private javax.swing.JButton blockButtonToBindingDetails;
private javax.swing.JTextField filterByShortCodeTextField;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private java.util.List<com.f1soft.SMSC.entities.ServerDetails> serverDetailsList;
private javax.swing.JPanel serverDetailsPanel;
private javax.persistence.Query serverDetailsQuery;
private javax.swing.JPanel smscBindingDetailsFilterPanel;
private javax.swing.JButton smscBindingFilterButton;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration     

感谢并愿意尽快得到专家的回复。

以下是如何将参数传递给查询的方法:

Query q = em.createQuery(jpql);
q.setParameter("filterShortCode", theParameterValue);

编辑:

上面的代码应该放在一个监听器方法中,在单击按钮时调用。它不应该放在initComponents()中,因为很明显,当调用initComponents()时,用户还没有填写文本并点击按钮:

smscBindingFilterButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String theParameterValue = filterByShortCodeTextField.getText();
        // TODO create and execute the query
    }
}

考虑到你的问题,我想你对Swing和一般事件一点也不熟悉。我建议您忘记NetBeans wysiwyg编辑器,并使用不涉及绑定和JPA的简单示例进行练习。遵循Swing教程,特别是关于事件的部分。

相关内容

  • 没有找到相关文章

最新更新