【问题标题】:Problem with mapping tag in hibernate configuration filehibernate配置文件中映射标签的问题
【发布时间】:2009-11-03 18:52:47
【问题描述】:

我正在使用休眠注释,当我遵循一切正常时

sessionFactory = new AnnotationConfiguration()
                                .addPackage("istreamcloudframework.objectmodel.member")
                                .addAnnotatedClass(User.class)
     .buildSessionFactory();

但我想避免以这种方式指定所有类,所以我尝试以下列方式将其放入休眠配置文件中,


    mapping package="istreamcloudframework.objectmodel.member"
    mapping class="istreamcloudframework.objectmodel.member.User"

我收到以下错误,


org.hibernate.MappingException: Unknown entity: istreamcloudframework.objectmodel.member.User

这里怎么了?

P.S:我已经检查了所有注释导入,而不是 org.hibernate.annotations.Entity。我正在使用 javax.persistence。 导入; *

【问题讨论】:

    标签: hibernate orm


    【解决方案1】:

    您必须使用AnnotationConfiguration 实例来处理包含您的配置的 XML 文件,但您应该能够在那里指定您的类。详情请见Hibernate Annotations documentation

    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
      <session-factory>
        <mapping package="istreamcloudframework.objectmodel.member" />
        <mapping class="istreamcloudframework.objectmodel.member.User" />
      </session-factory>
    </hibernate-configuration>
    

    创建会话工厂:

    SessionFactory sessionFactory = new AnnotationConfiguration()
     .configure().buildSessionFactory();
    

    另外请注意,没有必要映射包(这样做不会映射所述包中的类),除非您有包级注释。

    【讨论】:

    • hmmm....netbeans 的干净和构建使它工作。我很好奇为什么 netbeans 会这样,还是谢谢...