如何理解java中的映射概念,一对一映射的概念。解释映射

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 :

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

country

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源包含源代码 易看易懂 其实就是一发射机制 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问、检测和修改它本身状态或行为的一种能力。这一概念的提出很快引发了计算机科学领域关于应用反射性的研究。它首先被程序语言的设计领域所采用,并在Lisp和面向对象方面取得了成绩。其LEAD/LEAD++ 、OpenC++ 、MetaXa和OpenJava等就是基于反射机制的语言。最近,反射机制也被应用到了视窗系统、操作系统和文件系统。 反射本身并不是一个新概念,它可能会使我们联想到光学的反射概念,尽管计算机科学赋予了反射概念新的含义,但是,从现象上来说,它们确实有某些相通之处,这些有助于我们的理解。在计算机科学领域,反射是指一类应用,它们能够自描述和自控制。也就是说,这类应用通过采用某种机制来实现对自己行为的描述(self-representation)和监测(examination),并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义。可以看出,同一般的反射概念相比,计算机科学领域的反射不单单指反射本身,还包括对反射结果所采取的措施。所有采用反射机制的系统(即反射系统)都希望使系统的实现更开放。可以说,实现了反射机制的系统都具有开放性,但具有开放性的系统并不一定采用了反射机制,开放性是反射系统的必要条件。一般来说,反射系统除了满足开放性条件外还必须满足原因连接(Causally-connected)。所谓原因连接是指对反射系统自描述的改变能够立即反映到系统底层的实际状态和行为上的情况,反之亦然。开放性和原因连接是反射系统的两大基本要素。13700863760 Java,反射是一种强大的工具。它使您能够创建灵活的代码,这些代码可以在运行时装配,无需在组件之间进行源代表链接。反射允许我们在编写与执行时,使我们的程序代码能够接入装载到JVM的类的内部信息,而不是源代码选定的类协作的代码。这使反射成为构建灵活的应用的主要工具。但需注意的是:如果使用不当,反射的成本很高。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值