Hibernate的关系映射
来源:互联网 发布:shopnc 源码下载 编辑:程序博客网 时间:2024/06/11 09:58
Dept
[java] view plain copy
- package cn.et.hibernate.lesson02.relation;
- import java.util.HashSet;
- import java.util.Set;
- /**
- * Dept entity. @author MyEclipse Persistence Tools
- */
- public class Dept implements java.io.Serializable {
- // Fields
- private Short deptno;
- private String dname;
- private String loc;
- private Set emps = new HashSet(0);
- // Constructors
- /** default constructor */
- public Dept() {
- }
- /** minimal constructor */
- public Dept(Short deptno) {
- this.deptno = deptno;
- }
- /** full constructor */
- public Dept(Short deptno, String dname, String loc, Set emps) {
- this.deptno = deptno;
- this.dname = dname;
- this.loc = loc;
- this.emps = emps;
- }
- // Property accessors
- public Short getDeptno() {
- return this.deptno;
- }
- public void setDeptno(Short deptno) {
- this.deptno = deptno;
- }
- public String getDname() {
- return this.dname;
- }
- public void setDname(String dname) {
- this.dname = dname;
- }
- public String getLoc() {
- return this.loc;
- }
- public void setLoc(String loc) {
- this.loc = loc;
- }
- public Set getEmps() {
- return this.emps;
- }
- public void setEmps(Set emps) {
- this.emps = emps;
- }
- }
Emp
[java] view plain copy
- package cn.et.hibernate.lesson02.relation;
- import java.util.Date;
- /**
- * Emp entity. @author MyEclipse Persistence Tools
- */
- public class Emp implements java.io.Serializable {
- // Fields
- private Short empno;
- private Dept dept;
- private String ename;
- private String job;
- private Short mgr;
- private Date hiredate;
- private Double sal;
- private Double comm;
- private String sex;
- // Constructors
- /** default constructor */
- public Emp() {
- }
- /** minimal constructor */
- public Emp(Short empno) {
- this.empno = empno;
- }
- /** full constructor */
- public Emp(Short empno, Dept dept, String ename, String job, Short mgr,
- Date hiredate, Double sal, Double comm, String sex) {
- this.empno = empno;
- this.dept = dept;
- this.ename = ename;
- this.job = job;
- this.mgr = mgr;
- this.hiredate = hiredate;
- this.sal = sal;
- this.comm = comm;
- this.sex = sex;
- }
- // Property accessors
- public Short getEmpno() {
- return this.empno;
- }
- public void setEmpno(Short empno) {
- this.empno = empno;
- }
- public Dept getDept() {
- return this.dept;
- }
- public void setDept(Dept dept) {
- this.dept = dept;
- }
- public String getEname() {
- return this.ename;
- }
- public void setEname(String ename) {
- this.ename = ename;
- }
- public String getJob() {
- return this.job;
- }
- public void setJob(String job) {
- this.job = job;
- }
- public Short getMgr() {
- return this.mgr;
- }
- public void setMgr(Short mgr) {
- this.mgr = mgr;
- }
- public Date getHiredate() {
- return this.hiredate;
- }
- public void setHiredate(Date hiredate) {
- this.hiredate = hiredate;
- }
- public Double getSal() {
- return this.sal;
- }
- public void setSal(Double sal) {
- this.sal = sal;
- }
- public Double getComm() {
- return this.comm;
- }
- public void setComm(Double comm) {
- this.comm = comm;
- }
- public String getSex() {
- return this.sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- }
Dept.hbm.xml
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="cn.et.hibernate.lesson02.relation.Dept" table="DEPT" schema="SCOTT">
- <id name="deptno" type="java.lang.Short">
- <column name="DEPTNO" precision="3" scale="0" />
- <generator class="assigned" />
- </id>
- <property name="dname" type="java.lang.String">
- <column name="DNAME" length="14">
- <comment>部门名称</comment>
- </column>
- </property>
- <property name="loc" type="java.lang.String">
- <column name="LOC" length="13">
- <comment>部门所在位置</comment>
- </column>
- </property>
- <set name="emps" inverse="true">
- <key>
- <column name="DEPTNO">
- <comment>所属部门编号</comment>
- </column>
- </key>
- <one-to-many class="cn.et.hibernate.lesson02.relation.Emp" />
- </set>
- </class>
- </hibernate-mapping>
Emp.hbm.xml
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="cn.et.hibernate.lesson02.relation.Emp" table="EMP" schema="SCOTT">
- <id name="empno" type="java.lang.Short">
- <column name="EMPNO" precision="4" scale="0" />
- <generator class="assigned" />
- </id>
- <many-to-one name="dept" class="cn.et.hibernate.lesson02.relation.Dept" fetch="select">
- <column name="DEPTNO" precision="2" scale="0">
- <comment>所属部门编号</comment>
- </column>
- </many-to-one>
- <property name="ename" type="java.lang.String">
- <column name="ENAME" length="10">
- <comment>员工姓名</comment>
- </column>
- </property>
- <property name="job" type="java.lang.String">
- <column name="JOB" length="9">
- <comment>职位</comment>
- </column>
- </property>
- <property name="mgr" type="java.lang.Short">
- <column name="MGR" precision="4" scale="0">
- <comment>领导编号</comment>
- </column>
- </property>
- <property name="hiredate" type="java.util.Date">
- <column name="HIREDATE" length="7">
- <comment>雇佣日期</comment>
- </column>
- </property>
- <property name="sal" type="java.lang.Double">
- <column name="SAL" precision="7">
- <comment>月薪</comment>
- </column>
- </property>
- <property name="comm" type="java.lang.Double">
- <column name="COMM" precision="7">
- <comment>奖金</comment>
- </column>
- </property>
- <property name="sex" type="java.lang.String">
- <column name="SEX" length="3" />
- </property>
- </class>
- </hibernate-mapping>
hibernate.cfg.xml
[html] view plain copy
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <!-- Generated by MyEclipse Hibernate Tools. -->
- <hibernate-configuration>
- <session-factory>
- <!-- 告诉hibernate使用的是orcle数据库 -->
- <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
- <!-- 配置session Factory四要素 -->
- <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
- <property name="connection.username">scott</property>
- <property name="connection.password">tiger</property>
- <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
- <!-- 打印日志 -->
- <property name="show_sql">true</property>
- <!-- 扫描映射文件 -->
- <mapping resource="cn/et/hibernate/lesson02/relation/Dept.hbm.xml"/>
- <mapping resource="cn/et/hibernate/lesson02/relation/Emp.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
TestHibernate
[java] view plain copy
- package cn.et.hibernate.lesson02.relation;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import org.junit.Test;
- public class TestHibernate {
- /**
- */
- @Test
- public void test(){
- SessionFactory sf = new Configuration().configure("/cn/et/hibernate/lesson02/relation/hibernate.cfg.xml").buildSessionFactory();
- Session session = sf.openSession();
- Dept dept = (Dept) session.get(Dept.class,Short.parseShort("10"));
- Emp emp = (Emp)dept.getEmps().iterator().next();
- System.out.println(emp.getEname());
- }
- }
阅读全文
0 0
- hibernate 的映射关系
- Hibernate的关系映射
- hibernate的映射关系
- Hibernate的关系映射
- Hibernate的映射关系
- hibernate的关系映射
- Hibernate的关系映射
- Hibernate的关系映射
- Hibernate的映射关系
- hibernate的映射关系
- Hibernate的映射关系
- Hibernate的映射关系
- 【Hibernate】Hibernate的映射关系
- Hibernate的关联关系映射
- Hibernate的关联关系映射
- Hibernate的关联关系映射
- Hibernate的关联关系映射
- Hibernate的单向关系映射
- java中的包
- 完整iOS APP发布App Store上架流程
- VS2013 密钥
- ExpandTable手写点击下拉滑动模块
- js 操作数组
- Hibernate的关系映射
- MapStruct 代替BeanUtil 和ModelMapper
- Java实现-搜索旋转排序数组2
- 单元素的枚举类型实现单例
- python——使用map快速匹配字典的key
- redis 锁
- 从数据库获取long类型的当前时间
- Python条件语句和循环语句
- Hibernate的load和get