2013-04-25 74 views
0

Iam是JPA的新成员,iam试图创建EJB 3.0 + JPA(休眠)应用程序。当iam将数据保存到数据库中时,会得到异常。从数据库检索数据时获取异常org.hibernate.hql.ast.QuerySyntaxException:

一个sessionBean方法:

@PersistenceContext 
    private EntityManager em; 
    public void showCustDetails(){ 
     //System.out.println(em.getProperties()); 
     //EntityManager em = emf.createEntityManager(); 
     List customer = em.createQuery("select c from 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类(实体类)

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> 
     <!-- the JNDI data source--> 
     <jta-data-source>java/customer</jta-data-source> 
     <properties> 
      <!-- if this is true, hibernate will print (to stdout) the SQL it executes, 
       so you can check it to ensure it's not doing anything 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 most database servers have slightly different versions of the 
       SQL, Hibernate needs you to choose a dialect so it knows the subtleties of 
       talking to that server --> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" /> 
      <property name="hibernate.archive.autodetection" value="class"/> 
      <!-- this tell Hibernate to update the DDL when it starts, very useful 
       for development, dangerous in production --> 
      <property name="hibernate.hbm2ddl.auto" value="create" /> 
     </properties> 
    </persistence-unit> 
</persistence> 


异常

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: CUSTOMER is not mapped [select c from CUSTOMER c] 
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1376) 
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317) 
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:280) 
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:436) 
    at retail.ejb.service.CustomerSessionBeanImpl.showCustDetails(CustomerSessionBeanImpl.java:47) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) 
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388) 
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370) 
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360) 
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348) 
    at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:206) 
    ... 51 more 


请任何人提出这一点。

编辑输出将

INFO: Name ::::sdsdfsd::customer id ::0::email::sdsdfdsdssfd::address::sdfsdsdsdsdf:::ph number::0 
INFO: Hibernate: 
    insert 
    into 
     CUSTOMER 
     (customer_id, addres, email_add, Name, ph_number) 
    values 
     (default, ?, ?, ?, ?) 
INFO: Hibernate: 

values 
    identity_val_local() 

INFO: Hibernate: 
    select 
     customer0_.customer_id as customer1_0_, 
     customer0_.addres as addres0_, 
     customer0_.email_add as email3_0_, 
     customer0_.Name as Name0_, 
     customer0_.ph_number as ph5_0_ 
    from 
     CUSTOMER customer0_ 
INFO: List Size:::1 
INFO: Name ::::sdsdfsd::customer id ::1::email::sdsdfdsdssfd::address::sdfsdsdsdsdf:::ph number::0 


enter image description here

+0

你是否从昨天算出你的问题?有什么问题? – 2013-04-25 09:03:07

+0

@KevinBowersox是的。但是你的回答对我没有帮助,但是我修好了。 – developer 2013-04-25 09:04:45

+0

这是什么问题? – 2013-04-25 09:07:32

回答

1

实体的名称必须准确地拼写类名定义,独立于表映射的,在你的情况下它会是Customer

确切的说,在String对应myEntity.getClass().getSimpleName();

编辑的输出:JPQL = SQL

更多的,往往不是你不会得到相同的结果(如果有的话)使用相同的JPQL查询!与一个SQL编辑器,反之亦然。当SQL使用表标识符时,JPQL使用与类相对应的实体。

+0

非常感谢。 iam能够现在检索数据,但是当我在eclipse sql编辑器上运行相同的查询时,iam变为零行。但在代码中,我能够获得数据。令人惊讶的是我。 – developer 2013-04-25 09:19:32

+0

我也尝试在derby命令工具上运行查询。还有我有零行:( – developer 2013-04-25 09:22:41

+0

@developer欢迎您。行为是预期的,请参阅编辑。 – kostja 2013-04-25 09:27:47