The concept for one-to-one mapping. Explain the mapping

前端 未结 2 712
醉酒成梦
醉酒成梦 2020-12-11 13:57

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 t

2条回答
  •  孤街浪徒
    2020-12-11 14:56

    The problem is in the relation between your xml mapping and your Java code.

    If you specify

      
    

    there needs to be a field named "name_pm" in your Country POJO.

    You don't have such a field.

    If your Country class has a field for the corresponding PM object, that field's name should be used here.

    If neither class has a reference to the other, you need to add one.

    What I would probably do with your code is to add a field to country and use its name in the mapping.

    public class Country {
        private int c_id;
        private String name_c;
        private PM c_pm;
    
        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;
        }
    
        public PM getC_pm() {
            return c_pm;
        }
    
        public void setC_pm(PM c_PM) {
            this.c_pm = c_pm;
        }
    
    }
    

    Mapping:

    
          
              
          
          
          
    
    

    Disclaimer:

    I haven't tested any of this, and I'm not sure of exactly how the mapping should be done with a shared primary key. If anyone else feels like providing a tested version, I'll happily upvote another answer or let this one turn "community wiki".

提交回复
热议问题