Search code examples
javahibernatespring-mvcormspring-annotations

IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: ClassName is not mapped


I am getting this error in Spring MVC:

Request processing failed; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: SampleClass is not mapped sampleClass.

Below is my DaoClass function where I mapped SampleClass with SampleClass table

@Autowired
private SessionFactory sessionFactory;

@Transactional
public List<SampleClass> getData()
{
   Session session = sessionFactory.getCurrentSession();
   List <SampleClass> sampleClass = session.createQuery("from SampleClass",SampleClass.class).list();
            
   return sampleClass;
}

Even the table name is the same and the column name is the same as sampleClass property.

In the sample class I used annotations for mapping:

@Data
@Entity
public class SampleClass {
    
    @Getter
    @Setter
    @Id
    private  int id;
    @Getter
    @Setter
    private String aname;
    
}

I went through many solutions nothing resolved mine. Is there any issue related to dependency versions? One more thing @Entity is showing deprecated any other alternative?


Solution

  • One more thing @Entity is showing deprecated any other alternative?

    It looks like you use org.hibernate.annotations.Entity instead of the javax.persistence.Entity.

    So, correct your mapping in this way:

    import javax.persistence.Entity;
    
    @Entity
    public class SampleClass {
    
    }