• Hibernate多对一外键单向关联(Annotation配置)


    Hibernate多对一外键单向关联(Annotation配置)
    package edu.xaut.hibernate;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @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;
        }

        @Column(length = 20)
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }


    package edu.xaut.hibernate;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;

    @Entity
    @Table(name="t_user")
    public class User {
        private int id;
        private String name;
        private String title;
        private Group group;

        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        @Column(length = 20)
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Column(length = 10)
        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        @ManyToOne
        @JoinColumn(name = "groupId")
        public Group getGroup() {
            return group;
        }

        public void setGroup(Group group) {
            this.group = group;
        }
    }

    生成的SQL语句如下:
    create table t_group (
            id integer not null auto_increment,
            name varchar(20),
            primary key (id)
        )

        create table t_user (
            id integer not null auto_increment,
            name varchar(20),
            title varchar(10),
            groupId integer,
            primary key (id)
        )

        alter table t_user
            add index FKCB63CCB6D883DE2F (groupId),
            add constraint FKCB63CCB6D883DE2F
            foreign key (groupId)
            references t_group (id)

     from:http://blog.sina.com.cn/s/blog_4979ec3e0101754x.html

  • 相关阅读:
    Apache Spark源码走读之8 -- Spark on Yarn
    Apache Spark源码走读之7 -- Standalone部署方式分析
    Apache Spark源码走读之6 -- 存储子系统分析
    Linux服务器--所有用户登陆操作命令审计
    Linux--top命令查看系统状态,所有值讲解
    docker --help 详解
    Linux下使用《du》命令查看某文件及目录的大小
    Linux查看CPU《型号..》《内存..》《硬盘..》《系统..》
    Linux下 cmatrix的安装和使用
    CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡
  • 原文地址:https://www.cnblogs.com/lidabo/p/2917117.html
Copyright © 2020-2023  润新知