Hibernate注解方式处理实体映射关系
来源:互联网 发布:win7 64位优化版下载 编辑:程序博客网 时间:2024/06/06 05:50
在hibernate里配置对象关系映通常有两种方式一种通过配置文件方式还有一种是通过注解方式,下面简单说说通过注解方式怎么配置映射关系
//OneToMany//这里使用Oracle数据库Scott账户的Emp和Dept表做例子package com.emp.entity;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;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 org.hibernate.annotations.GenericGenerator;import org.hibernate.annotations.LazyCollection;import org.hibernate.annotations.LazyCollectionOption;@Entity// 如果我们当前这个bean要设置成实体对象,就需要加上Entity这个注解@Table(name = "DEPT", schema = "SCOTT")//设置数据库的表名public class Dept implements java.io.Serializable { private Byte deptno; private String dname; private String loc; private Set<Emp> emps = new HashSet<Emp>(0); public Dept() { } public Dept(String dname, String loc, Set<Emp> emps) { this.dname = dname; this.loc = loc; this.emps = emps; } @GenericGenerator(name = "generator", strategy = "increment") @Id//表对应主键 @GeneratedValue(generator = "generator")//主键生成策略 // Column中的name属性对应了数据库的该字段名字 @Column(name = "DEPTNO", unique = true, nullable = false, precision = 2, scale = 0) /** * @Column常用属性解析 * unique 可选,是否在该列上设置唯一约束 * nullable 可选,是否设置该列的值可以为空 * insertable 可选,该列是否作为生成的insert语句中的一个列 * updatable 可选,该列是否作为生成的update语句中的一个列 * columnDefinition 可选: 为这个特定列覆盖SQL DDL片段 (这可能导致无法在不同数据库间移植)比如我们想把Integer readNum 设为默认0,可使用此属性: INT DEFAULT 0 * length 可选,列长度 * precision 可选,列十进制精度(decimal precision) * scale 可选,如果列十进制数值范围(decimal scale)可用,在此设置 */ public Byte getDeptno() { return this.deptno; } public void setDeptno(Byte deptno) { this.deptno = deptno; } @Column(name = "DNAME", length = 14) public String getDname() { return this.dname; } public void setDname(String dname) { this.dname = dname; } @Column(name = "LOC", length = 13) public String getLoc() { return this.loc; } public void setLoc(String loc) { this.loc = loc; } //OneToMany指定了一对多的关系,mappedBy="dept"指定了由多的那一方来维护关联关系,mappedBy指的是多的一方对1的这一方的依赖的属性. //(注意:如果没有指定由谁来维护关联关系,则系统会给我们创建一张中间表) // LazyCollection属性设置成EXTRA指定了当如果查询数据的个数时候,只会发出一条 count(*)的语句,提高性能 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "dept") @LazyCollection(LazyCollectionOption.EXTRA) public Set<Emp> getEmps() { return this.emps; } public void setEmps(Set<Emp> emps) { this.emps = emps; }}//ManyToOne//上面重复的注解就不多说package com.emp.entity;import java.sql.Timestamp;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;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 = "EMP", schema = "SCOTT")public class Emp implements java.io.Serializable { private Short empno; private Dept dept; private String ename; private String job; private Short mgr; private Timestamp hiredate; private Double sal; private Double comm; public Emp() { } public Emp(Dept dept, String ename, String job, Short mgr, Timestamp hiredate, Double sal, Double comm) { this.dept = dept; this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; } @GenericGenerator(name = "generator", strategy = "increment") @Id @GeneratedValue(generator = "generator") @Column(name = "EMPNO", unique = true, nullable = false, precision = 4, scale = 0) public Short getEmpno() { return this.empno; } public void setEmpno(Short empno) { this.empno = empno; } //ManyToOne指定了多对一的关系,fetch=FetchType.LAZY属性表示在多的那一方通过延迟加载的方式加载对象(默认不是延迟加载) @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "DEPTNO")//通过 JoinColumn 的name属性指定了外键的名称 rid (注意:如果我们不通过JoinColum来指定外键的名称,系统会给我们声明一个名称) public Dept getDept() { return this.dept; } public void setDept(Dept dept) { this.dept = dept; } @Column(name = "ENAME", length = 10) public String getEname() { return this.ename; } public void setEname(String ename) { this.ename = ename; } @Column(name = "JOB", length = 9) public String getJob() { return this.job; } public void setJob(String job) { this.job = job; } @Column(name = "MGR", precision = 4, scale = 0) public Short getMgr() { return this.mgr; } public void setMgr(Short mgr) { this.mgr = mgr; } @Column(name = "HIREDATE", length = 7) public Timestamp getHiredate() { return this.hiredate; } public void setHiredate(Timestamp hiredate) { this.hiredate = hiredate; } @Column(name = "SAL", precision = 7) public Double getSal() { return this.sal; } public void setSal(Double sal) { this.sal = sal; } @Column(name = "COMM", precision = 7) public Double getComm() { return this.comm; } public void setComm(Double comm) { this.comm = comm; }}OneToOne//通过人与身份证的例子说明@Entity@Table(name="t_person")public class Person{ private int id; private String name; private IDCard card; @OneToOne(mappedBy="person") ---> 指定了OneToOne的关联关系,mappedBy同样指定由对方来进行维护关联关系 public IDCard getCard() { return card; } public void setCard(IDCard card) { this.card = card; } @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}@Entity@Table(name="t_id_card")public class IDCard{ private int id; private String no; private Person person; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } @OneToOne ---> OnetoOne指定了一对一的关联关系,一对一中随便指定一方来维护映射关系,这里选择IDCard来进行维护 @JoinColumn(name="pid") ---> 指定外键的名字 pid public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; }}//ManyToMany//多对多这里通常有两种处理方式,一种是通过建立一张中间表,然后由任一一个多的一方来维护关联关系,另一种就是将多对多拆分成两个一对多的关联关系
0 0
- Hibernate注解方式处理实体映射关系
- hibernate注解方式来处理映射关系
- Hibernate注解方式处理映射关系
- hibernate注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- Application Installation Failed 解决办法
- Spring框架总结之面向切面(一)
- MySQL数据库yyyy-MM-dd HH:mm:ss格式查询最近一周内每天的记录
- gitblit.cmd运行自动关闭
- Spring面向切面编程
- Hibernate注解方式处理实体映射关系
- ASP.NET ZERO 学习 —— (6) 开发手册之MVC后台多页面应用及迁移控制台程序
- 什么是关系型数据库??
- leetcode21~Merge Two Sorted Lists
- MyBatis的联合查询
- 【LeetCode】24. Swap Nodes in Pairs
- addroid实现广告倒计时跳Activity之CountDownTimer
- C#在窗体上画椭圆和三角形
- 循环的边界溢出