开发者

Getting hibernate entity error

I am using hibernate in spring app. but due to some problem i can't use spring injection so i manually have to declare the session factory like below

SessionFactory sessionFactory = new AnnotationConfiguration()
        .configure("com/vaannila/service/hibernate.cfg.xml")
        .buildSessionFactory();

          Session session = sessionFactory.openSession();
          Registration person = (Registration) session.get(Registration.class, 1);

As i am using annotation in entity class i get the fol开发者_如何学运维lowing error

org.hibernate.MappingException: Unknown entity: com.vaannila.domain.Registration



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
    <!-- We're using MySQL database so the dialect needs to MySQL as well-->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <!-- Enable this to see the SQL statements in the logs-->
    <property name="show_sql">true</property>
    <!-- This will drop our existing database and re-create a new one.
            Existing data will be deleted! -->
    <property name="hbm2ddl.auto">update</property>
  <mapping class="com.vaannila.domain.Country" />


  </session-factory>
</hibernate-configuration>


This could be because of the wrong FQCN entry for the class Registration or, you might be having

import javax.persistence.Entity;

instead of,

import org.hibernate.annotations.Entity;


Make sure your Registration class is annotated correctly with "javax.persistence.Entity". I am just making sure, I have had same trouble when I missed that.

    import javax.persistence.Entity;


    @Entity
public class Registration{ 
}


Another reason could be that you class is not listed in hibernate.cfg.xml:

<mapping class="your.package.Registration"/>

Personally I haven't found the way to force standalone Hibernate scanning entity annotations, so I have pretty big list of mappings inside.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜