【问题标题】:Hibernate: many to one mapping with different database tablesHibernate:与不同数据库表的多对一映射
【发布时间】:2016-07-02 02:17:59
【问题描述】:

我有 2 个课程:UserBranch。许多用户可以有一个分支。 IE。这是多对一的关联。 我的users 表位于第一个数据库中,名为fi,第二个数据库中的branch 表名为fi-master

我正面临这个错误:

Caused by: org.hibernate.MappingException: An association from the table user refers to an unmapped class: com.hcl.fi.web.model.Branch
    at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1134)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1052)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:997)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:720)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
    ... 100 more

如何映射它们?

userhbm:

  <many-to-one name="branchDetail" column="branch_id"  class="com.hcl.fi.web.model.Branch" cascade="all"  
          not-found="ignore" not-null="true"/>  

【问题讨论】:

  • 您可能忘记在 Branch 类中添加 @Entity。根据您的配置,您可能还需要在 persistence.xml 中声明 Branch
  • @ArnaudDenoyelle 他使用xml 映射。
  • 请添加有关 Hibernate 版本以及所有 xml 映射和配置代码的信息。

标签: java mysql hibernate hibernate-mapping


【解决方案1】:

请看下面

  • 路径下应该有一个名为“Branch”的实体类 'com.hcl.fi.web.model'。它应该标记为@Entity。
  • 确保“Branch.hbm.xml”文件与“User.hbm.xml”的位置相同 如果“分支”需要,请确保路径更正。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    相关资源
    最近更新 更多