Hibernate 实现 多对一 例子
来源:互联网 发布:中国名校网络教育联盟 编辑:程序博客网 时间:2024/05/18 21:48
public class Dept { private Integer deptNo; private String deptName; public Integer getDeptNo() { return deptNo; } public void setDeptNo(Integer 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.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.hello.hql2.one" schema="happyy2165"> <class name="Dept" table="Dept" dynamic-update="true"> <id name="deptNo" column="deptNo"> <generator class="native"/> </id> <property name="deptName" column="deptName"></property> </class></hibernate-mapping>
package cn.hello.hql2.one;import java.util.Date;public class Emp { private Integer empno; private String ename; private Dept dept=new Dept(); public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; }}
<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.hello.hql2.one"><class name="Emp" table="Emp" schema="happyy2165"> <id name="empno" column="empno"> <generator class="native"></generator> </id> <property name="ename" column="ename"/> <many-to-one name="dept" column="deptno" class="cn.hello.hql2.one.Dept"></many-to-one></class></hibernate-mapping>
public class text20171006_2 { Configuration cfg; Session session; Transaction tx; SessionFactory factory; @Before public void before(){ cfg=new Configuration().configure(); factory = cfg.buildSessionFactory(); session = HibmentUtil.getSession(); tx = session.beginTransaction(); } @Test // 多对一 public void aVoid(){ cn.hello.hql2.one.Emp emp = session.get(cn.hello.hql2.one.Emp.class, 1); System.out.println(emp.getDept().getDeptName()); }}
<?xml version='1.0' encoding='utf-8'?><!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> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">happyy2165</property> <property name="connection.password">a123456a</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 使用getCurrentSession 进行配置--> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="cn/hello/entity/Dog.hbm.xml"/> <mapping resource="cn/hello/hql/Dept.hbm.xml"/> <mapping resource="cn/hello/entity/Emp.hbm.xml"/> <mapping resource="cn/hello/hql2/one/Emp.hbm.xml"/> <mapping resource="cn/hello/hql2/one/Dept.hbm.xml"/> <mapping resource="cn/hello/hql2/two/Emp.hbm.xml"/> <mapping resource="cn/hello/hql2/two/Dept.hbm.xml"/> <mapping resource="cn/hello/hql2/three/Employee.hbm.xml"/> <mapping resource="cn/hello/hql2/three/Project.hbm.xml"/> </session-factory></hibernate-configuration>
阅读全文
0 0
- Hibernate 实现 多对一 例子
- hibernate EJB注解双向(一对多、多对一)例子
- Hibernate+Spring实现多对一关联
- hibernate多对多例子
- hibernate多对多例子
- Hibernate 多对多 例子
- Hibernate多对一
- hibernate多对一
- hibernate多对一
- hibernate多对一...
- hibernate多对一
- hibernate 多对一
- hibernate多对一和一对多实现
- 【hibernate框架】多对一单向关联(XML实现)
- 【hibernate框架】多对一单向关联(Annotation实现)
- hibernate实现多对一映射文件配置
- Hibernate使用持久化注解实现多对一映射
- Hibernate Gossip: 多对一
- 简单接口回调
- hex 模拟题
- 有趣问题及答案
- 数据类型和运算符
- getParameter&getAttribute()
- Hibernate 实现 多对一 例子
- 吴恩达《深度学习》课程
- getParameter()&getAttribute()的区别
- RedHat虚拟机配置ssh免密登录无效的问题解决
- Hibernate 多对一小案例
- 使用内部的MSI振荡器给STM32L476RG单片机提供80MHz的时钟
- node * 和 node **的使用
- Java-排序算法之插入排序算法
- 流程控制