# JPA Suche



## Karkrau (13. Mai 2016)

Hallo,

ich habe ein JPA Projekt in Verbindung mit MySQL erstellt.
Zur Suche einer Geschaeftsstelle habe ich folgenden Code:


```
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //
        // Suchen
        try{
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("wf_projekt");
            EntityManager em = emf.createEntityManager();
            Query q = null;
          
            if(jTextField1.getText() != null ){
                String s = jTextField1.getText();
                q = em.createQuery("SELECT * FROM Geschaeftsstelle WHERE GsID = " + s + ";");
            }
            else if(jTextField2.getText() != null ){
                String s = jTextField2.getText();
                q = em.createQuery("SELECT * FROM Geschaeftsstelle WHERE Wohnort = " + s + ";");
            }
            else if(jComboBox1.getSelectedItem() != null){
                String s = (String) jComboBox1.getSelectedItem();
                q = em.createQuery("SELECT * FROM Geschaeftsstelle WHERE Land = " + s + ";");
            }
            else{
                q = em.createQuery("SELECT * FROM Geschaeftsstelle;");
            }
            @SuppressWarnings("unchecked")
            List<Geschaeftsstelle> gsList = q.getResultList();
          
            int r = 1;
            for(Geschaeftsstelle geschaeftsstelle : gsList){
                jTable1.setValueAt(Integer.toString(geschaeftsstelle.getGsID()), r, 1);
                jTable1.setValueAt(geschaeftsstelle.getStrasse().toString(), r, 2);
                jTable1.setValueAt(Integer.toString(geschaeftsstelle.getPlz()), r, 3);
                jTable1.setValueAt(geschaeftsstelle.getWohnort().toString(), r, 4);
                jTable1.setValueAt(geschaeftsstelle.getLand().toString(), r, 5);
                r++;
            }     
            em.close();
            emf.close();
          
            JOptionPane.showMessageDialog(null, "Daten geladen");      
          
        }
      
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "Fehler");
            System.err.println(e.getMessage());
        }
      
      
    }//GEN-LAST:event_jButton1ActionPerformed
```

Wenn ich das nun ausführe und zB mit einer ID von 3 kommt folgender Fehler:


```
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing [SELECT * FROM Geschaeftsstelle WHERE GsID = 3;].
[31, 31] A select statement must have a FROM clause.
[7, 7] The left expression is missing from the arithmetic expression.
[9, 31] The right expression is not an arithmetic expression.
[44, 46] '3;' is not a valid numeric value.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
    at gui.GeschaeftsstelleUI.jButton1ActionPerformed(GeschaeftsstelleUI.java:510)
    at gui.GeschaeftsstelleUI.access$0(GeschaeftsstelleUI.java:500)
    at gui.GeschaeftsstelleUI$1.actionPerformed(GeschaeftsstelleUI.java:159)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [SELECT * FROM Geschaeftsstelle WHERE GsID = 3;].
[31, 31] A select statement must have a FROM clause.
[7, 7] The left expression is missing from the arithmetic expression.
[9, 31] The right expression is not an arithmetic expression.
[44, 46] '3;' is not a valid numeric value.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
    ... 39 more
```

Ein bisschen Hilfe wäre nett


----------



## mrBrown (13. Mai 2016)

Du musst deine Querys mit JPQL und nicht SQL schreiben.

In deinem Fall etwa:

```
"SELECT g FROM Geschaeftsstelle g WHERE g.GsID = " + s + ";"
```


----------



## Karkrau (13. Mai 2016)

Also ich habe es geändert und dann kommt folgender Fehler:


```
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing [SELECT g FROM Geschaeftsstelle g WHERE g.GsID = 3;].
[48, 50] '3;' is not a valid numeric value.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
    at gui.GeschaeftsstelleUI.jButton1ActionPerformed(GeschaeftsstelleUI.java:510)
    at gui.GeschaeftsstelleUI.access$0(GeschaeftsstelleUI.java:500)
    at gui.GeschaeftsstelleUI$1.actionPerformed(GeschaeftsstelleUI.java:159)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [SELECT g FROM Geschaeftsstelle g WHERE g.GsID = 3;].
[48, 50] '3;' is not a valid numeric value.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
    ... 39 more
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing [SELECT g FROM Geschaeftsstelle g WHERE g.GsID = 3;].
[48, 50] '3;' is not a valid numeric value.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
    at gui.GeschaeftsstelleUI.jButton1ActionPerformed(GeschaeftsstelleUI.java:510)
    at gui.GeschaeftsstelleUI.access$0(GeschaeftsstelleUI.java:500)
    at gui.GeschaeftsstelleUI$1.actionPerformed(GeschaeftsstelleUI.java:159)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [SELECT g FROM Geschaeftsstelle g WHERE g.GsID = 3;].
[48, 50] '3;' is not a valid numeric value.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
    ... 39 more
```

Er hat anscheinend ein Problem mit der Zahl die ich als String einfüge. Der Rest scheint gelöst zu sein.


----------



## mrBrown (13. Mai 2016)

Der hat ein Problem mit dem `;`


----------



## Karkrau (13. Mai 2016)

Jo das habe ich auch festgestellt und es entfernt. Danach kommt folgende Fehlermeldung:


```
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Problem compiling [SELECT g FROM Geschaeftsstelle g WHERE g.GsID = 3].
[39, 45] The state field path 'g.GsID' cannot be resolved to a valid type.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
    at gui.GeschaeftsstelleUI.jButton1ActionPerformed(GeschaeftsstelleUI.java:510)
    at gui.GeschaeftsstelleUI.access$0(GeschaeftsstelleUI.java:500)
    at gui.GeschaeftsstelleUI$1.actionPerformed(GeschaeftsstelleUI.java:159)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [SELECT g FROM Geschaeftsstelle g WHERE g.GsID = 3].
[39, 45] The state field path 'g.GsID' cannot be resolved to a valid type.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:347)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
    ... 39 more
```


----------



## mrBrown (13. Mai 2016)

Die Fehlermeldung sagt doch, was das Problem ist...
 Ohne den Code gesehen zu haben ist es aber nur raten, vermutlich wird `GsID` mit einem Kleinbuchstaben begonnen


----------



## Karkrau (13. Mai 2016)

Das ist so richtig geschrieben:


```
CREATE TABLE Geschaeftsstelle
(
GsID INT NOT NULL AUTO_INCREMENT,
Strasse varchar(255),
Plz INT,
Wohnort varchar(255),
Land varchar(255),
PRIMARY KEY (GsID)
);
```

Das ist der SQL Code


----------



## Karkrau (13. Mai 2016)

```
q = em.createQuery("SELECT g FROM Geschaeftsstelle g");
```

Wenn ich es damit versuche kommt nur ein Eintrag im JTable und folgende Exception:


```
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5 >= 5
    at java.util.Vector.elementAt(Unknown Source)
    at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
    at sun.swing.SwingUtilities2.convertColumnIndexToModel(Unknown Source)
    at javax.swing.JTable.convertColumnIndexToModel(Unknown Source)
    at javax.swing.JTable.setValueAt(Unknown Source)
    at gui.GeschaeftsstelleUI.jButton1ActionPerformed(GeschaeftsstelleUI.java:533)
    at gui.GeschaeftsstelleUI.access$0(GeschaeftsstelleUI.java:500)
    at gui.GeschaeftsstelleUI$1.actionPerformed(GeschaeftsstelleUI.java:159)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
```

Außerdem ist es komisch, dass er wenn ich das ganze nicht auskommentiere immer nur nach der GsID sucht und nicht nach allen wie es eigtl sein sollte wenn alles leer ist.


UPDATE:
Nun funktioniert es nachdem ich die jTable verbessert habe.

Das einzige Problem ist nun dass die if-Schleife nicht funktioniert und eben die speziellere Suche.

UPDATE 2:
Das if-Problem liegt anscheinend an meinen if-Bedingungen.
Hab es an anderer Stelle getestet und es hat auch nicht funktioniert


----------



## stg (13. Mai 2016)

mrBrown hat gesagt.:


> Du musst deine Querys mit JPQL und nicht SQL schreiben.



Müssen muss man garnix 

Man kann über den EntityManager mittels EntityManager#createNativeQuery eine andere Implementierung vom Query-Interface anfragen, über welche man dann auch native SQL Statements absetzen kann.


----------



## stg (13. Mai 2016)

@Problem:
Du mixt hier ganz wüst die Verantworlichkeiten und machst offenbar alles gleichzeitig. Dein Code bzgl der Datenbank-Abfrage sollte von der View überhaupt nichts wissen. Es muss dort egal sein, wie und von wem die Daten weiterverwendet werden. Wenn du deinen Code klarer strukturierst, so ist es auch viel leichter einen Fehler einzugrenzen.

Deine aktuelle Fehlerbeschreibung "Funktioniert nicht" hilft nicht weiter. Poste deinen aktuellen Code (insbesondere auch deine Entitäts-Klasse) und schildere was genau nicht funktioniert, was du für ein Verhalten erwartest und was stattdessen geschieht.


----------



## Karkrau (13. Mai 2016)

Okay also klarer strukturierte Fragen:

1. Wie schreibe ich folgende if-Bedingungen, sodass sie funktionieren:

```
if(jTextField1.getText() != null ){
}
```
Weder mit null noch mit "" funktioniert das.

2. Folgende Query funktioniert nicht:

```
q = em.createQuery("SELECT g FROM Geschaeftsstelle g WHERE g.GsID = " + s);
```

Der Fehler der kommt lautet:

```
[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+exception']Exception[/URL] in thread "AWT-EventQueue-0" java.lang.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+illegalargumentexception']IllegalArgumentException[/URL]: An exception occurred while creating a query in EntityManager:
[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+exception']Exception[/URL] Description: Problem compiling [SELECT g FROM Geschaeftsstelle g WHERE g.GsID=3].
[39, 45] The state field path 'g.GsID' cannot be resolved to a valid type.
   at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
    at gui.GeschaeftsstelleUI.jButton1ActionPerformed(GeschaeftsstelleUI.java:510)
    at gui.GeschaeftsstelleUI.access$0(GeschaeftsstelleUI.java:500)
    at gui.GeschaeftsstelleUI$1.actionPerformed(GeschaeftsstelleUI.java:159)
    at javax.swing.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+abstractbutton']AbstractButton[/URL].fireActionPerformed(Unknown Source)
    at javax.swing.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+abstractbutton']AbstractButton[/URL]$Handler.actionPerformed(Unknown Source)
    at javax.swing.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+defaultbuttonmodel']DefaultButtonModel[/URL].fireActionPerformed(Unknown Source)
    at javax.swing.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+defaultbuttonmodel']DefaultButtonModel[/URL].setPressed(Unknown Source)
    at javax.swing.plaf.basic.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+basicbuttonlistener']BasicButtonListener[/URL].mouseReleased(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+component']Component[/URL].processMouseEvent(Unknown Source)
    at javax.swing.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+jcomponent']JComponent[/URL].processMouseEvent(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+component']Component[/URL].processEvent(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+container']Container[/URL].processEvent(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+component']Component[/URL].dispatchEventImpl(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+container']Container[/URL].dispatchEventImpl(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+component']Component[/URL].dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+container']Container[/URL].dispatchEventImpl(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+window']Window[/URL].dispatchEventImpl(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+component']Component[/URL].dispatchEvent(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+eventqueue']EventQueue[/URL].dispatchEventImpl(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+eventqueue']EventQueue[/URL].access$500(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+eventqueue']EventQueue[/URL]$3.run(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+eventqueue']EventQueue[/URL]$3.run(Unknown Source)
    at java.security.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+accesscontroller']AccessController[/URL].doPrivileged(Native[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+method']Method[/URL])
    at java.security.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+protectiondomain']ProtectionDomain[/URL]$1.doIntersectionPrivilege(Unknown Source)
    at java.security.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+protectiondomain']ProtectionDomain[/URL]$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+eventqueue']EventQueue[/URL]$4.run(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+eventqueue']EventQueue[/URL]$4.run(Unknown Source)
    at java.security.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+accesscontroller']AccessController[/URL].doPrivileged(Native[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+method']Method[/URL])
    at java.security.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+protectiondomain']ProtectionDomain[/URL]$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+eventqueue']EventQueue[/URL].dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by:[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+exception']Exception[/URL][EclipseLink-0](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException
[URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+exception']Exception[/URL] Description: Problem compiling [SELECT g FROM Geschaeftsstelle g WHERE g.GsID=3].
[39, 45] The state field path 'g.GsID' cannot be resolved to a valid type.
   at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:347)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
    ... 39 more
```

Meine Entitätsklasse ist:


```
package model;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the geschaeftsstelle database table.
 * 
 */
@Entity
@NamedQuery(name="Geschaeftsstelle.findAll", query="SELECT g FROM Geschaeftsstelle g")
public class Geschaeftsstelle implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private int gsID;

    private String land;

    private int plz;

    private String strasse;

    private String wohnort;

    public Geschaeftsstelle() {
    }

    public int getGsID() {
        return this.gsID;
    }

    public void setGsID(int gsID) {
        this.gsID = gsID;
    }

    public String getLand() {
        return this.land;
    }

    public void setLand(String land) {
        this.land = land;
    }

    public int getPlz() {
        return this.plz;
    }

    public void setPlz(int plz) {
        this.plz = plz;
    }

    public String getStrasse() {
        return this.strasse;
    }

    public void setStrasse(String strasse) {
        this.strasse = strasse;
    }

    public String getWohnort() {
        return this.wohnort;
    }

    public void setWohnort(String wohnort) {
        this.wohnort = wohnort;
    }

}
```

Diese wurde automatisch von Eclipse erstellt.


Hoffe das ganze ist nun spezifisch genug


----------



## mrBrown (13. Mai 2016)

1.
Was willst du erreichen? Vergleichen mit "" musst du mit equals, nicht mit ==/!= (oder isEmpty nutzen)

2. Dein Feld heißt `gsID`, in der Query steht `GsID`


----------



## Karkrau (13. Mai 2016)

1. Ich will erreichen, dass er die if-klammer nur ausführt wenn in dem textfield etwas drinsteht.
Ich habe es nun so gelöst:

```
if(jTextField1.getText().isEmpty() == false){
                String s = jTextField1.getText();
                q = em.createQuery("SELECT g FROM Geschaeftsstelle g WHERE g.gsID = " + s);
            }
```
Und nun funktioniert es alles.
Auch die Nummer 2 

Das einzige was nun nicht funktioniert sind die Suche nach Ort oder Land:

```
else if(jTextField2.getText().isEmpty() == false){
                String s = jTextField2.getText();
                q = em.createQuery("SELECT g FROM Geschaeftsstelle g WHERE g.wohnort = " + s);
            }
            else if(jComboBox1.getSelectedIndex() != 0){
                String s = (String) jComboBox1.getSelectedItem();
                q = em.createQuery("SELECT g FROM Geschaeftsstelle g WHERE g.land = " + s);
            }
```

Ansonsten funktioniert inzwischen alles.

Die Fehler die jeweils kommen:


```
An exception occurred while creating a query in EntityManager: 
Exception Description: Problem compiling [SELECT g FROM Geschaeftsstelle g WHERE g.wohnort = Ulm]. 
[39, 48] The basic mapping 'g.wohnort' cannot be used in conjunction with the = operator.
[51, 54] The identification variable 'Ulm' is not defined in the FROM clause
```

bzw.


```
An exception occurred while creating a query in EntityManager: 
Exception Description: Problem compiling [SELECT g FROM Geschaeftsstelle g WHERE g.land = Deutschland]. 
[39, 45] The basic mapping 'g.land' cannot be used in conjunction with the = operator.
[48, 59] The identification variable 'Deutschland' is not defined in the FROM clause.
[/java]
```


----------



## stg (13. Mai 2016)

Ich rate dir dringend dazu. dir mal ein Einsteiger-Buch zu der Thematik anzuschauen. Was du machst sieht mir mehr nach Trial&Error aus...

Zu deinem aktuellen Problem:
Statt `SELECT g FROM Geschaeftsstelle g WHERE g.wohnort = Ulm` muss es `SELECT g FROM Geschaeftsstelle g WHERE g.wohnort LIKE 'Ulm'` heißen


----------



## Karkrau (14. Mai 2016)

Okay dankeschön.
Wir hatten im Studium bisher nur JDBC und ganz kurz JPA. In unserem Projekt müssen wir nun aber mit JPA arbeiten.
Werde mir da wohl noch nen bisschen was anschauen müssen.
Trz danke an euch beide! Habt mir wirklich weitergeholfen


----------

