Hibernate:使用不同的数据库表进行多对一映射

时间:2022-09-21 22:52:42

I have 2 classes: User and Branch. many user can have one branch. I.e. this is association many-to-one. My users table is in first database called fi and the branch table in the second database called fi-master.

我有2个类:User和Branch。许多用户可以拥有一个分支。即这是多对一的关联。我的用户表位于名为fi的第一个数据库中,第二个数据库中的分支表称为fi-master。

I am facing this error:

我正面临这个错误:

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

How can I map them?

我该如何映射它们?

in user hbm :

在用户hbm中:

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

1 个解决方案

#1


0  

Can you please look at below

你能看一下下面吗?

  • You should have a entity class called 'Branch' under the path 'com.hcl.fi.web.model'. It should be decalred as @Entity.
  • 您应该在路径“com.hcl.fi.web.model”下有一个名为“Branch”的实体类。它应该被称为@Entity。
  • Make sure that 'Branch.hbm.xml' file is same location as 'User.hbm.xml' Please make sure that path correction if needed for 'Branch'.
  • 确保'Branch.hbm.xml'文件与'User.hbm.xml'的位置相同。如果'Branch'需要,请确保路径更正。

#1


0  

Can you please look at below

你能看一下下面吗?

  • You should have a entity class called 'Branch' under the path 'com.hcl.fi.web.model'. It should be decalred as @Entity.
  • 您应该在路径“com.hcl.fi.web.model”下有一个名为“Branch”的实体类。它应该被称为@Entity。
  • Make sure that 'Branch.hbm.xml' file is same location as 'User.hbm.xml' Please make sure that path correction if needed for 'Branch'.
  • 确保'Branch.hbm.xml'文件与'User.hbm.xml'的位置相同。如果'Branch'需要,请确保路径更正。