Home > Software engineering >  Hibernate. Person is not mapped
Hibernate. Person is not mapped

Time:11-05

I just want to find all rows from the database (postgres:alpine) with Hibernate but got "not mapped" error.

Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Person is not mapped [from Person p]

I've a lot of read about this error. So people in createQuery write incorrect name. I have right one (classname). But still get this error. Any ideas please?

Entity:

import javax.persistence.*;
import javax.persistence.Entity;
import java.io.Serializable;

@Entity
@Table(name="users")
public class Person implements Serializable {
    protected int id;
    protected String name;
    protected int age;
    protected String email;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "email")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "age")
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Repository:

import ch.fdsgn.models.Person;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.List;

@Transactional
@Repository("personDao")
public class PersonDAO {

    private SessionFactory sessionFactory;

    @Transactional(readOnly=true)
    public List<Person> findAll() {
        return this.getSessionFactory().getCurrentSession().createQuery("from Person p").list();
    }
  
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Resource(name = "sessionFactory")
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}

applicationContext.xml:

<util:properties id="hibernateProperties">
    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL10Dialect</prop>
    <prop key="hibernate.max_fetch_depth">3</prop>
    <prop key="hibernate.jdbc.fetch_size">50</prop>
    <prop key="hibernate.jdbc.batch_size">10</prop>
    <prop key="hibernate.show_sql">false</prop>
</util:properties>       
     
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
    p:dataSource-ref="webDataSource"
    p:packagesToScan="ch.fdsgn.dao"
    p:hibernateProperties-ref="hibernateProperties"/>

CodePudding user response:

You don't scan the correct package.

  • Your entity is in ch.fdsgn.models
  • You scan p:packagesToScan="ch.fdsgn.dao"
  • Related