2014-04-11 20 views
38

Mam przykład aplikacji webowej Hibernate 4.3.5 + baza danych Derby 10.10.1.1+ Glassfish4.0 z IDE NetBeans 8.0Beta.org.hibernate.hql.internal.ast.QuerySyntaxException: tabela nie jest mapowana

mam następnego wyjątek:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: CUSTOMERV is not mapped 
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189) 
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109) 
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95) 
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:331) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3633) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3522) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299) 
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247) 
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278) 
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206) 
... 72 more 

postaci z index.xhtml

<h:panelGrid id="panel1" columns="2" border="1" 
       cellpadding="5" cellspacing="1"> 
     <f:facet name="header"> 
      <h:outputText value="Add Customer Information"/> 
     </f:facet> 
     <h:outputLabel value="First Name:"/> 
     <h:inputText value="#{customer.firstName}" id="fn"/> 
     <h:outputLabel value="Last Name:"/> 
     <h:inputText value="#{customer.lastName}" id="ln"/> 
     <h:outputLabel value="Email:"/> 
     <h:inputText value="#{customer.email}" id="eml"/> 
     <h:outputLabel value="Date of Birth:"/> 
     <h:inputText value="#{customer.sd}" id="s"/> 
     <f:facet name="footer"> 
      <h:outputLabel value="#{customer.msg}" id="msg" styleClass="msg"/> 
      <h:commandButton value="Save" action="#{customer.saveCustomer}"> 
      </h:commandButton> 
     </f:facet> 
    </h:panelGrid> 

Customer.java

package com.javaknowledge.entity; 

    import com.javaknowledge.dao.CustomerDao; 
    import java.text.ParseException; 
    import java.text.SimpleDateFormat; 
    import java.util.ArrayList; 
    import java.util.Date; 
    import java.util.List; 
    import javax.faces.bean.ManagedBean; 
    import javax.faces.bean.SessionScoped; 
    import javax.persistence.*;  

    @ManagedBean 
    @SessionScoped 

    public class Customer implements java.io.Serializable { 

    private Integer custId; 
    private String firstName; 
    private String lastName; 
    private String email; 
    private Date dob; 
    private String sd, msg, selectedname; 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 


    public Customer() { 
    } 

    public Customer(String firstName, String lastName, String email, Date dob) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.email = email; 
     this.dob = dob; 
    } 

    public String getSd() { 
     return sd; 
    } 

    public void setSd(String sd) { 
     this.sd = sd; 
    } 

    public Integer getCustId() { 
     return this.custId; 
    } 

    public void setCustId(Integer custId) { 
     this.custId = custId; 
    } 

    public String getFirstName() { 
     return this.firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return this.lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 
    @Column(name = "EMAIL") 
    public String getEmail() { 
     return this.email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    @Column(name = "DOB") 
    public Date getDob() { 
     return this.dob; 
    } 

    public void setDob(Date dob) { 
     this.dob = dob; 
    } 

    public String getMsg() { 
     return msg; 
    } 

    public void setMsg(String msg) { 
     this.msg = msg; 
    } 

    public String getSelectedname() { 
     return selectedname; 
    } 

    public void setSelectedname(String selectedname) { 
     this.selectedname = selectedname; 
    } 

    public void saveCustomer() { 
     try { 
      Date d = sdf.parse(sd); 
      System.out.println(d); 
      this.dob = d; 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     CustomerDao dao = new CustomerDao(); 
     dao.addCustomer(this); 
     this.msg = "Member Info Saved Successfull!"; 
     clearAll(); 
    } 
    public void updateCustomer() { 
     try { 
      Date d = sdf.parse(sd); 
      System.out.println(d); 
      this.dob = d; 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     CustomerDao dao = new CustomerDao(); 
     dao.updateCustomer(this); 
     this.msg = "Member Info Update Successfull!"; 
     clearAll(); 
    } 
    public void deleteCustomer() { 
     CustomerDao dao = new CustomerDao(); 
     dao.deleteCustomer(custId); 
     this.msg = "Member Info Delete Successfull!"; 
     clearAll(); 
    } 

    public List<Customer> getAllCustomers() { 
     List<Customer> users = new ArrayList<Customer>(); 
     CustomerDao dao = new CustomerDao(); 
     users = dao.getAllCustomers(); 
     return users; 
    } 

    public void fullInfo() { 
     CustomerDao dao = new CustomerDao(); 
     List<Customer> lc = dao.getCustomerById(selectedname); 
     System.out.println(lc.get(0).firstName); 
     this.custId = lc.get(0).custId; 
     this.firstName = lc.get(0).firstName; 
     this.lastName = lc.get(0).lastName; 
     this.email = lc.get(0).email; 
     this.dob = lc.get(0).dob; 
     this.sd = sdf.format(dob); 
    } 

    private void clearAll() { 
     this.firstName = ""; 
     this.lastName = ""; 
     this.sd = ""; 
     this.email = ""; 
     this.custId=0; 
    } 

    } 

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property> 
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property> 
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/derbyDB</property> 
    <property name="hibernate.connection.username">user1</property> 
    <property name="hibernate.connection.password">user1</property> 
    <property name="hibernate.hbm2ddl.auto">create</property> 

    <property name="c3p0.min_size">1</property> 
    <property name="c3p0.max_size">5</property> 
    <property name="c3p0.timeout">300</property> 
    <property name="c3p0.max_statements">50</property> 
    <property name="c3p0.idle_test_period">300</property> 

    <mapping class="com.javaknowledge.entity.Customer" resource="com/javaknowledge/entity/Customer.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="com.javaknowledge.entity.Customer" table="CUSTOMERV" schema="APP"> 
     <id name="custId" type="java.lang.Integer"> 
      <column name="cust_id" /> 
      <generator class="increment" /> 
     </id> 
     <property name="firstName" type="string"> 
      <column name="first_name" length="45" not-null="true" /> 
     </property> 
     <property name="lastName" type="string"> 
      <column name="last_name" length="45" not-null="true" /> 
     </property> 
     <property name="email" type="string"> 
      <column name="email" length="45" not-null="true" /> 
     </property> 
     <property name="dob" type="date"> 
      <column name="dob" length="10" not-null="true" /> 
     </property> 
    </class> 
</hibernate-mapping> 

Odpowiedz

57

Wreszcie znalazłem błąd! Mam nadzieję, że jest to przydatne dla kogoś. Kiedy robisz zapytanie do bazy danych (w moim przypadku to Apache Derby), nazwę bazy należy wpisać pierwszą wielką literą, drugą małymi literami.

Jest źle zapytania:

session.createQuery("select first_name from CUSTOMERV"). 

Dotyczy to zapytanie

session.createQuery("select first_name from Customerv"). 

podmiot klasy I musi być taka sama jak nazwa bazy danych, ale nie jestem pewien.

+15

To może nie być powód. Wybierz cz klienta c to jest drobna kwerenda, ponieważ klient to twoja nazwa klasy, a my powinniśmy wpisać nazwę klasy w zapytaniu, a nie nazwę tabeli. Kolejna rzecz w twoim pliku hibernate.cfg.xml dałaś tylko jeden będzie dobrze. Proszę to sprawdzić. –

+0

Dzięki za porady Shoalib Chikate! –

+0

Proszę o przewodnika na http://stackoverflow.com/questions/35657292/invocation-of-init-method-failed-nested-exception-is-java-lang-illegalargumente – Prateek

0

problem został częściowo rozwiązany. Poza tym tworzenie jdbc/resource (DB Derby) musiało utworzyć pulę połączeń JDBC dla zasobu db w konsoli administracyjnej Glassfish i sprawdzić ją podczas pingowania. Teraz cała operacja CRUD działa dobrze. Sprawdzam, obiekt Klient w bazie danych dodaje poprawnie, aktualizuje i usuwa też. Ale w GlassFish dzienniku wyjściowym mają ten sam wyjątek:

SEVERE: org.hibernate.hql.internal.ast.QuerySyntaxException: CUSTOMERV is not mapped [select concat(first_name, ' ', last_name) as name from CUSTOMERV] 
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96) 
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234) 
    ....... 

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: CUSTOMERV is not mapped 
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189) 
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109) 
2

Może to uczyni to jaśniejszym i oczywiście ma sens.

@Entity 
@Table(name = "users") 

/** 
* 
* @author Ram Srinvasan 
* Use class name in NamedQuery 
* Use table name in NamedNativeQuery 
*/ 
@NamedQueries({ @NamedQuery(name = "findUserByName", query = "from User u where u.name= :name") }) 

@NamedNativeQueries({ @NamedNativeQuery(name = "findUserByNameNativeSQL", query = "select * from users u where u.name= :name", resultClass = User.class) }) 
public class User implements Principal { 
... 
} 
1

Jest jeszcze jedna szansa na uzyskanie tego wyjątku, nawet jeśli użyliśmy nazwy klasy, np. Jeśli mamy dwie klasy o tej samej nazwie w różnych pakietach. dostaniemy ten problem.

myślę hibernacji może się niejasności i rzuca ten wyjątek, więc rozwiązaniem jest użycie pełnej nazwy kwalifikowanej (np com.test.Customerv)

Dodałem tę odpowiedź, która pomoże w scenariuszu jak wspomniałem . Ten sam scenariusz utknął mi na jakiś czas.

6

Plik hibernate.cfg.xml powinien mieć mapowanie dla tabel takich jak poniżej. Sprawdź, czy nie ma go w twoim pliku.

...... 
<hibernate-configuration> 
...... 
...... 
    <session-factory> 
...... 
<mapping class="com.test.bean.dbBean.testTableHibernate"/> 
...... 
</session-factory> 

</hibernate-configuration> 
..... 
+0

tak było w przypadku mnie, zmieniając implementację do pracy z Oracle DB zamiast Derby - z Derby, z jakiegoś powodu, deklarowanie encji w persistence.xml nie było konieczne (wydawało mi się to logiczne, ponieważ jednostki były już przypisane) (może to było z powodu starszego sterownika (ojdbc6)) –

1

w HQL kwerendy nie pisze nazwa tabeli napisać jednostki nazwę klasy w zapytaniu jak String s = „z entity_cllass nazwą”; zapytanie qry = session.createUqery (s);