2011-11-22 15 views
45

Ho giocato in giro con un esempio molto semplice JPA e sto cercando di modificarlo a un database esistente. Ma non riesco a superare questo errore. (Sotto.) Dev'essere solo una cosa semplice che non vedo.JPA mappatura: "QuerySyntaxException: foobar non è mappato ..."

org.hibernate.hql.internal.ast.QuerySyntaxException: FooBar is not mapped [SELECT r FROM FooBar r] 
    org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180) 
    org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110) 
    org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93) 

Nella classe DocumentManager sotto (un semplice servlet, come questo è il mio obiettivo di destinazione) fa due cose:

  1. inserire una riga
  2. restituire tutte le righe

Il l'inserimento funziona perfettamente - tutto va bene lì. Il problema è con il recupero. Ho provato tutti i tipi di valori per i parametri Query q = entityManager.createQuery, ma senza fortuna, e ho provato varie annotazioni più esplicative della classe (come i tipi di colonna), tutte senza successo.

Si prega di salvare da me stesso. Sono certo che è qualcosa di piccolo. La mia inesperienza con JPA mi impedisce di andare oltre. file di

mio ./src/ch/geekomatic/jpa/FooBar.java:

@Entity 
@Table(name = "foobar") 
public class FooBar { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    @Column(name="id") 
    private int id; 

    @Column(name="rcpt_who") 
    private String rcpt_who; 

    @Column(name="rcpt_what") 
    private String rcpt_what; 

    @Column(name="rcpt_where") 
    private String rcpt_where; 

    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 

    public String getRcpt_who() { 
     return rcpt_who; 
    } 
    public void setRcpt_who(String rcpt_who) { 
     this.rcpt_who = rcpt_who; 
    } 

    //snip...the other getters/setters are here 
} 

La mia classe ./src/ch/geekomatic/jpa/DocumentManager.java

public class DocumentManager extends HttpServlet { 
    private EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ch.geekomatic.jpa"); 

    protected void tearDown() throws Exception { 
     entityManagerFactory.close(); 
    } 

    @Override 
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 
     FooBar document = new FooBar(); 
     document.setRcpt_what("my what"); 
     document.setRcpt_who("my who"); 

     persist(document); 

     retrieveAll(response); 
    } 

    public void persist(FooBar document) { 
     EntityManager entityManager = entityManagerFactory.createEntityManager(); 
     entityManager.getTransaction().begin(); 
     entityManager.persist(document); 
     entityManager.getTransaction().commit(); 
     entityManager.close(); 
    } 

    public void retrieveAll(HttpServletResponse response) throws IOException { 
     EntityManager entityManager = entityManagerFactory.createEntityManager(); 
     entityManager.getTransaction().begin(); 

     // *** PROBLEM LINE *** 
     Query q = entityManager.createQuery("SELECT r FROM foobar r", FooBar.class); 
     List<FooBar> result = q.getResultList(); 

     for (FooBar doc : result) { 
      response.getOutputStream().write(event.toString().getBytes()); 
      System.out.println("Document " + doc.getId() ); 
     } 
     entityManager.getTransaction().commit(); 
     entityManager.close(); 
    } 
} 

Il {tomcat-casa} /webapps/ROOT/WEB-INF/classes/METE-INF/persistance.xml presentare

<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
    version="2.0"> 

<persistence-unit name="ch.geekomatic.jpa"> 
    <description>test stuff for dc</description> 

    <class>ch.geekomatic.jpa.FooBar</class> 

    <properties> 
     <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> 
     <property name="javax.persistence.jdbc.url"  value="jdbc:mysql://svr:3306/test" /> 
     <property name="javax.persistence.jdbc.user"  value="wafflesAreYummie" /> 
     <property name="javax.persistence.jdbc.password" value="poniesRock" /> 

     <property name="hibernate.show_sql"  value="true" /> 
     <property name="hibernate.hbm2ddl.auto" value="create" /> 
    </properties> 

</persistence-unit> 
</persistence> 

La descrizione della tabella MySQL:

mysql> describe foobar; 
+------------+--------------+------+-----+---------+----------------+ 
| Field  | Type   | Null | Key | Default | Extra   | 
+------------+--------------+------+-----+---------+----------------+ 
| id   | int(11)  | NO | PRI | NULL | auto_increment | 
| rcpt_what | varchar(255) | YES |  | NULL |    | 
| rcpt_where | varchar(255) | YES |  | NULL |    | 
| rcpt_who | varchar(255) | YES |  | NULL |    | 
+------------+--------------+------+-----+---------+----------------+ 
4 rows in set (0.00 sec) 

risposta

120

JPQL principalmente è case-insensitive. Una delle cose che distingue tra maiuscole e minuscole è i nomi delle entità Java. Modifica la tua richiesta a:

"SELECT r FROM FooBar r" 
+3

Quindi, il nome della classe e non il nome della tabella? –

+27

Sì.Anche se sembra sql, il tuo linguaggio di interrogazione JPA in esecuzione; quindi stai selezionando da Entità, non da tabelle. – Chris

+11

*** Sei il mio eroe per il mese di dicembre 2011 *** Grazie. Ho saltato più di un'ora su questo ... –

14

C'è anche un'altra possibile fonte di questo errore. In alcuni J2EE/contenitori web (nella mia esperienza sotto Jboss 7.xe Tomcat 7.x) È necessario aggiungere ogni classe che si desidera utilizzare come un letargo Entità nel file persistence.xml come

<class>com.yourCompanyName.WhateverEntityClass</class>

In caso di jboss, ciò riguarda ogni classe di entità (locale, ovvero all'interno del progetto che si sta sviluppando o in una libreria). Nel caso di Tomcat 7.x questo riguarda solo le classi di entità all'interno delle librerie.

3

Lei ha dichiarato la propria classe come:

@Table(name = "foobar") 
public class FooBar { 

È necessario scrivere il nome della classe per la ricerca.
da FooBar

Problemi correlati