zoukankan      html  css  js  c++  java
  • hibernate(七)组件映射与多对一映射

    一、组件映射

    用注解配置组件映射:

    Husband为我们映射的类,wife是这个类的一部分(属性不能与husband中属性重名,不要写Entity注解,不要有主键)

    Husband类:(在getWife()方法上添加注解@Embedded)

    package cn.orlion.hibernate.model;
    
    import javax.persistence.Embedded;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @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;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        @Embedded
        public Wife getWife() {
            return wife;
        }
    
        public void setWife(Wife wife) {
            this.wife = wife;
        }
    }

    wife类:

    package cn.orlion.hibernate.model;
    
    public class Wife {
        
        private String wifename;
    
        public String getWifename() {
            return wifename;
        }
    
        public void setWifename(String wifename) {
            this.wifename = wifename;
        }
    
    }

    建表语句:

    create table Husband (
            id integer not null auto_increment,
            name varchar(255),
            wifename varchar(255),
            primary key (id)
        )

    二、多对一映射

    1、多对一单向映射

    注解方式配置:(一个组有多个用户,每个用户对应一个组,user是多的一方,group是一)

    Group类:

    package cn.orlion.hibernate.model;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    /*
     * 一个组有多个用户,每个用户对应一个组,user是多的一方,group是一
     */
    @Entity
    @Table(name="t_group")
    public class Group {
    
        private int id;
        
        private String name;
        @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;
        }
    }

    User类:

    package cn.orlion.hibernate.model;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="t_user")
    public class User {
    
        private int id;
        
        private String name;
        
        private Group group;
        @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;
        }
        @ManyToOne
        public Group getGroup() {
            return group;
        }
    
        public void setGroup(Group group) {
            this.group = group;
        }
    }

    hibernate生成的sql:

    create table t_group (
            id integer not null auto_increment,
            name varchar(255),
            primary key (id)
        )
    
    create table t_user (
            id integer not null auto_increment,
            name varchar(255),
            group_id integer,
            primary key (id)
        )
    
    alter table t_user 
            add constraint FK_e5f24mh6aryt9hsy99oydps6g 
            foreign key (group_id) 
            references t_group (id)
  • 相关阅读:
    ReactNative: 数据请求
    ReactNative: 使用Geolocation的API获取位置信息
    ReactNative: 使用第三方库图像选择器react-native-image-picker和react-native-image-crop-picker
    MDG_TR_DEST
    【VerySky原创】后台JOB运行-相关表
    【VerySky原创】RPR_ABAP_SOURCE_SCAN
    【VerySky原创】 ME9F
    【VerySky原创】如何查找SNRO编号范围的使用情况;
    【VerySky原创】怎样查找到CDHDR、CDPOS表中的OBJECTCLAS字段
    【由VerySky原创】由Number Range 导致凭证生成但无法保存的问题
  • 原文地址:https://www.cnblogs.com/orlion/p/5071349.html
Copyright © 2011-2022 走看看