Hibernate注解编程实现
hibernate注解编程
注解:用来代替xml配置 一:hibernate有两种xml配置文件: 1、Hibernate.cfg.xml核心配置文件 2、Car.hbm.xml ORM配置文件 注解代替的是ORM配置文件 二:常用注解 1、orm注解 在实体类上使用@Entity注解,表示当前类 与 同名表有对应关系 @Entity public class Car implements Serializable {} 扩展:使用@Table注解,指定实体类对应的表名 @Entity @Table(name = “t_Car”) public class Car implements Serializable {}
注意:随着实体与表对应,默认实体中所有的属性,都有与之数量相同,名称相同的表字段 在主键属性或get方法上使用@Id注解,表示当前属性是主键属性,启动框架时,作为单记录sql语句的条件字段,建议写在get方法上,如: @Id private Integer cno; 或 @Id public Integer getCno() { return cno; } 扩展: 在属性上或get方法上,使用@Column注解,设置属性与字段具体的映射信息 @Column(name = “carname”,length=8) public String getCname() { return cname; }
扩展:在属性上使用@Transient注解,表示当前属性不与表字段对应
注意:orm实体设置完成后,需要将其关联到核心配置文件中
<mapping class="com.hibernate.domain.Car"></mapping>
package com.hibernate.domain;
import jakarta.persistence.*;
import jakarta.transaction.Transactional;
import java.io.Serializable;
@Entity
@Table(name = "t_Car")
public class Car implements Serializable {
@Id
private Integer cno;
private String cname;
private String color;
private Integer price;
public Car() {
}
public Car(Integer cno, String cname, String color, Integer price) {
this.cno = cno;
this.cname = cname;
this.color = color;
this.price = price;
}
@Id
public Integer getCno() {
return cno;
}
public void setCno(Integer cno) {
this.cno = cno;
}
@Column(name = "carname",length = 8)
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
@Transient
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}
2、主键生成机制 情景一: @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer cno;
import java.io.Serializable;
@Entity
@Table(name = "t_Car")
public class Car implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer cno;
private String cname;
private String color;
private Integer price;
说明: @GeneratedValue(strategy = GenerationType.TDENTITY); mysql 表自增 @GeneratedValue(strategy = GenerationType.SEQUENCE) ; oracle序列 @GeneratedValue(strategy = GenerationType.AUTO):根据数据库自动选择(以上两种) 情景二:Oracle序列 @Id @SequenceGenerator(name = “seq”,sequenceName = “seq1”,initialValue = 1,allocationSize = 1) @GeneratedValue(generator = “seq”) private Integer cno;
import java.io.Serializable;
@Entity
@Table(name = "t_Car")
public class Car implements Serializable {
@Id
@SequenceGenerator(name = "seq",sequenceName = "seq1",initialValue = 1,allocationSize = 1)
@GeneratedValue(generator = "seq")*
private Integer cno;
private String cname;
private String color;
private Integer price;
说明: sequenceName = “seq1” :数据序列的名称 initialValue = 1 :序列初始值 allocationSize = 1 :序列递增值 情景三: @GenericGenerator(name=“aaa”,strategy = “uuid”) @GeneratedValue(generator = “aaa”) private Integer cno;
import java.io.Serializable;
@Entity
@Table(name = "t_Car")
public class Car implements Serializable {
@Id
@GenericGenerator(name="aaa",strategy = "uuid")
@GeneratedValue(generator = "aaa")
private Integer cno;
private String cname;
private String color;
private Integer price;
3、关联关系 一对一(人和身份证) @OneToOne :表示是一个一对一关联属性 @PrimaryKeyJoinColumn:表示用主键的key进行关联 扩展:@OneToOne(cascade = CascadeType.ALL):设置级联 人的实体类
@Entity
@Table(name = "t_person")
public class Person implements Serializable {
@Id
private Integer pid;
private String pname;
private Integer age;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private Card card;
身份证实体类
@Entity
@Table(name = "t_card")
public class Card {
@Id
private Integer cid;
private String cno;
private String address;
@OneToOne
@PrimaryKeyJoinColumn
private Person person;
}
注意:要在核心配置文件中关联
<!--使用注解时用-->
<mapping class="com.hibernate.domain.Person"></mapping>
<mapping class="com.hibernate.domain.Card"></mapping>
一对多(部门+员工) @OneToMany:表示当前属性是一个 一对多关联属性 @OneToMany( cascade = CascadeType.ALL, //设置级联 mappedBy = “dept” //设置主控(主控在many端) ,fetch = FetchType.EAGER //设置懒加载及抓取策略 )
注意:mappedBy 用来指定主控在many端,关联many端Emp时,关联条件参考emp端dept属性上的 @JoinColumn(name = “dno”) ,就是dept如何关联emp,要看emp是如何关联dept的,所以many端是主控,所以One端不能再使用@JoinColumn注解了
注意:fetch有2个值:1:FetchType.EAGER 关闭懒加载 和 迫切抓取,关联语句查询 2:FetchType.LAZY 懒加载和N+1查询
@JoinColumn(name = “dno”) 表示关联的外键字段
部门实体
@Entity
@Table(name = "t_dept")
public class Dept {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer dno;
private String dname;
@OneToMany(cascade = CascadeType.ALL,mappedBy = "dept")
private Set<Emp> emps;
}
@ManyToOne 表示是一个多对一 关联属性 @JoinColumn(name = “dno”) 表示关联的外键
员工实体
@Entity
@Table(name = "t_emp")
public class Emp {
@Id
@GenericGenerator(name = "aaa",strategy = "uuid")
@GeneratedValue(generator = "aaa")
private String eno;
private String ename;
private Integer sal;
@ManyToOne
@JoinColumn(name = "dno")
private Dept dept;
}
多对多(老师+学生) @ManyToMany:表示是一个多对多关联属性 @JoinTable( name = “t_teacher_student”, //表示关联的中间表 JoinColumns = {@JoinColumn(name = “tno”)}, //Teacher.id与中间表的tno关联 inverseJoinColumns = {@JoinColumn(name = “sno”)} //Student.id与中间表sno关联 )
老师表
@Entity
@Table(name = "t_techaer")
public class Teacher implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer tno;
private String tname;
private Integer sal;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "t_teacher_student",
JoinColumns = {@JoinColumn(name = "tno")},
inverseJoinColumns = {@JoinColumn(name = "sno")}
)
private Set<Student> studentSet;
}
学生实体
@Entity
@Table(name = "t_student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer sno;
private String sname;
private Integer age;
@ManyToMany
@JoinTable(
name = "t_teacher_student",
joinColumns ={@JoinColumn(name = "sno")},
inverseJoinColumns = {@JoinColumn(name = "tno")}
)
private Set<Teacher> teachers;
}
|