数据库mysql:

1.一对一

person50表password50表是一对一的关系; password50表中有外键 person_id

person实体类:

package com.c50.entity;

import java.util.Date;

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

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="person50")
public class Person {
    @Id
    @GenericGenerator(name="inc50",strategy="increment")
    @GeneratedValue(generator="inc50")
    private Integer id;
    private String name;
    private Integer age;
    private Date birth;
    //关系属性
    //mappedBy="对方中映射着外键的关系属性"=="对方的关系属性"
    @OneToOne(mappedBy="person50")
    //ALL:增删改都可以级联(cascade)
    //save-update:增加和更新可以级联
    //delete:删除可以级联
    //如果不设置,则增删改都不可以级联
    //注意:查询例外,不受级联权限的控制,默认级联。
    @Cascade(value=CascadeType.ALL)
    private Passport passport50;
    public Passport getPassport50() {
        return passport50;
    }
    public void setPassport50(Passport passport50) {
        this.passport50 = passport50;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", age=" + age
                + ", birth=" + birth + "]";
    }
    public Person(){}
    public Person(Integer id, String name, Integer age, Date birth) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.birth = birth;
    }

}
password实体类:

package com.c50.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="passport50")
public class Passport {
    @Id
    @GenericGenerator(name="inc50",strategy="increment")
    @GeneratedValue(generator="inc50")
    private Integer id;
    private String note;
    private Integer expire;
    //关系属性
    @OneToOne
    @JoinColumn(name="person_id")
    //@Cascade
    private Person person50;

    public Person getPerson50() {
        return person50;
    }
    public void setPerson50(Person person50) {
        this.person50 = person50;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }
    public Integer getExpire() {
        return expire;
    }
    public void setExpire(Integer expire) {
        this.expire = expire;
    }
    @Override
    public String toString() {
        return "Passport [id=" + id + ", note=" + note + ", expire=" + expire
                + "]";
    }
    public Passport(){}
    public Passport(Integer id, String note, Integer expire) {
        super();
        this.id = id;
        this.note = note;
        this.expire = expire;
    }

}

2.一对多

user50表和order50表是一对多的关系   ;  order50表有外键: user_id

1)双向关系:user可以找到order,order也可以找到user

user类:

package com.c50.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;

//代表此类参与ORM映射,此注解必须要有
@Entity
//代表user这个类映射了一个表user50,如果表名和类名一样,此注解可以省略
@Table(name="user50")
//读写缓存权限
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class User {
    //主键,此注解必须要有
    @Id
    //数据库表中主键列名为id,如果实体类属性名和列名相同,此注解可以省略
    @Column(name="id")
    //主键的维护策略
    @GenericGenerator(name="inc50",strategy="increment")
    @GeneratedValue(generator="inc50")
    private Integer userID;
    private String name;
    private Integer age;
    @Column(name="birth")
    //此属性是日期类型,time:只保存时间       date:只保存日期         timestamp:(默认)日期+时间
    @Temporal(TemporalType.DATE)
    private Date birthday;
    //关系属性
    //mappedBy="对方中映射着外键的关系属性"=="order类中关系属性为user"
    //延迟加载:fetch = FetchType.LAZY
    @OneToMany(mappedBy="user",fetch=FetchType.LAZY)
    //ALL:增删改都可以级联(cascade)
    //save-update:增加和更新可以级联
    //delete:删除可以级联
    //如果不设置,则增删改都不可以级联
    //注意:查询例外,不受级联权限的控制,默认级联。
    @Cascade(value=CascadeType.DELETE)
    private Set<Order> orders=new HashSet<Order>();

    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }

    public User(){}

    public User(Integer userID, String name, Integer age, Date birthday) {
        super();
        this.userID = userID;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public Integer getUserID() {
        return userID;
    }
    public void setUserID(Integer userID) {
        this.userID = userID;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User [userID=" + userID + ", name=" + name + ", age=" + age
                + ", birthday=" + birthday + "]";
    }

}

order类:

package com.c50.entity;

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;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="order50")
public class Order {
    @Id
    @GenericGenerator(name="inc50",strategy="increment")
    @GeneratedValue(generator="inc50")
    private Integer id;
    private Double price;
    private String note;
    //关系属性
    @ManyToOne
    //数据库中order表,外键名为user_id,指向user表
    @JoinColumn(name="user_id")
    private User user;

    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Double getPrice() {
        return price;
    }
    public void setPrice(Double price) {
        this.price = price;
    }
    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }
    @Override
    public String toString() {
        return "Order [id=" + id + ", price=" + price + ", note=" + note + "]";
    }
    public Order(){}
    public Order(Integer id, Double price, String note) {
        super();
        this.id = id;
        this.price = price;
        this.note = note;
    }

}

2)单向关系:由user维护外键,user可以级联order,order不可以级联user

user实体:

package com.c50.entity.single;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="user50")
public class User {
    @Id
    @Column(name="id")
    @GenericGenerator(name="inc50",strategy="increment")
    @GeneratedValue(generator="inc50")
    private Integer userID;
    private String name ;
    private Integer age;
    @Column(name="birth")
    @Temporal(TemporalType.DATE)
    private Date birthday;

    //@OneToMany(mappedBy="user")

    //单向
    @OneToMany
    @JoinColumn(name="user_id")
    @Cascade(value=CascadeType.SAVE_UPDATE)
    private Set<Order> orders=new HashSet<Order>();
    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }

    public User(){}

    public User(Integer userID, String name, Integer age, Date birthday) {
        super();
        this.userID = userID;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public Integer getUserID() {
        return userID;
    }
    public void setUserID(Integer userID) {
        this.userID = userID;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User [userID=" + userID + ", name=" + name + ", age=" + age
                + ", birthday=" + birthday + "]";
    }

}
package com.c50.entity.single;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="user50")
public class User {
    @Id
    @Column(name="id")
    @GenericGenerator(name="inc50",strategy="increment")
    @GeneratedValue(generator="inc50")
    private Integer userID;
    private String name ;
    private Integer age;
    @Column(name="birth")
    @Temporal(TemporalType.DATE)
    private Date birthday;

    //@OneToMany(mappedBy="user")

//单向
    @OneToMany
    @JoinColumn(name="user_id")
    @Cascade(value=CascadeType.SAVE_UPDATE)
    private Set<Order> orders=new HashSet<Order>();
    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }

    public User(){}

    public User(Integer userID, String name, Integer age, Date birthday) {
        super();
        this.userID = userID;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public Integer getUserID() {
        return userID;
    }
    public void setUserID(Integer userID) {
        this.userID = userID;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User [userID=" + userID + ", name=" + name + ", age=" + age
                + ", birthday=" + birthday + "]";
    }

}

order实体:无user类的信息

package com.c50.entity.single;

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;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="order50")
public class Order {
    @Id
    @GenericGenerator(name="inc50",strategy="increment")
    @GeneratedValue(generator="inc50")
    private Integer id;
    private Double price;
    private String note;
    //关系属性
    /*@ManyToOne
    @JoinColumn(name="user_id")
    private User user;

    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }*/
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Double getPrice() {
        return price;
    }
    public void setPrice(Double price) {
        this.price = price;
    }
    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }
    @Override
    public String toString() {
        return "Order [id=" + id + ", price=" + price + ", note=" + note + "]";
    }
    public Order(){}
    public Order(Integer id, Double price, String note) {
        super();
        this.id = id;
        this.price = price;
        this.note = note;
    }

}

3.多对多

course50表和student50表是多对多的关系; r_c_s表关系,有外键:cour_id和stu_id

course实体:

package com.c50.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="course50")
public class Course {
    @Id
    @GenericGenerator(name="inc50",strategy="increment")
    @GeneratedValue(generator="inc50")
    private Integer id;
    private String name;
    private Integer duration;
    //关系属性
    @ManyToMany
    @JoinTable(name="r_c_s"
               ,joinColumns={@JoinColumn(name="cour_id")}
               ,inverseJoinColumns={@JoinColumn(name="stu_id")})
    @Cascade(value=CascadeType.ALL)
    private Set<Student> students=new HashSet<Student>();

    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getDuration() {
        return duration;
    }
    public void setDuration(Integer duration) {
        this.duration = duration;
    }
    @Override
    public String toString() {
        return "Course [id=" + id + ", name=" + name + ", duration=" + duration
                + "]";
    }
    public Course(){}
    public Course(Integer id, String name, Integer duration) {
        super();
        this.id = id;
        this.name = name;
        this.duration = duration;
    }

}

student实体:

package com.c50.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="student50")
public class Student {
    @Id
    @GenericGenerator(name="inc50",strategy="increment")
    @GeneratedValue(generator="inc50")
    private Integer id;
    private String name;
    private Integer age;
    //关系属性:实例化,接口,泛型
    @ManyToMany(fetch=FetchType.EAGER)
    //中间表,两个外键: joinColumns=指向自己的外键   inverseJoinColumns=指向对方的外键
    @JoinTable(name="r_c_s"
               ,joinColumns={@JoinColumn(name="stu_id")}
               ,inverseJoinColumns={@JoinColumn(name="cour_id")})
    @Cascade(value=CascadeType.ALL)
    private Set<Course> courses=new HashSet<Course>();

    public Set<Course> getCourses() {
        return courses;
    }
    public void setCourses(Set<Course> courses) {
        this.courses = courses;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    public Student(){}
    public Student(Integer id, String name, Integer age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

}

Hibernate用注解实现实体类和表的映射的更多相关文章

  1. Eclipse中通过Hibernate Tools插件实现从数据库逆向生成Hibernate带注解的实体类

    一.安装hibernate tools插件 1.在线安装 通过Eclipse的Help->Install New Software 在线安装插件,插件连接为: eclipse helios(3. ...

  2. hibernate用注解配置实体类的映射

    一.注解类 1. @Table 声明了该实体bean映射指定的表(table),目录(catalog)和schema名字 2. @Id 声明了该实体bean的标识属性(对应表中的主键). 3. @Co ...

  3. Myeclipse 10使用hibernate生成注解(annotation)实体类

    以MySQL数据库为例,请在数据库里面建好对应的表. 1.配置数据库链接 打开Myelipse Database Explorer视图 Window-->Open Perspective--&g ...

  4. Eclipse从数据库逆向生成Hibernate带注解的实体类

    http://www.2cto.com/database/201501/372023.html

  5. 通过数据库中的表,使用 MyEclipse2017的反向生成工具-->hibernate反转引擎引擎(MyEclipse2017自带的插件) 来反转生成实体类和对应的映射文件

    通过数据库中的表,使用 MyEclipse2017的反向生成工具-->hibernate反转引擎引擎(MyEclipse2017自带的插件) 来反转生成实体类和对应的映射文件   文章目录 Ja ...

  6. 批量产生ssh2项目中hibernate带注解的pojo类的快捷方法

    近几个月一直在忙于项目组的ios应用项目的开发,没有太多时间去研究web应用方面的问题了.刚好,昨天有网友问到如何批量产生hibernate带注解的pojo类的快捷方法,所谓批量就是指将当前数据库中所 ...

  7. EF:根据实体类生成表结构SQL

    根据实体类生成表结构SQL: PM> Enable-Migrations -ProjectName Domain -StartUpProjectName Handler -Force PM> ...

  8. mybatis逆向工程自动生成实体类、接口以及映射Mapper.xml配置文件

    Mybatis的逆向工程非常简单,只要一个配置文件和一个Main方法就可以实现,下面以maven工程为例: (1)在pom.xml中引入依赖包 <dependency> <group ...

  9. hibernate通过注解实现实体和表的映射

    参考: 表名的映射: //代表此类参与ORM映射,此注解必须要有 @Entity //代表user这个类映射了一个表user50,如果表名和类名一样,此注解可以省略 @Table(name=" ...

随机推荐

  1. 调试2440 RAM拷贝至SDRAM遇到的问题

    汇编代码主要是初始化一些寄存器,关狗,初始化时钟,初始化存储管理器以便访问内存,然后将SoC上4k RAM数据拷贝至SDRAM,然后在SRAM里面运行,由于代码未正常跑起来,于是使用JLinkExe来 ...

  2. [To be translated] Nova:libvirt image 的生命周期

    翻译自:http://www.pixelbeat.org/docs/openstack_libvirt_images/ The main stages of a Virtual Machine dis ...

  3. [转]How to add new table in NopCommerce

    本文转自:http://www.tech-coder.com/2015/07/how-to-add-new-table-in-nopcommerce.html Hey guys I am back a ...

  4. MMORPG大型游戏设计与开发(客户端架构 part4 of vegine)

    昨天是七夕,祝大家都过的快乐,希望这句迟到的问候不会造成大家心中的困扰.这一节讲到了前端比较重要的模块,性能以及调试异常模块.一个应用的性能往往是最核心的部分,就像人身体的各个器官一样,一小部分也不能 ...

  5. AC日记——ISBN号码 openjudge 1.7 29

    29:ISBN号码 总时间限制:  1000ms 内存限制:  65536kB 描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x ...

  6. 社交化分享SDK for Unity

    前言 社交化分享,即分享到社交网络. 本文主要记录的是在Unity集成社交化分享SDK,现主流的分享SDK有如下: 1.友盟社交化分享 for unity 2.ShareSDK分享 for unity ...

  7. 2016年秋季的我,work with hololens

  8. [转]mysql 乱码问题解决终结

    http://www.th7.cn/db/mysql/2011-07-07/9217.shtml 查看 MYSQL的字符设置,在SQL查询界面输入 SHOW VARIABLES LIKE 'chara ...

  9. Java之单例模式

    单例模式: public class Person{ public static Person per//定义一个静态变量,用来储存当前类的对象 private Person()//构造方法私有化 { ...

  10. 移动开发webapp开发常用meta设置手机浏览器全屏模式

    1.WebApp全屏模式: <meta name="viewport" content="width=device-width,initial-scale=1.0, ...