Hibernate注解
1.什么是注解
在类上或者方法上打上其他类型的类型元数据
@标志
使用过的注解:
@override 方法重写
@Test Junit 单元测试
@Before 单元测试
@After 单元测试
Hibernate中常用的注解
[email protected] 将一个类声明为持久化类
[email protected] 声明持久化类的标识属性
[email protected] 定义标识属性值的生成策略,默认为native
[email protected] 将属性映射到类
[email protected] 持久化类映射指定表
[email protected] 忽略这些属性
Hibernate中可以使用小配置文件来关联映射,也可以使用注解.
1.一对一关联
一个人Human 有一个身份证号Identity 一个身份证号只属于一个人
实体类如下
@Entity @Table public class Human { private Integer hid; @Column private String hname; private Identity identity; @OneToOne @JoinColumn(name = "iid") public Identity getIdentity() { return identity; } public void setIdentity(Identity identity) { this.identity = identity; } @Id @GeneratedValue public Integer getHid() { return hid; } public void setHid(Integer hid) { this.hid = hid; } public String getHname() { return hname; } public void setHname(String hname) { this.hname = hname; } }
@Entity @Table public class Identity { private Integer iid; private String inum; private Human human; @OneToOne(mappedBy = "identity", cascade = CascadeType.ALL) public Human getHuman() { return human; } public void setHuman(Human human) { this.human = human; } @Id @GeneratedValue public Integer getIid() { return iid; } public void setIid(Integer iid) { this.iid = iid; } public String getInum() { return inum; } public void setInum(String inum) { this.inum = inum; } }
测试如下
@Test public void insert() { Identity id = new Identity(); id.setInum("1231231212123"); Human human = new Human(); human.setHname("鬼"); human.setIdentity(id); id.setHuman(human); session.save(human); session.save(id); }
2.双向一对多关联
一个部门Dept 有多个员工Emp 一个员工只能属于一个部门
实体类如下
@Entity @Table(name = "dept3") public class Dept { @Id @GeneratedValue private Integer deptid; private String dname; @OneToMany(mappedBy = "dept", cascade = {CascadeType.ALL}) private Set<Emp> emps = new HashSet<Emp>(); public Integer getDeptid() { return deptid; } public void setDeptid(Integer deptid) { this.deptid = deptid; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public Set<Emp> getEmps() { return emps; } public void setEmps(Set<Emp> emps) { this.emps = emps; } }
@Entity @Table(name = "Emp3") public class Emp { @Id @GeneratedValue private Integer eid; private String ename; @ManyToOne @JoinColumn(name = "deptid") private Dept dept; public Integer getEid() { return eid; } public void setEid(Integer eid) { this.eid = eid; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } }
测试如下
@Test public void insert() { Dept dept = new Dept(); dept.setDname("鬼畜部"); Emp emp1 = new Emp(); emp1.setEname("天命"); emp1.setDept(dept); Emp emp2 = new Emp(); emp2.setEname("斯沃"); emp2.setDept(dept); Set<Emp> set = new HashSet<Emp>(); set.add(emp1); set.add(emp2); dept.setEmps(set); session.save(dept); }
3.多对多关联
一个玩家Player 可以玩多个游戏Game 一个游戏可以被多个玩家玩
实体类如下
@Entity @Table public class Game { @Id @GeneratedValue private Integer gid; private String gname; @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "", joinColumns = {@JoinColumn(name = "gid")}, inverseJoinColumns = {@JoinColumn(name = "pid")}) private Set<Player> players = new HashSet<Player>(); public Integer getGid() { return gid; } public void setGid(Integer gid) { this.gid = gid; } public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } public Set<Player> getPlayers() { return players; } public void setPlayers(Set<Player> players) { this.players = players; } }
@Entity @Table public class Player { @Id @GeneratedValue private Integer pid; private String pname; @ManyToMany(cascade = CascadeType.ALL, mappedBy = "players") private Set<Game> games = new HashSet<Game>(); public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Set<Game> getGames() { return games; } public void setGames(Set<Game> games) { this.games = games; } }
测试如下
@Test public void insert(){ Game g1=new Game(); g1.setGname("我的课堂"); Game g2=new Game(); g2.setGname("感恩的心"); Player p1=new Player(); p1.setPname("Chara"); Player p2=new Player(); p2.setPname("Frisk"); g1.getPlayers().add(p1); g1.getPlayers().add(p2); g2.getPlayers().add(p1); g2.getPlayers().add(p2); session.save(g1); session.save(g2); }
时间: 2024-10-22 05:00:09