2019独角兽企业重金招聘Python工程师标准>>>

配置文件hibernate.cfg.xml:

<?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><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.url">jdbc:mysql://localhost:3306/hubin</property><property name="connection.username">root</property><property name="connection.password"></property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="myeclipse.connection.profile">mysql</property><property name="show_sql">true</property><property name="format_sql">true</property><mapping resource="com/hubin/entity/Users.hbm.xml" /><mapping resource="com/hubin/entity/Userinf.hbm.xml" /></session-factory></hibernate-configuration>

关系对象模型文件主表文件Users.hbm.xml:

<?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="com.hubin.entity.Users" table="users" catalog="hubin"><id name="id" type="java.lang.Long"><column name="id" /><generator class="increment" /></id><property name="username" type="java.lang.String"><column name="username" /></property><property name="password" type="java.lang.String"><column name="password" /></property><property name="email" type="java.lang.String"><column name="email" /></property><set name="userinfs" inverse="true" cascade="delete"><key><column name="userID" not-null="true" /></key><one-to-many class="com.hubin.entity.Userinf" /></set></class>
</hibernate-mapping>

关系对象模型副表文件Userinf.hbm.xml:

<?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="com.hubin.entity.Userinf" table="userinf" catalog="hubin"><id name="id" type="java.lang.Integer"><column name="id" /><generator class="increment" /></id><many-to-one name="users" class="com.hubin.entity.Users" fetch="select"><column name="userID" not-null="true" /></many-to-one><property name="arm" type="java.lang.String"><column name="arm" length="30" /></property></class>
</hibernate-mapping>

Java代码:

实体类Users.java:

package com.hubin.entity;import java.util.HashSet;
import java.util.Set;/*** Users entity. @author MyEclipse Persistence Tools*/public class Users implements java.io.Serializable {// Fieldsprivate Long id;private String username;private String password;private String email;private Set userinfs = new HashSet(0);// Constructors/** default constructor */public Users() {}/** full constructor */public Users(String username, String password, String email, Set userinfs) {this.username = username;this.password = password;this.email = email;this.userinfs = userinfs;}// Property accessorspublic Long getId() {return this.id;}public void setId(Long id) {this.id = id;}public String getUsername() {return this.username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return this.password;}public void setPassword(String password) {this.password = password;}public String getEmail() {return this.email;}public void setEmail(String email) {this.email = email;}public Set getUserinfs() {return this.userinfs;}public void setUserinfs(Set userinfs) {this.userinfs = userinfs;}}

实体类Userinf.java:

package com.hubin.entity;/*** Userinf entity. @author MyEclipse Persistence Tools*/public class Userinf implements java.io.Serializable {// Fieldsprivate Integer id;private Users users;private String arm;// Constructors/** default constructor */public Userinf() {}/** minimal constructor */public Userinf(Users users) {this.users = users;}/** full constructor */public Userinf(Users users, String arm) {this.users = users;this.arm = arm;}// Property accessorspublic Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public Users getUsers() {return this.users;}public void setUsers(Users users) {this.users = users;}public String getArm() {return this.arm;}public void setArm(String arm) {this.arm = arm;}}

dao层代码:

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;import org.hibernate.Session;import com.hubin.entity.Userinf;
import com.hubin.entity.Users;
import com.hubin.sessionfactory.HibernateSessionFactory;public class UsersDao {/*** 获取用户信息表所有数据* */public static void getUserInfo(){Session session=HibernateSessionFactory.getSession();session.beginTransaction();List<Userinf> list=session.createQuery("from Userinf").list();System.out.println("集合的数目"+list.size());for(Userinf uf:list){System.out.println(uf.getUsers().getUsername()+" "+uf.getArm());}session.getTransaction().commit();session.close();}/*** 获取用户表所有数据信息*/public static void getUsers(){Session session=HibernateSessionFactory.getSession();session.beginTransaction();List<Users> list=session.createQuery("from Users").list();for(Users us:list){System.out.println(us.getUsername());Set<Userinf> set=us.getUserinfs();for(Userinf uf:set){System.out.print(uf.getUsers().getEmail()+"    ");}System.out.println("");}System.out.println("************************************************");Iterator<Users> it=list.iterator();while(it.hasNext()){System.out.println(it.next().getUsername());}session.getTransaction().commit();session.close();}/*** 获取用户部分信息*/public static void getUsersUsernameAndPassword(){Session session=HibernateSessionFactory.getSession();session.beginTransaction();//此时不能通过用户表对象泛型进行接收,只能通过集合对象进行接收List list=session.createQuery("select username,password from Users").list();for(int i=0;i<list.size();i++){//此时取出来的是一个对象数组Object obj[]=(Object[])list.get(i);System.out.println(obj[0].toString()+"  "+obj[1].toString());System.out.println("");}session.getTransaction().commit();session.close();}public static void main(String args[]){
//调用方法getUsersUsernameAndPassword();}
}

转载于:https://my.oschina.net/huhaoren/blog/282413

hibernate 之HQL查询实例相关推荐

  1. Hibernate 笔记 HQL查询

    http://www.cnblogs.com/zilong882008/archive/2011/11/05/2237123.html Hibernate 笔记 HQL查询(一)单属性,多属性查询 H ...

  2. java hql多条件查询_JSP 开发之hibernate的hql查询多对多查询

    JSP 开发之hibernate的hql查询多对多查询 在hibernate的hql查询中,假如说分组信息与试题是多对多关系,那么我们要在hql查询中对含有多个分组信息的试题都要查询出来.并同时查询出 ...

  3. Hibernate(九)HQL查询

    一.Hibernate提供的查询方式 OID查询方式:主键查询.通过get()或者load()方法加载指定OID的对象查询结果为一个 HQL查询方式:通过Query接口使用HQL语言进行查询 QBC查 ...

  4. hibernate的HQL查询语句

    1.标准查询Criteria (主要应用于简单的查询) Session session=HibernateSessionFactory.getSession();Query query=null;Cr ...

  5. hibernate的hql查询语句总结

    来自: http://www.cnblogs.com/xiaoluo501395377/p/3376256.html 转载于:https://www.cnblogs.com/rambo12932713 ...

  6. Hibernate 学习笔记(二)—— Hibernate HQL查询和 QBC 查询

    目录 一.Hibernate 的 HQL 查询 1.1.查询所有数据 1.2.条件查询 1.3.排序查询 1.4.统计查询 1.5.分页查询 1.6.投影查询 二.Hibernate 的 QBC 查询 ...

  7. Hibernate的查询 HQL查询 查询某几列

    HQL 是Hibernate Query Language的简写,即 hibernate 查询语言:HQL采用面向对象的查询方式.HQL查询提供了更加丰富的和灵活的查询特性,因此Hibernate将H ...

  8. hql 字符串where语句_hibernate的hql查询语句总结

    4.3 使用HQL查询 Hibernate提供了异常强大的查询体系,使用Hibernate有多种查询方式.可以选择使用Hibernate的HQL查询,或者使用条件查询,甚至可以使用原生的SQL查询语句 ...

  9. Hibernate Query数据查询

    2019独角兽企业重金招聘Python工程师标准>>> 主要由三种查询:HQL查询.Criteria条件查询.SQL查询. 以下分别讲解 1. HQL查询 HQL(Hibernate ...

最新文章

  1. Mac-使用技巧之快捷键
  2. SAP EWM Table Overview [转]
  3. python引入jit_从numba导入jit
  4. Puppy Linux U盘 Linux
  5. button按钮onclick触发不了_单按钮启停:测试模拟脉冲发生器的动作
  6. JS 给某个对象添加专属方法
  7. Spring_Hibernate
  8. 技术人观点:开发人员在处理云应用时该注意什么?
  9. JForum3 学习笔记1
  10. 部署容器jenkins_使用Jenkins部署用于进行头盔检测的烧瓶容器
  11. SpringBoot-项目2-收货地址(新增,修改,删除,设为默认收货地址)
  12. 微信开发工具获取用户头像和用户昵称,实现本地和真机调试
  13. Android游戏添加游戏动画,Android游戏中的动画制作
  14. elementary OS 5 Juno (Pantheon) 安装后配置总结(干货很多)
  15. 前有莫雷,今有清华学霸要求公开华人程序员自杀真相,被Facebook开除了
  16. linux运维是做什么工作的?有哪些岗位?
  17. 163yum源的配置安装
  18. 超详细的实现上传文件功能教程,文件上传实现。
  19. C ++程序将给定的英寸转换为等效的码,英尺和英寸
  20. 已解决 vmware 虚拟机安装后没有虚拟网卡问题

热门文章

  1. 因供应商遭不明网络攻击,丰田汽车宣布停产
  2. 【BlackHat】黑帽大会上值得关注的安全工具
  3. DevSecOps 现状:云 IT 的复杂度制造了“无法改变的”安全问题
  4. nodejs pm2的简单应用
  5. 《Kotlin 极简教程 》第5章 集合类
  6. Oracle浅谈第一回
  7. 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性...
  8. 第一周 从C走进C++ 002 命令行参数
  9. 发表优质书评,获得管家推荐【51CTO家园读书帮助】
  10. How To Replace The Firefox Icon With Your Logo