Hibernate-Annotation(下)

简介: Hibernate-Annotation(下)

三,关系映射级别注解



image.png

1. 一对一单向外键关联(两张表都要在hibernate住配置文件中进行配置)


image.png


//主表
package Entity;
import javax.persistence.*;
import java.sql.Timestamp;
/**
 * Created by futao on 2017/9/27.
 */
@Entity
@Table(name = "imooc_student")
public class Student {
    /**
     * 主键,学号
     */
    @Id
    @GeneratedValue
    private int sid;
    /**
     * 性别
     */
    private String gender;
    /**
     * 生日
     */
    private Timestamp birthday;
    /**
     * 专业
     */
    private String major;
    /**
     * 身份证对象
     */
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "pid",unique = true)
    private IdCard idCard;
    public IdCard getIdCard() {
        return idCard;
    }
    public void setIdCard(IdCard idCard) {
        this.idCard = idCard;
    }
    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public Timestamp getBirthday() {
        return birthday;
    }
    public void setBirthday(Timestamp birthday) {
        this.birthday = birthday;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
}

//关联表
package Entity;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
/**
 * Created by futao on 2017/9/27.
 */
@Entity
@Table(name = "imoooc_id_card")
public class IdCard {
    @Id
    @GeneratedValue(generator = "pid")
    @GenericGenerator(name="pid",strategy = "assigned")
    @Column(length = 18)
    private String pid;
    private String sname;
    private String address;
    public String getPid() {
        return pid;
    }
    public void setPid(String pid) {
        this.pid = pid;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

//Test
    @Test
    fun dbtest() {
        session.save(IdCard().apply {
            pid = "11"
            sname = "熊anger"
            address = "上海小二房"
        })
        session.save(Student().apply {
            gender = "男"
            birthday = Timestamp(DateTime.now().millis)
            major = "计算机"
            idCard = session.get(Student::class.java, 2) as IdCard
        })
    }


2.  不推荐使用双向外键关联


3.  多对一单向外键关联

//多的那一方
package ManyToOne;
import javax.persistence.*;
import java.sql.Timestamp;
/**
 * Created by futao on 2017/9/28.
 */
/**
 * 学生实体类
 */
@Entity
@Table(name = "imooc_senior_student")
public class Student {
    @Id
    @GeneratedValue
    private int sid;
    private String gender;
    private Timestamp birthday;
    private String major;
    /**
     * 一对多单向外键关联关系
     * 级联关系:全级连
     * 抓取策略
     */
//@joinColumn(name="对用生成的表中的字段名",referencedColumnName="一的那一方的主键名")
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "fuckyou",referencedColumnName ="cid" )
    private Classroom classroom;
    public Classroom getClassroom() {
        return classroom;
    }
    public void setClassroom(Classroom classroom) {
        this.classroom = classroom;
    }
    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public Timestamp getBirthday() {
        return birthday;
    }
    public void setBirthday(Timestamp birthday) {
        this.birthday = birthday;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
}

//一的那一方
package ManyToOne;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
/**
 * Created by futao on 2017/9/28.
 */
/**
 * 班级实体类
 */
@Entity
@Table(name = "imooc_senior_classroom")
public class Classroom {
    @Id
    @GeneratedValue(generator = "cid")
    @GenericGenerator(name = "cid",strategy = "assigned")
    @Column(length = 4)
    private String cid;
    private String cname;
    public String getCid() {
        return cid;
    }
    public void setCid(String cid) {
        this.cid = cid;
    }
    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
}


4.  多对多单向外键


多对多会生成一张中间表存储对应关系

image.png


package Many2Many;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Set;
/**
 * Created by futao on 2017/9/28.
 */
@Entity
@Table(name = "many2many_student")
public class Student {
    @Id
    @GeneratedValue
    private int sid;
    private String name;
    private String gender;
    private Timestamp birthday;
    private String major;
    /**
     * 学生持有教师的集合
     */
    @ManyToMany
    @JoinTable(
            //中间表的名字
            name = "student_teacher",
            joinColumns = {@JoinColumn(name = "sid")},
            inverseJoinColumns = {@JoinColumn(name = "tid")}
    )
    private Set<Teacher> teacherSet;
    public Set<Teacher> getTeacherSet() {
        return teacherSet;
    }
    public void setTeacherSet(Set<Teacher> teacherSet) {
        this.teacherSet = teacherSet;
    }
    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public Timestamp getBirthday() {
        return birthday;
    }
    public void setBirthday(Timestamp birthday) {
        this.birthday = birthday;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
}

package Many2Many;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
/**
 * Created by futao on 2017/9/28.
 */
@Entity
@Table(name = "many2many_teacher")
public class Teacher {
    @Id
    @GeneratedValue(generator = "tid")
    @GenericGenerator(name = "tid", strategy = "assigned")
    @Column(length = 4)
    private String tid;
    private String tname;
    public String getTid() {
        return tid;
    }
    public void setTid(String tid) {
        this.tid = tid;
    }
    public String getTname() {
        return tname;
    }
    public void setTname(String tname) {
        this.tname = tname;
    }
}

@Test
    fun many2many(){
        val t1=Teacher().apply {
            tid="T001"
            tname="张老师"
        }
        val t2=Teacher().apply {
            tid="t999"
            tname="吴彦祖"
        }
        val teachers=HashSet<Teacher>()
        teachers.add(t1)
        teachers.add(t2)
        val s1=Many2Many.Student().apply {
            name="熊老一"
            gender="男"
            birthday= Timestamp(DateTime.now().millis)
            major="计算机"
            teacherSet=teachers
        }
        val s2= Many2Many.Student().apply {
            name="熊老二"
            gender="男"
            birthday= Timestamp(DateTime.now().millis)
            major="软件工程"
        }
        val s3=Many2Many.Student().apply {
            name="李子"
            gender="女"
            birthday= Timestamp(DateTime.now().millis)
            major="建筑学"
            teacherSet=teachers
        }
        session.save(t1)
        session.save(t2)
        session.save(s1)
        session.save(s2)
        session.save(s3)
    }

5.  多对多双向外键关联


image.png


总结



image.png

image.png

相关文章
|
XML Java 数据格式
JavaConfig与常见Annotation(必知必会系列)
上一篇我们介绍了 Spring中Bean的生命周期,这一篇我们接着学习在Spring Boot中的一些常用注解。
169 0
JavaConfig与常见Annotation(必知必会系列)
|
SQL Java 数据库连接
hibernate 实体映射注解
hibernate 实体映射注解
130 1
|
Java Spring
Annotation-specified bean name ‘brdbService‘ for bean class
Annotation-specified bean name ‘brdbService‘ for bean class
247 0
|
Java 数据库连接 API
|
XML Java 数据库连接
Hibernate 注解方式
第一次接触HIbernate都是以XML方式,今天来看看注解的方式如何写 1. hibernate的常用注解 //标识实体类 @Entity //该实体类对应的表 默认对应的表为类名 可通过@Table(name ="xxx")来标识实体类名与表名不...
1289 0
|
Java 数据库连接 数据库
|
Java 数据库连接
Hibernate-Annotation
课程地址 使用hibernate注解的目的 为了简化ORM映射文件(*.hbm)繁琐的配置。 注解需要做的是映射文件要做的。 JPA与Hibernate Java Persistence API Java持久化接口 JPA是标准接口,HIbernate是JPA的实现,但是功能是JAP的超集。
966 0
|
Java Maven
ConflictingBeanDefinitionException: Annotation-specified bean name 'errorPageFilter'
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/77679431 Idea CE 下创建的基于 maven 的 Spring Boot 项目,在使用 命令启动时出现错误。
1999 0
|
Java Maven
ConflictingBeanDefinitionException: Annotation-specified bean name &#39;errorPageFilter&#39;
Idea CE 下创建的基于 maven 的 Spring Boot 项目,在使用 命令启动时出现错误。 完整错误信息: [ERROR] Failed to execute goal org.springframework.
2413 0