【问题标题】:Errors while executing Select Statement using JPA使用 JPA 执行 Select 语句时出错
【发布时间】:2016-03-07 21:15:36
【问题描述】:

我正在尝试将 Select Query 与 JPA 一起使用,但出现了一些错误:

这是我的CountryDto.java 文件

@Entity    
@Table(name = "COUNTRY")
public class CountryDto {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)  
   @Column(name="COUNTRY_ID")
   private int Country_Id;
   @Column(name="COUNTRY")
   private String Country;  

   // Getters & Setters go here

   public CountryDto() {
    // TODO Auto-generated constructor stub
   }
   public CountryDto(int country_Id, String country) {
   //   super();
       Country_Id = country_Id;
       Country = country;
   }
}

我的 Persistence.xml 文件如下:

<persistence-unit name="texttiledb" transaction-type="RESOURCE_LOCAL">
    <class>com.textileworld.mill.Dto.CountryDto</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/texttiledb" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="root" />
        <property name="eclipselink.logging.level" value="FINE" />
        <property name="eclipselink.ddl-generation" value="create-tables" />
    </properties>
</persistence-unit>

这是我要运行的代码

    EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("texttiledb");
    EntityManager entitymanager = emfactory.createEntityManager();
    entitymanager.getTransaction( ).begin( );
    Query query = entitymanager.createQuery("SELECT c FROM COUNTRY c",CountryDto.class);
         List<CountryDto> rol= (List<CountryDto> ) query.getResultList();
         for (CountryDto con : rol){
             System.out.println(con);
         }

    entitymanager.getTransaction( ).commit( );
    entitymanager.close( );
    emfactory.close( );

但我不知道为什么会显示他的抽象模式类型“国家”未知的错误。

这是我得到的错误:

Exception Description: Problem compiling [SELECT c FROM COUNTRY c]
[14, 21] The abstract schema type 'COUNTRY' is unknown.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1625)
    at com.textileworld.mill.Dao.CountryDao.main(CountryDao.java:24)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [SELECT c FROM COUNTRY c]. 
[14, 21] The abstract schema type 'COUNTRY' is unknown.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:347)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)

【问题讨论】:

    标签: java eclipse hibernate jpa persistence


    【解决方案1】:

    如下更改查询。

    Query query = entitymanager.createQuery("SELECT c FROM CountryDao c ",CountryDto.class);
    

    记住以下内容。

    @Table 是可选的。 @Entity 用于将 POJO 类注释为实体

    如果实体按如下方式创建,

     @Entity(name="MyEntityName")
     @Table(name="MyEntityTableName")
     class MyEntity {
    

    然后创建一个名称为 MyEntityTableName 的表,实体名称为 MyEntityName

    您的 JPQL 查询将是:

    select * from MyEntityName
    

    或者,如果实体按如下方式创建,

    @Entity
     class MyEntity {
    

    将创建一个名为 MyEntity 的表,实体名称为 MyEntity

    您的 JPQL 查询将是:

    select * from MyEntity
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      尝试只使用这个查询:

      "SELECT c FROM CountryDto c"
      

      而不是这个:

      "SELECT c FROM COUNTRY c" 
      

      @Table(name = "COUNTRY") 注解用于 DB。

      【讨论】:

        【解决方案3】:

        试试这个...首先你应该使用 NamedQuery 以获得更好的性能。

            EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("texttiledb");
            EntityManager entitymanager = emfactory.createEntityManager();
            entitymanager.getTransaction( ).begin( );
            Query query = entitymanager.createNamedQuery("selectAllCountry");
                 List<CountryDto> rol= (List<CountryDto> ) query.getResultList();
                 for (CountryDto con : rol){
                     System.out.println(con);
                 }
        
            entitymanager.getTransaction( ).commit( );
              entitymanager.close( );
              emfactory.close( );
        

        在 CountryDto 中像这样定义您的命名查询

        @Entity
        @NamedQueries({
                @NamedQuery(name = "selectAllCountry", query = "SELECT C FROM CountryDto C")
        })
        @Table(name = "COUNTRY")
        public class CountryDto {
        
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)     
        @Column(name="COUNTRY_ID")
        private int Country_Id;
        @Column(name="COUNTRY")
        private String Country;
        
        public String getCountry() {
            return Country;
        }
        public int getCountry_Id() {
            return Country_Id;
        }
        public void setCountry(String country) {
            Country = country;
        }
        public void setCountry_Id(int country_Id) {
            Country_Id = country_Id;
        }
        public CountryDto() {
            // TODO Auto-generated constructor stub
        }
        public CountryDto(int country_Id, String country) {
        //  super();
            Country_Id = country_Id;
            Country = country;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-20
          • 1970-01-01
          • 2020-04-16
          • 2016-03-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多