Exception beim Erstellen einer Query

markai

Aktives Mitglied
Würde gerne den Benutzern meiner Webanwendung ermöglichen meine Datenbank zu durchsuchen. Dazu habe ich (zum Testen) folgendes gemacht:
Java:
@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.
Java:
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? ;(
 
Versuch mal das hier :
Java:
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
 
Java:
List results = getFacade().getEntityManager().createQuery("select r.* from recipe r").getResultList();

bringt mir das:

Java:
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)
        ...
 
Schau nochmal in der Datenbank nach, wie der Name der Tabelle ist.
Und versuch mal statt "*" einfach mal ein einzelnes Feld zu laden.
 
2.Versuch
Java:
List results = getFacade().getEntityManager().createQuery("select r from recipe r").getResultList();
 
hab ich auch schon versucht...

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

liefert mir...
Java:
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)
 
Gibt es denn die Klasse "Recipe" ?
Ist die Klasse als Entity markiert per Annotation ?

Poste mal die Klasse Recipe und dann schauen wir mal.
 
Sollt so stimmen oder?

Java:
@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
 
3.Versuch
Java:
List results = getFacade().getEntityManager().createQuery("select r from Recipe r").getResultList();
 

Neue Themen


Zurück
Oben