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;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…