数据库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. 浅谈C++设计模式之工厂方法(Factory Method)

    为什么要用设计模式?根本原因是为了代码复用,增加可维护性. 面向对象设计坚持的原则:开闭原则(Open Closed Principle,OCP).里氏代换原则(Liskov Substitution ...

  2. Android ViewPager再探:增加滑动指示条

    上一篇:<Android ViewPager初探:让页面滑动起来> ViewPager只是左右滑动有些丑,也不知道当前位于第几页面. 可以在上方加入滑动指示条,来确定当前位置. 只需要修改 ...

  3. System.getProperty()引起的悲剧--您的主机中的软件中止了一个已建立的连接

    我已无法形容此刻我的心情.. 本来是已经写好的netty5的demo程序,server和client之间创建tcp长连接的..然后随便传点数据的简单demo..然后今天试了一下tcp粘包的例子,用到了 ...

  4. [转]Oracle 分组聚合二种写法,listagg和wmsys.wm_concat

    本文转自:http://www.cnblogs.com/ycdx2001/p/3502495.html with temp as( select 'China' nation ,'Guangzhou' ...

  5. 浅析Java中的访问权限控制

    浅析Java中的访问权限控制 今天我们来一起了解一下Java语言中的访问权限控制.在讨论访问权限控制之前,先来讨论一下为何需要访问权限控制.考虑两个场景: 场景1:工程师A编写了一个类ClassA,但 ...

  6. 学生管理系统<分层开发>

    一:分层架构 搭建DAL层(数据访问层).UI层(表示层).BLL层(业务逻辑层)以及Model层(实体层) 各层的引用关系: DAL.UI.BLL层引用Model层 UI层引用BLL层 BLL层引用 ...

  7. uva10167 Birthday Cake

    Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them. Now we put t ...

  8. Linq语法详细(转)

    原文地址:http://www.cnblogs.com/knowledgesea/p/3897665.html 开门见山 读这篇文章之前,我先说下,每一种搜索结果集,我都以三种方式变现出来,为啦更好的 ...

  9. laravel5.3引入第三方类库的方法

    laravel版本:5.3 今天做的是引入第三方的phpquery类库,方法: 在laravel的app目录下自定义一个文件夹,我用的名字是:Libs 然后直接将phpquery类库扔进这个目录 在c ...

  10. zlog学习笔记(level)

    level.h /** * */ #ifndef __zlog_level_h #define __zlog_level_h #include "stdio.h" #include ...