# Exception beim Erstellen einer Query



## markai (8. Jun 2012)

Würde gerne den Benutzern meiner Webanwendung ermöglichen meine Datenbank zu durchsuchen. Dazu habe  ich (zum Testen) folgendes gemacht: 

```
@ManagedBean(name = "recipeController")
@SessionScoped
public class RecipeController implements Serializable {
        ...
        public String find(){
        List results = getFacade().getEntityManager().createQuery("select * from recipe").getResultList();
        System.out.println(results.size());
        return null;
        }
```

Jedoch bekomme ich folgende Exception.

```
WARNING: #{recipeController.find}: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing the query [select * from recipe;], line 1, column 7: unexpected token [*].
Internal Exception: NoViableAltException(93@[330:16: ( DISTINCT )?])
javax.faces.FacesException: #{recipeController.find}: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing the query [select * from recipe;], line 1, column 7: unexpected token [*].
Internal Exception: NoViableAltException(93@[330:16: ( DISTINCT )?])
	at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
	at javax.faces.component.UICommand.broadcast(UICommand.java:315)
	at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
	at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
	at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
```

Führe ich die query direkt auf meiner db aus liefert sie ergebnisse. Was mache ich jetzt schon wieder falsch? ;(


----------



## diel2001 (8. Jun 2012)

Versuch mal das hier : 

```
List results = getFacade().getEntityManager().createQuery("select r.* from recipe r").getResultList();
```
Und wenn das nicht geht, debugge mit eclipse und führe den Befehl in dem Display


----------



## markai (8. Jun 2012)

```
List results = getFacade().getEntityManager().createQuery("select r.* from recipe r").getResultList();
```

bringt mir das:


```
WARNING: #{recipeController.find}: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing the query [select r.* from recipe r], line 1, column 9: unexpected token [*].
Internal Exception: org.eclipse.persistence.internal.jpa.parsing.jpql.InvalidIdentifierException
javax.faces.FacesException: #{recipeController.find}: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing the query [select r.* from recipe r], line 1, column 9: unexpected token [*].
Internal Exception: org.eclipse.persistence.internal.jpa.parsing.jpql.InvalidIdentifierException
	at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
	at javax.faces.component.UICommand.broadcast(UICommand.java:315)
	at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
	at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
	at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
	at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
        ...
```


----------



## diel2001 (8. Jun 2012)

Schau nochmal in der Datenbank nach, wie der Name der Tabelle ist. 
Und versuch mal statt "*" einfach mal ein einzelnes Feld zu laden.


----------



## diel2001 (8. Jun 2012)

2.Versuch

```
List results = getFacade().getEntityManager().createQuery("select r from recipe r").getResultList();
```


----------



## markai (8. Jun 2012)

hab ich auch schon versucht...


```
List results = getFacade().getEntityManager().createQuery("select r from recipe r").getResultList();
```

liefert mir...

```
WARNING: #{recipeController.find}: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Error compiling the query [select r from recipe r]. Unknown entity type [recipe].
javax.faces.FacesException: #{recipeController.find}: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Error compiling the query [select r from recipe r]. Unknown entity type [recipe].
	at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
	at javax.faces.component.UICommand.broadcast(UICommand.java:315)
	at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
	at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
	at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
```


----------



## diel2001 (8. Jun 2012)

Gibt es denn die Klasse "Recipe" ? 
Ist die Klasse als Entity markiert per Annotation ?

Poste mal die Klasse Recipe und dann schauen wir mal.


----------



## markai (8. Jun 2012)

Sollt so stimmen oder?


```
@Entity
public class Recipe implements Serializable, Comparable<Recipe> {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(nullable=false) @NotBlank
    private String name;
    @Column(nullable=false) @Lob @NotBlank
    private String description;
    ...

    // getter u setter
```


----------



## diel2001 (8. Jun 2012)

3.Versuch

```
List results = getFacade().getEntityManager().createQuery("select r from Recipe r").getResultList();
```


----------



## markai (8. Jun 2012)

OMG es funktioniert!! :toll: Schaut so aus als wäre das ganze case sensitive...
Ich danke dir vielmals!! :applaus:


----------

