java - getting exception when retrieving data from database org.hibernate.hql.ast.QuerySyntaxException: -
iam new jpa, iam trying create ejb 3.0 + jpa (hibernate) application. when iam persisting data database getting below exception.
sessionbean method:
@persistencecontext private entitymanager em; public void showcustdetails(){ //system.out.println(em.getproperties()); //entitymanager em = emf.createentitymanager(); list customer = em.createquery("select c customer c").getresultlist(); system.out.println("list size:::"+ customer.size()); for(object c:customer){ customer customers = (customer) c; system.out.println("name ::::" + customers.getname() + "::customer id ::"+customers.getcustomer_id()+"::email::"+customers.getemail_id()+"::address::"+customers.getaddress()+ ":::ph number::"+customers.getphnumber()); } }
customer class(entity class)
package retail.model.vo; import java.io.serializable; import javax.ejb.transactionattribute; import javax.ejb.transactionmanagement; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.table; import static javax.ejb.transactionmanagementtype.bean; @entity @table(name = "customer") //@transactionattribute(value=required) public class customer implements serializable{ /** * */ private static final long serialversionuid = 5256938732963606407l; private int customer_id; private string name; private string address; private string email_id; private int phnumber; @id @generatedvalue(strategy = generationtype.identity) @column(name = "customer_id") public int getcustomer_id() { return customer_id; } public void setcustomer_id(int customer_id) { this.customer_id = customer_id; } @column(name = "name") public string getname() { return name; } public void setname(string name) { this.name = name; } @column(name = "addres") public string getaddress() { return address; } public void setaddress(string address) { this.address = address; } @column(name = "email_add") public string getemail_id() { return email_id; } public void setemail_id(string email_id) { this.email_id = email_id; } @column(name = "ph_number") public int getphnumber() { return phnumber; } public void setphnumber(int phnumber) { this.phnumber = phnumber; } public string validate(){ if(name!=null && name!=""){ system.out.println("chandan"); return "viewcustomerdetails"; } else{ return "viewcustomerdetails"; } } }
persistence.xml
<persistence-unit name="retailunit" > <provider>org.hibernate.ejb.hibernatepersistence</provider> <!-- jndi data source--> <jta-data-source>java/customer</jta-data-source> <properties> <!-- if true, hibernate print (to stdout) sql executes, can check ensure it's not doing crazy <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.clientdriver"/> <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/company;create=true" /> <property name="hibernate.connection.username" value="user" /> <property name="hibernate.connection.password" value="123" /> --> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <!-- since database servers have different versions of sql, hibernate needs choose dialect knows subtleties of talking server --> <property name="hibernate.dialect" value="org.hibernate.dialect.derbydialect" /> <property name="hibernate.archive.autodetection" value="class"/> <!-- tell hibernate update ddl when starts, useful development, dangerous in production --> <property name="hibernate.hbm2ddl.auto" value="create" /> </properties> </persistence-unit> </persistence>
exception
caused by: java.lang.illegalargumentexception: org.hibernate.hql.ast.querysyntaxexception: customer not mapped [select c customer c] @ org.hibernate.ejb.abstractentitymanagerimpl.convert(abstractentitymanagerimpl.java:1376) @ org.hibernate.ejb.abstractentitymanagerimpl.convert(abstractentitymanagerimpl.java:1317) @ org.hibernate.ejb.abstractentitymanagerimpl.createquery(abstractentitymanagerimpl.java:280) @ com.sun.enterprise.container.common.impl.entitymanagerwrapper.createquery(entitymanagerwrapper.java:436) @ retail.ejb.service.customersessionbeanimpl.showcustdetails(customersessionbeanimpl.java:47) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:601) @ org.glassfish.ejb.security.application.ejbsecuritymanager.runmethod(ejbsecuritymanager.java:1052) @ org.glassfish.ejb.security.application.ejbsecuritymanager.invoke(ejbsecuritymanager.java:1124) @ com.sun.ejb.containers.basecontainer.invokebeanmethod(basecontainer.java:5388) @ com.sun.ejb.ejbinvocation.invokebeanmethod(ejbinvocation.java:619) @ com.sun.ejb.containers.interceptors.aroundinvokechainimpl.invokenext(interceptormanager.java:800) @ com.sun.ejb.ejbinvocation.proceed(ejbinvocation.java:571) @ com.sun.ejb.containers.interceptors.systeminterceptorproxy.doaround(systeminterceptorproxy.java:162) @ com.sun.ejb.containers.interceptors.systeminterceptorproxy.aroundinvoke(systeminterceptorproxy.java:144) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:601) @ com.sun.ejb.containers.interceptors.aroundinvokeinterceptor.intercept(interceptormanager.java:861) @ com.sun.ejb.containers.interceptors.aroundinvokechainimpl.invokenext(interceptormanager.java:800) @ com.sun.ejb.containers.interceptors.interceptormanager.intercept(interceptormanager.java:370) @ com.sun.ejb.containers.basecontainer.__intercept(basecontainer.java:5360) @ com.sun.ejb.containers.basecontainer.intercept(basecontainer.java:5348) @ com.sun.ejb.containers.ejbobjectinvocationhandler.invoke(ejbobjectinvocationhandler.java:206) ... 51 more
please suggest on this.
edit out put
info: name ::::sdsdfsd::customer id ::0::email::sdsdfdsdssfd::address::sdfsdsdsdsdf:::ph number::0 info: hibernate: insert customer (customer_id, addres, email_add, name, ph_number) values (default, ?, ?, ?, ?) info: hibernate: values identity_val_local() info: hibernate: select customer0_.customer_id customer1_0_, customer0_.addres addres0_, customer0_.email_add email3_0_, customer0_.name name0_, customer0_.ph_number ph5_0_ customer customer0_ info: list size:::1 info: name ::::sdsdfsd::customer id ::1::email::sdsdfdsdssfd::address::sdfsdsdsdsdf:::ph number::0
the name of entity must spelled defined class name, independent of table mapping, in case customer
.
to exact, string
corresponds output of myentity.getclass().getsimplename();
edit: jpql != sql
more not not same results (if any) using same jpql query sql editor , vice versa. while sql uses table identifiers, jpql uses entities, corresponding classes.