Hibernate 多对一小案例
来源:互联网 发布:中国名校网络教育联盟 编辑:程序博客网 时间:2024/06/05 09:11
1.创建实体类Dept和Emp以及小配置
package cn.happy.manytoone;/** * Created by guo on 2017/10/6. */public class Dept { private int deptno; private String deptname; public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; }}
-----------------------------------------------------------------------------------<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernaate.net/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.happy.manytoone"> <class name="Dept" schema="guohua"> <id name="deptno"> <!-- 算法的核心思想是结合机器的网卡、当地时间、一个随机数来生成GUID --> <generator class="native"></generator> </id> <property name="deptname"/> </class></hibernate-mapping>--------------------------------------------------------------------------------------package cn.happy.manytoone;/** * Created by guo on 2017/10/6. */public class Emp{ private int empno; private String ename; private Dept dept=new Dept(); public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public int getEmpno() { return empno; } public void setEmpno(int empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; }}---------------------------------------------------------------------
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernaate.net/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.happy.manytoone"> <class name="Emp" schema="guohua"> <id name="empno"> <!-- 算法的核心思想是结合机器的网卡、当地时间、一个随机数来生成GUID --> <generator class="native"></generator> </id> <property name="ename"/> <many-to-one name="dept" column="deptno" class="Dept"></many-to-one> </class></hibernate-mapping>-----------------------------------------------------------------------------------------------------------------------------------------------------------
2.编写大配置文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- 数据库链接--> <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">guohua</property> <property name="connection.password">123456</property> <!-- 辅助参数--> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <property name="format_sql">true</property> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- <property name="current_session_context_class">thread</property>--> <mapping resource="cn/happy/manytoone/Dept.hbm.xml"/> <mapping resource="cn/happy/manytoone/Emp.hbm.xml"/> <mapping resource="cn/happy/onetomanydouble/Dept.hbm.xml"/> <mapping resource="cn/happy/onetomanydouble/Emp.hbm.xml"/> <mapping resource="cn/happy/manytomany/Employee.hbm.xml"/> <mapping resource="cn/happy/manytomany/Project.hbm.xml"/> </session-factory></hibernate-configuration>
----------------------------------------------------------------------------------------------------------
3.编写测试类
package cn.happy.test;import cn.happy.manytoone.Emp;import cn.happy.util.sessionUtil;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;/** * Created by guo on 2017/10/6. */public class manytoone { public static void main(String[] args) { Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); //Session session = factory.openSession(); Session session = sessionUtil.getSession(); Transaction tx = session.beginTransaction(); Emp emp = session.get(Emp.class, 1); System.out.println(emp.getDept().getDeptname()); tx.commit(); session.close(); }}
阅读全文
0 0
- Hibernate 多对一小案例
- Hibernate多对一小案例
- Hibernate 多对多小案例
- Hibernate多对多的小案例
- hibernate详解(三)---单向多对一案例
- hibernate详解(四)---双向多对一案例
- hibernate系列七:多对一关联配置及案例
- Hibernate (分页,一对多,多对多,多对一)案例
- 小白记录~Hibernate中多对一关系
- Hibernate多对一
- hibernate多对一
- hibernate多对一
- hibernate多对一...
- hibernate多对一
- hibernate 多对一
- Hibernate一对多的小案例
- hibernate系列九:多对多关联配置及案例(一)
- read-Atleap-10-主业务分析-ContentField类-Hibernate多对一关系实施案例
- getParameter&getAttribute()
- Hibernate 实现 多对一 例子
- 吴恩达《深度学习》课程
- getParameter()&getAttribute()的区别
- RedHat虚拟机配置ssh免密登录无效的问题解决
- Hibernate 多对一小案例
- 使用内部的MSI振荡器给STM32L476RG单片机提供80MHz的时钟
- node * 和 node **的使用
- Java-排序算法之插入排序算法
- 流程控制
- 快速幂 取模
- poj 2429 GCD & LCM Inverse
- Hibernate (分页,一对多,多对多,多对一)案例
- BZOJ1218: [HNOI2003]激光炸弹