一对一映射的概念。解释映射 [英] The concept for one-to-one mapping. Explain the mapping

查看:522
本文介绍了一对一映射的概念。解释映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何定义2个类之间的一对一关系?在概念上,我认为我在某个地方出了问题。我不知道是什么,但它是。



让我们假设有两个类,分别是 Country PM

 国家
int c_id
字符串name_c

PM
int c_id
String name_pm

现在对于一个国家来说,只能有一个PM,PM只能属于一个国家。如何在我的映射文件中执行此操作?



我试过这个:

 < class name =pojo.Countrytable =country> 
< id name =c_id>
< generator class =increment/>
< / id>
< property name =name_c/>
< / class>

< class name =pojo.PMtable =pm>
< id name =c_id>
< generator class =increment/>
< / id>
< property name =name_pm/>
< / class>

但是这种有效映射没有什么比在运行时产生一个异常。它说属性 name_pm 无法在国家类中找到!但它是否在 Country 类中进行搜索。它应该在 PM 类中进行搜索。



同时帮助我完成两个类之间的一对一映射。我一直在尝试这一点。



Java代码:



  public class Country {
private int c_id;
私人字符串name_c;

public int getC_id(){
return c_id;
}

public void setC_id(int c_id){
this.c_id = c_id;
}

public String getName_c(){
return name_c;
}

public void setName_c(String name_c){
this.name_c = name_c;
}

}

PM

  public class PM {
private int c_id;
私人字符串name_pm;

public int getC_id(){
return c_id;
}

public void setC_id(int c_id){
this.c_id = c_id;
}

public String getName_pm(){
return name_pm;
}

public void setName_pm(String name_pm){
this.name_pm = name_pm;
}

}


解决方案

如果您的部分问题也是...我认为我在某个地方出了问题,概念上...,我想提供一些解释。



<首先, One-to-One 表示引用位于对象(实例)之间。换言之,必须从一个对象到另一个对象的引用。不仅是一个常见的简单类型(即 String name_pm String name_c



两个对象应该互相引用:

  public class PM {
private国家/地区; //引用PM
...
}

公共类国家{
私人PM pm; //引用一个国家的实例
...
}

DB 级别上,它更复杂一些。由于引用是基于 ID 列的,并且我们向我们保证,这两个 Country-PM 将始终属于一起,我们必须重新使用 ID 值。



所以,一个对象将扮演主人的角色,例如国家和第二个将根据它分配其ID。如果我们不遵循这种模式,我们不能肯定,迟早会出现一些多对一多 - 至-many 关系。



这就是为什么映射通常看起来像这样

 < class name =pojo.Countrytable =country> 
< id name =c_id>
< generator class =increment/>
< / id>
< property name =name_c/>
< / class>

< class name =pojo.PMtable =pm>
< id name =pmidtype =java.lang.Integer>
< column name =c_id/>
< generator class =foreign>
< param name =property> country< / param>
< / generator>
< / id>
< / class>

但是一对一映射确实非常特殊。我想,PM方面的多对多映射(国家编号参考数据库)可能更适合。这是一种不同的方法,但... ...

请尝试看看这篇不错的文章:


How to define a One-to-One relation between 2 classes ? I think I am going wrong somewhere,conceptually. I don't know what ,but it is.

Let us suppose there are two classes named Country and PM :

Country
  int c_id
  String name_c

PM
 int c_id
 String name_pm

Now for one country, there can only be one PM and PM can belong only to one country. How do I do this in my mapping file ?

I was trying this :

<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>

But this effete mapping does nothing than to produce an exception during run time.It says the property name_pm cannot be found inside the Country class ! But does it search inside the Country class. It should rather search inside the PM class.

Also help me accomplish my one-to-one mapping between the 2 classes. I have been trying this for some time.

Java Code:

Country

    public class Country {
    private int c_id;
    private String name_c;

    public int getC_id() {
        return c_id;
    }

    public void setC_id(int c_id) {
        this.c_id = c_id;
    }

    public String getName_c() {
        return name_c;
    }

    public void setName_c(String name_c) {
        this.name_c = name_c;
    }

}

PM

    public class PM {
    private int c_id;
    private String name_pm;

    public int getC_id() {
        return c_id;
    }

    public void setC_id(int c_id) {
        this.c_id = c_id;
    }

    public String getName_pm() {
        return name_pm;
    }

    public void setName_pm(String name_pm) {
        this.name_pm = name_pm;
    }

}

解决方案

If part of your question is also "...I think I am going wrong somewhere,conceptually... ", I'd like to provide some explanation.

First of all, One-to-One means, that the reference is between objects (instances). Other words, there must be reference from one object to other. Not only a common simple type (i.e. String name_pm or String name_c)

Both objects should reference each other:

public class PM {
    private Country country;  // reference to an instance of the PM
    ...
}

public class Country {
    private PM pm; // reference to an instance of the Country
    ...
}

On a DB level, it is a bit more trickier. Because references are based on ID columns and we do assure us, that these two Country-PM will always belong together, we have to reuse the ID value.

So, one object will play the role of the Master, e.g. Country and the second will assign its ID based on it. If we won't follow that pattern, we cannot be sure, that there will sooner or later arise some many-to-one or many-to-many relation.

And that's why the mapping usually looks like this

<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="pm" cascade="save-update" />
</class>

<class name="pojo.PM" table="pm">
    <id name="pmid" type="java.lang.Integer">
        <column name="c_id" />
        <generator class="foreign">
           <param name="property">country</param>
        </generator>
    </id>
    <one-to-one name="country" class="pojo.Country" constrained="true" />
</class>

But one-to-one mapping is really very exceptional. I guess that mapping many-to-on on PM side (with country id reference in DB) could be more suitable. It is different approach at all, but...

Please, try to see this nice article:

这篇关于一对一映射的概念。解释映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆