zoukankan      html  css  js  c++  java
  • hibernate联合主键映射

    联合主键用的不是很多,用的时候查到就是。

    Wife里id和name联合做主键,需要新建一个主键类,WifePK,必须实现Serializable接口,重写hashcode、equals。

    在Husband类里用Wife的id和name做外键关联。

    Wife类:

    package com.oracle.hibernate;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.IdClass;
    
    @Entity
    @IdClass(WifePK.class)//指明你外键用的是哪个class
    public class Wife {
    
        
        private int id;
        private String name;
        
        private int age;
        //指明id是一部分id
        @Id
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        //name是一部分id
        @Id
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
        
    }

    Husband类:

    package com.oracle.hibernate;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinColumns;
    import javax.persistence.OneToOne;
    
    @Entity
    public class Husband {
    
        private int id;
        private String name;
        private Wife  wife;    //wife的引用
        
        @Id    //主键id
        @GeneratedValue    //自动生成
        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;
        }
        
        @OneToOne    //一对一映射
        /**
         * 外键是两个字段,改变外键名字用joinColumns,值是joinColumn的数组
         * 指定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;
        }
    }

    WifePK联合主键类:

    package com.oracle.hibernate;
    
    import java.io.Serializable;
    
    /**
     * 联合主键类,不是实体,必须实现Serializable接口, 序列化,可以把这个对象写到硬盘上、读出来,或者在网络上传输。
     * 应该重写hashcode,equals,在内存里保证唯一性
     */
    public class WifePK implements 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;
        }
    
        @Override
        public int hashCode() {
    
            return this.name.hashCode();
        }
    
        // 重写equals,逻辑不能乱写
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof WifePK) {
                WifePK pk = (WifePK) obj;
                if (pk.id == this.getId() && pk.name.equals(this.getName()))
                    return true;
            }
            return false;
        }
    
    }

    生成的表:

    hibernate生成表的语句:

    create table Husband (
    id integer not null auto_increment,
    name varchar(255),
    wifeId integer,
    wifeName varchar(255),
    primary key (id)
    )
    07:14:29,202 DEBUG SchemaExport:377 -
    create table Wife (
    id integer not null,
    name varchar(255) not null,
    age integer not null,
    primary key (id, name)
    )
    07:14:29,474 DEBUG SchemaExport:377 -
    alter table Husband
    add index FKAEEA401B2953CE18 (wifeId, wifeName),
    add constraint FKAEEA401B2953CE18
    foreign key (wifeId, wifeName)
    references Wife (id, name)

  • 相关阅读:
    判断文件结束,feof……
    第五篇 分治思想(例子待加)
    第四篇 枚举思想
    第三篇 贪心思想
    第二篇 递归思想
    第一篇 递推思想
    爬虫系列
    整数划分问题
    html中a标签做容器的问题
    H5学习小结——div+css创建电子商务静态网页
  • 原文地址:https://www.cnblogs.com/lihaoyang/p/4916206.html
Copyright © 2011-2022 走看看