【问题标题】:Spring Hibernate春季休眠
【发布时间】:2011-05-24 17:54:45
【问题描述】:

我收到此错误:

org.hibernate.MappingException: Unknown entity: xxx.model.Application

但是,一切看起来都已正确配置。谁能看看我有没有遗漏的东西?

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="file:/dctm/db.props"/>
</bean>

<bean id="xxxDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="${db.url}"/>
    <property name="username" value="${db.username}"/>
    <property name="password" value="${db.password}"/>
</bean>

<bean id="xxxSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="xxxDataSource"/>

    <property name="annotatedClasses">
    <list>
        <value>xxx.model.Application</value>
    </list>
        </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${db.dialect}</prop>
            <prop key="hibernate.show_sql">${db.debug_sql}</prop>
            <prop key="hibernate.c3p0.minPoolSize">1</prop>
            <prop key="hibernate.c3p0.maxPoolSize">5</prop>
            <prop key="hibernate.c3p0.timeout">${db.timeout}</prop>
            <prop key="hibernate.c3p0.max_statement">50</prop>
            <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
        </props>
    </property>
</bean>

<bean id="patiDao" class="xxx.dao.hibernate.PatiHibernateDao">
    <property name="sessionFactory" ref="xxxSessionFactory"/>
</bean>

【问题讨论】:

  • 从您的xxx.model.Applicatio发布代码
  • 我认为 jboss 将休眠 jar 包作为服务器的一部分,你能检查一下应用程序与服务器上的休眠版本是否存在冲突吗?

标签: java hibernate spring jakarta-ee


【解决方案1】:

你可能错过了@Entity

@Entity
@Table(name="COURSES")
public class Application{
  //some code
}

【讨论】:

  • 不,这一切都完成了。我刚刚注意到的一件奇怪的事情是它在 JUnit 测试中运行良好,但在部署到 JBoss 5.1 时却出现错误。
  • 这表明 JBOSS 没有正确找到您的 .hbm.xml 文件。该消息意味着它找不到 xx.model.Application 的文件。检查你的假设——你做错了。
  • 没有hbm.xml文件,都是在spring config中完成的。
  • 我怀疑你对@Entity 使用了错误的导入,它应该是@Entity
【解决方案2】:

我看到此错误的两个原因。首先,如果您错过了Application 课程中的@Entity。第二个是Application 类不在构建路径中并且没有部署到 JBoss。

【讨论】:

  • 在后面的情况下不应该是ClassNotFoundException 吗?
  • 你可能是对的.. ClassNotFoundException 可能在 org.hibernate.MappingException: Unknown entity: xxx.model.Application 之前并且提问者没有看它。
【解决方案3】:

您是使用 JPA 还是直接使用 Hibernate?如果您使用 JPA,请尝试定义 META-INF/persistence.xml,您将在其中定义带注释的实体类:

<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:/MyDS</non-jta-data-source>
    <class>xxx.model.Application</class>
    <class>xxx.model.Class2</class>
    <exclude-unlisted-classes />
</persistence-unit>

【讨论】:

    【解决方案4】:

    您可以尝试让 AnnotationSessionFactoryBean 找到 @Entity 类本身(通过类路径扫描),方法是为它的 packagesToScan 提供一个值属性,而不是使用 annotatedClasses 属性手动指定类。

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 2012-12-05
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多