1

I am trying to use Select Query with JPA but i am getting some error:

This is my CountryDto.java file

@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;
   }
}

My Persistence.xml file is as follows:

<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>

And This is the code that i am trying to run

    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( );

But i dont know why is it showing error that he abstract schema type 'COUNTRY' is unknown.

Here is the error that i am getting :

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)

3 Answers 3

1

try this... first thing you should use NamedQuery for better performance.

    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( );

define your named query like this in your 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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try just to use this query:

"SELECT c FROM CountryDto c"

Instead of this:

"SELECT c FROM COUNTRY c" 

The @Table(name = "COUNTRY") annotation is used for DB.

Comments

0

Change the query as follows.

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

Remember the following.

@Table is optional. @Entity is needed for annotating a POJO class as an entity

If the entity is created as the following,

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

then a table with name MyEntityTableName is created and the entity name is MyEntityName.

Your JPQL query would be :

select * from MyEntityName

Or, if the entity is created as the following,

@Entity
 class MyEntity {

A table with name MyEntity will be created and the Entity name will be MyEntity.

Your JPQL query would be:

select * from MyEntity

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.