Spring + Hibernate annotations error "is not mapped"

annotations, hibernate, spring

Solution

Usually the problem is trivial: you should use `javax.persistence.Entity` instead of Hibernate-specific `org.hibernate.annotations.Entity`. The latter was deprecated in Hibernate in favour of JPA annotations where possible.

That's exactly what you didn't show, so hope it's a lucky shot :)

Problem

I use Spring + Hibernate with annotations and i got the following error : ``` org.hibernate.hql.ast.QuerySyntaxException: Produit is not mapped [from Produit] ``` it appens when i call this function : ``` public List<Produit> getListeProduit() { return sessionFactory.getCurrentSession().createQuery("from Produit").list(); } ``` This is my hibernate.cfg.xml ``` <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <mapping class="port.domain.Produit" /> </session-factory> </hibernate-configuration> ``` Produit class are annoted well with @Entity, @Table ID with @Id, @Column, @GeneratedValue The others columns with @Column Here is my bean SessionFactory in my XXX-servlet.xml : ``` <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> <property name="configurationClass"> <value>org.hibernate.cfg.AnnotationConfiguration</value> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> ``` EDIT : Entity Code ``` @Entity @Table(name="produit") public class Produit implements Serializable{ @Id @Column(name="produit_id") @GeneratedValue(strategy=GenerationType.IDENTITY) private int produitId; @Column(name="produit_nom") private String produitNom; public void setProduitId(int i) { produitId = i; } public int getProduitId() { return produitId; } public void setProduitNom(String s) { produitNom = s; } public String getProduitNom() { return produitNom; } } ``` I know there are many threads about this problem but i don't find any correct issues. I understand that Hibernate can't mapped my class but i don't know why ... Where could the problem come from ? Thanks a lot.

Original source

Related problems