hibernate 之HQL查询实例

原创
2014/06/20 15:52
阅读数 8.3K

配置文件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 {

    // Fields

    private 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 accessors

    public 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 {

    // Fields

    private 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 accessors

    public 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();
    }
}


展开阅读全文
打赏
0
3 收藏
分享
加载中
更多评论
打赏
0 评论
3 收藏
0
分享
返回顶部
顶部