简体   繁体   English

两个类之间无法建立“一对一”关系

[英]Unable to make 'one-to-one' relationship between the two classes

What shall I do in the following xml to make a one-to-one relationship between Country class and PM class ? 我应该在以下xml中做什么one-to-oneCountry类和PM类之间建立one-to-one关系?

  <class name="pojo.Country" table="country">
      <id name="c_id">
          <generator class="increment" />
      </id>
      <property name="name_c" />
      <one-to-one class="pojo.PM" name="name_pm" />
  </class>

  <class name="pojo.PM" table="pm">
      <id name="c_id">
          <generator class="increment" />
      </id>
      <property name="name_pm"  />
  </class>

I am making a one-to-one relation between name_pm which is a property defined inside the PM class and name_c which is a property defined inside Country class. 我正在name_pm这是PM类内部定义的属性)和name_c (是Country类中定义的属性)之间name_pm一对一的关系。 The problem occurs when I write : 我写的时候会出现问题:

<one-to-one class="pojo.PM" name="name_pm" />

It starts to search the property name_pm inside the class PM when it should search inside the Country class. 当它应该在Country类中进行搜索时,它将开始在PM类中搜索属性name_pm

How do I define a One-toOne relation between name_pm and name_c ? 如何定义name_pmname_c之间One-toOne关系?

Seems like wrong syntax. 似乎语法错误。

Try 尝试

<one-to-one name="pmNameInCountryPojo" class="pojo.PM"
            cascade="save-update"></one-to-one>

The exception itself points to the solution. 异常本身指向解决方案。

You need to create getter and setter for name_pm in your pojo.Country class. 您需要在pojo.Country类中为name_pm创建getter和setter。

and add 并添加

<one-to-one class="pojo.Country" name="${variable-name}" />

in your pojo.PM configuration and create setter and getter for respective variable in PM class. 在您的pojo.PM配置中,并为PM类中的各个变量创建setter和getter。

Look this example, it will help you. 这个例子,它将对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM