博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate one-to-one mapping with Composite key
阅读量:4992 次
发布时间:2019-06-12

本文共 2493 字,大约阅读时间需要 8 分钟。

1.Annotation

 

Husband class: @JoinColumns ({@JoinColumn(name="",referencedColumnName="") , @JoinColumn(name="",referencedColumnName="")})

 

@Entity public class Husband {        private int id;        private String name;        private Wife wife;                @Id        @GeneratedValue        public int getId() {            return id;        }        public void setId(int id) {            this.id = id;        }                @OneToOne        //name of the column; By default is (wife_id)        @JoinColumns(           {               @JoinColumn(name="wifeId",referencedColumnName="id"),                                             @JoinColumn(name="wifeName",referencedColumnName="name")             }        )        public Wife getWife() {            return wife;        }        public void setWife(Wife wife) {            this.wife = wife;        }                public String getName() {            return name;        }        public void setName(String name) {            this.name = name;        }}      @Entity@IdClass(value=WifePK.class)public class Wife {    private int id;    private String name;    private int age;    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Id    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    @Id    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}public class WifePK implements java.io.Serializable{    private int id;    private String name;        public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }        public boolean equal(Object o){        if(o instanceof WifePK){            WifePK p=(WifePK)o;            if(this.name.equals(p.getName())&&this.id==p.getId()){                return true;            }        }        return false;    }    public int hashCode(){        return this.name.hashCode();    }}

 

2.XML

Student class: under <many-to-one name="xx"> tag, we should name each column of  composite primary key of xx as <column name="xx"> 

 

 

  

 

转载于:https://www.cnblogs.com/fifi043/p/4906111.html

你可能感兴趣的文章
Datatable get请求传参应用
查看>>
杭电1170
查看>>
3unit8
查看>>
kettle与各数据库建立链接的链接字符串
查看>>
【转】Apache Solr 访问权限控制
查看>>
PostgreSQL - 转义字符
查看>>
两步搞定一台电脑同时开启多个tomcat
查看>>
jQuery EasyUI弹出确认对话框(确认操作中.....)
查看>>
CentOS7 监控网络流量
查看>>
根据控件名称反射查找控件
查看>>
编写和执行C#代码的插件:CS-Script for Notepad++
查看>>
BZOJ3569: DZY Loves Chinese II(线性基构造)
查看>>
Android系统源码下载及使用(Android 14到19源码)
查看>>
绑定dropdownlist
查看>>
[LeetCode] Sudoku Solver
查看>>
实验四
查看>>
Python Day04
查看>>
Android新增API之AudioEffect中文API与应用实例
查看>>
颜色空间RGB与HSV(HSL)的转换
查看>>
swift 用协议实现代理传值功能
查看>>