爱成过去 發表於 2023-9-24 00:00:00

HQL查询语言的使用介绍

<p>
        HQL查询依赖于Query类,每个Query实例对应一个查询对象,使用HQL查询按如下步骤进行:</p>
<p>
        1.获取Hibernate Session对象<br>
        2.编写HQL语句<br>
        3.以HQL语句作为参数,调用Session的createQuery方法创建查询对象<br>
        4.如果HQL语句包含参数,则调用Query的setXxx方法为参数赋值<br>
        5.调用Query独享的list()或uniqueResult()方法返回查询结果列表</p>
<p>
        简单的例子:</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code7931">
        <p>
                <br>
                @SuppressWarnings("deprecation")<br>
                public class HibernateUtil {</p>
        <p>
                 </p>
        <p>
                    private static final SessionFactory sessionFactory;<br><br>
                    static {<br>
                        sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();<br>
                    }<br><br>
                    public static Session getOpenSession() {<br>
                        return sessionFactory.openSession();<br>
                    }<br><br>
                    public static Session getCurrentSession() {<br>
                        return sessionFactory.getCurrentSession();<br>
                    }<br>
                }<br>
                @Entity<br>
                public class Employee {</p>
        <p>
                    private Integer id;<br>
                    private String name;<br>
                    private Integer age;<br><br>
                    @Id<br>
                    @GeneratedValue<br>
                    public Integer getId() {<br>
                        return id;<br>
                    }<br><br>
                    public void setId(Integer id) {<br>
                        this.id = id;<br>
                    }<br><br>
                    @Basic<br>
                    public String getName() {<br>
                        return name;<br>
                    }<br><br>
                    public void setName(String name) {<br>
                        this.name = name;<br>
                    }<br><br>
                    @Basic<br>
                    public Integer getAge() {<br>
                        return age;<br>
                    }<br><br>
                    public void setAge(Integer age) {<br>
                        this.age = age;<br>
                    }<br><br>
                    public String toString() {<br>
                        return "id:" + id + "   " + "name:" + name +  "   " + "age:" + age;<br>
                    }<br>
                }<br>
                @SuppressWarnings("all")<br>
                public class HQLDemo {</p>
        <p>
                    @Test<br>
                    public void testHQL() {<br>
                        Session session = HibernateUtil.getOpenSession();<br>
                        List&lt;Employee&gt; employeeList = session.createQuery("from Employee as e").list();<br>
                        for(Employee e : employeeList)<br>
                            System.out.println(e);<br>
                    }<br><br>
                    @Test<br>
                    public void testHQLHasParameter() {<br>
                        Session session = HibernateUtil.getOpenSession();<br>
                        List&lt;Employee&gt; employeeList = session.createQuery("from Employee as e where e.name = :personName").setString("personName", "xujianguo").list();<br>
                        for(Employee e : employeeList)<br>
                            System.out.println(e);<br>
                    }<br>
                }</p>
</div>
<p>
         </p>
<p>
        HQL查询的from子句:</p>
<p>
        from是最简单的HQL语句,也是最基本的HQL语句,from关键字后紧跟持久化类的类名,如:</p>
<p>
        from Employee表名从Employee类中选出全部的实例</p>
<p>
        不过我们常用的是这样做:</p>
<p>
        from employee as e这个e就是Employee的别名,也就是实例名,推荐这么写。</p>
<p>
          HQL查询的select子句:</p>
<p>
          select子句用于选择指定的属性或直接选择某个实体,当然select选择的属性必须是from后持久化类包含的属性,如:</p>
<p>
        select e.name from Employee as e  select可以选择任意属性,即不仅可以选择持久化类的直接属性,还可以选择组件属性包含的属性,如:</p>
<p>
        select e.name.firstName from Employee as eHQL查询的聚集函数:</p>
<p>
          聚集函数:  </p>
<p>
            avg:计算属性的平均值</p>
<p>
            count:统计选择对象的数量</p>
<p>
            max:统计属性值的最大值</p>
<p>
            min:统计属性值的最小值</p>
<p>
            sum:计算属性值的总和</p>
<p>
        如:</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code99645">
        <br>
        select count(*) from Employee as e    @Test<br>
            public void testHQLFunction() {<br>
                Session session = HibernateUtil.getOpenSession();<br>
                System.out.println(session.createQuery("select count(*) from Employee as e").uniqueResult());<br>
            }  </div>
<p>
         </p>
<p>
            多态查询:</p>
<p>
          HQL不仅会查询出该持久化类的全部实例,还会查询出该类的子类的全部实例,前提是存在继承映射。</p>
<p>
          HQL查询的where子句:</p>
<p>
          where子句主要用于筛选选中的结果,缩小选择的范围,如:</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code77618">
        <br>
        from employee as e where e.name like "xjg%"    @Test<br>
            public void testHQLWhere() {<br>
                Session session = HibernateUtil.getOpenSession();<br>
                List&lt;Employee&gt; employeeList = session.createQuery("from Employee as e where e.name like 'zhou%'").list();<br>
                for(Employee e : employeeList)<br>
                    System.out.println(e);<br>
            }  </div>
<p>
         </p>
<p>
            order by子句:</p>
<p>
          查询返回结合可以根据类或组件属性的任何属性进行排序,还可以使用asc或desc关键字指定升序或者降序,如:</p>
<p>
        from Employee as e order by e.name desc  </p>
<p>
        子查询:</p>
<p>
          子查询中就是查询语句中还有查询语句,如:</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code17139">
        <p>
                <br>
                from Employee as e where e.age &gt; (select p.age from Person as p)    @Test<br>
                    public void testHQLChildQuery() {<br>
                        Session session = HibernateUtil.getOpenSession();<br>
                        List&lt;Employee&gt; employeeList = session.createQuery("from Employee as e where e.age &gt; (select e1.age from Employee as e1 where e1.name = 'xujianguo')").list();<br>
                        for(Employee e : employeeList)<br>
                            System.out.println(e);<br>
                    }  <br>
                </p>
        <p>
                 </p>
        <p>
                    命名查询:</p>
        <p>
                HQL查询还支持将查询所用的HQL语句放入配置文件中,而不是代码中,在Hibernate映射文件的&lt;hibernate-mapping&gt;元素中使用&lt;query&gt;子元素来定义命名查询,这个&lt;query&gt;元素只需指定一个name属性,指定该命名查询的名字 ,如:</p>
        <p>
                <br>
                &lt;query name="query"&gt;<br>
                    from Employee as e<br>
                &lt;query /&gt;  </p>
</div>
<p>
         </p>
<p>
        Session里提供了一个getNamedQuery(String name)方法,该方法用于创建一个Query对象,一旦获得Query对象,剩下的工作就跟前面的一样了。</p>
<p>
         </p>
<div class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</div>
<div class="codebody" id="code98577">
        <br>
            @Test<br>
            public void testHQLNamedQuery() {<br>
                Session session = HibernateUtil.getOpenSession();<br>
                List&lt;Employee&gt; employeeList = session.getNamedQuery("query").list();<br>
                for(Employee e : employeeList)<br>
                    System.out.println(e);<br>
            }</div>
<p>
         </p>
<p>
        HQL 查询语句</p>
<p>
        /**<br>
         *<br>
         */<br>
        package com.b510.example;</p>
<p>
        import java.util.Iterator;<br>
        import java.util.List;<br>
        import java.util.Map;</p>
<p>
        import org.hibernate.Criteria;<br>
        import org.hibernate.FetchMode;<br>
        import org.hibernate.Query;<br>
        import org.hibernate.Session;</p>
<p>
        /**<br>
         *<br>
         * @author XHW<br>
         *<br>
         * @date 2011-6-18<br>
         *<br>
         */<br>
        public class HibernateTest {</p>
<p>
         /**<br>
          * @param args<br>
          */<br>
         public static void main(String[] args) {<br>
          HibernateTest test = new HibernateTest();<br>
          test.where();<br>
          test.function();<br>
          test.update();<br>
          test.jiaoChaCheck();<br>
          test.innerJoin();<br>
          test.QBC();<br>
          test.leftOuterJoin();<br>
          test.rightOuterJoin();<br>
         }</p>
<p>
        <br>
         public void where() {<br>
          // 使用where查询<br>
          Session session = HibernateSessionFactoryUtil.getSessionFactory()<br>
            .openSession();<br>
          session.beginTransaction();<br>
          Query query = session<br>
            .createQuery("from User where id not between 200 and 2000");<br>
          List&lt;User&gt; list = query.list();</p>
<p>
          for (User user : list) {<br>
           System.out.println(user.getId() + user.getUsername());<br>
          }<br>
          // 投影查询 中使用where子句<br>
          query = session.createQuery("select username from User where id=2");<br>
          List&lt;String&gt; listname = query.list();</p>
<p>
          for (String name : listname) {<br>
           System.out.println(name);<br>
          }<br>
          // in查询<br>
          query = session<br>
            .createQuery("from User where username in ('Hongten','Hanyuan','dfgd')");<br>
          List&lt;User&gt; listin = query.list();</p>
<p>
          for (User user : listin) {<br>
           System.out.println(user.getId() + user.getUsername());<br>
          }<br>
          // like查询<br>
          query = session.createQuery("from User where username not like 'Hon%'");<br>
          List&lt;User&gt; listlike = query.list();</p>
<p>
          for (User user : listlike) {<br>
           System.out.println(user.getId() + user.getUsername());<br>
          }<br>
          // null查询<br>
          query = session.createQuery("from User where password is null");<br>
          List&lt;User&gt; listnull = query.list();</p>
<p>
          for (User user : listnull) {<br>
           System.out.println(user.getId() + user.getUsername());<br>
          }<br>
          // and查询<br>
          query = session<br>
            .createQuery("from User where password is not null and id&lt;5");<br>
          List&lt;User&gt; listand = query.list();</p>
<p>
          for (User user : listand) {<br>
           System.out.println(user.getId() + user.getUsername()<br>
             + user.getPassword());<br>
          }<br>
          // order by<br>
          query = session.createQuery("from User order by username,id desc");<br>
          List&lt;User&gt; listorderby = query.list();</p>
<p>
          for (User user : listorderby) {<br>
           System.out.println(user.getId() + user.getUsername());<br>
          }<br>
          // 使用"?"号 作为参数占位符,一条HQL语句中可以使用多个?<br>
          // query.setInteger(0,2)<br>
          // query.setString(0,"Hongten")<br>
          query = session<br>
            .createQuery("select username from User where username=?");<br>
          query.setString(0, "Hongten");<br>
          List&lt;String&gt; listwenhao = query.list();<br>
          for (String name : listwenhao) {<br>
           System.out.println(name);<br>
          }</p>
<p>
          session.getTransaction().commit();</p>
<p>
         }</p>
<p>
         public void function() {// 把大写字母转化为小写字母<br>
          // 作用可以用在:比如在一个用户注册的程序中,大小写不容易区分,但是全部转化为小写后就可以很容易进行比较<br>
          Session session = HibernateSessionFactoryUtil.getSessionFactory()<br>
            .openSession();<br>
          session.beginTransaction();<br>
          // 输出原始的数据<br>
          Query query = session.createQuery("select username from User");<br>
          List&lt;String&gt; list = query.list();</p>
<p>
          for (String name : list) {<br>
           System.out.println(name);<br>
          }<br>
          System.out.println("-------------------------------------------");<br>
          // 输出的数据全部转化为小写<br>
          query = session.createQuery("select lower(username) from User");<br>
          List&lt;String&gt; listChange = query.list();</p>
<p>
          for (String name : listChange) {<br>
           System.out.println(name);<br>
          }<br>
          session.getTransaction().commit();<br>
         }</p>
<p>
         public void update() {<br>
          Session session = HibernateSessionFactoryUtil.getSessionFactory()<br>
            .openSession();<br>
          session.beginTransaction();<br>
          Query query = session<br>
            .createQuery("update User set username='洪伟1231' where id=?");<br>
          query.setInteger(0, 3);<br>
          int rowCount = query.executeUpdate();<br>
          System.out.println(rowCount);<br>
          session.getTransaction().commit();<br>
         }</p>
<p>
         public void operateProfile() {// 对profile这个类执行HQL语句操作<br>
          Session session = HibernateSessionFactoryUtil.getSessionFactory()<br>
            .openSession();<br>
          session.beginTransaction();<br>
          // 执行查询操作<br>
          Query query = session.createQuery("from Profile");<br>
          List&lt;Profile&gt; list = query.list();<br>
          for (Profile profile : list) {<br>
           System.out.println(profile.getId() + profile.getEmail()<br>
             + profile.getAddress() + profile.getMobile()<br>
             + profile.getPostcode());<br>
          }<br>
          // 执行删除操作<br>
          query = session.createQuery("delete from Profile where id=?");<br>
          query.setInteger(0, 3);<br>
          int rowCount = query.executeUpdate();<br>
          System.out.println(rowCount);<br>
          session.getTransaction().commit();<br>
         }</p>
<p>
         public void jiaoChaCheck() {//交叉查询<br>
          //这种方法查询出来的结果是笛卡尔积,对于我们开发中没有多大用处<br>
          Session session = HibernateSessionFactoryUtil.getSessionFactory()<br>
            .openSession();<br>
          session.beginTransaction();<br>
          Query query=session.createQuery("from User,Profile");<br><br>
          List&lt;Object[]&gt; list=query.list();<br><br>
          for(Object[] values:list){<br>
           User user=(User)values;<br>
           System.out.print("ID :"+user.getId()+",UserName:"+user.getUsername()+",Password:"+user.getPassword());<br>
           Profile profile=(Profile)values;<br>
           System.out.println(profile.getEmail()+profile.getMobile()+profile.getAddress()+profile.getPostcode());<br>
          }<br><br>
          session.getTransaction().commit();<br>
         }</p>
<p>
         public void innerJoin(){//内连接查询<br>
          /**<br>
           * 下面三种hql语句都是可以得到相同的结果<br>
           * String hql="select p from Profile as p inner join p.user";<br>
           * 在下面的hql语句中加入"fetch"后,此hql语句变为了"迫切HQL"语句,这样的查询效率要比上面的hql语句要高<br>
           * String hql="select p from Profile as p inner join fetch p.user";<br>
           *<br>
           * String hql="select p from Profile p,User u where p.user=u";<br>
           * String hql="select p from Profile p,User u where p.user.id=u.id";<br>
           * <br>
           */ <br>
          Session session = HibernateSessionFactoryUtil.getSessionFactory()<br>
            .openSession();<br>
          session.beginTransaction();<br>
          String hql="select p from Profile as p inner join fetch p.user";<br>
          //String hql="select p from Profile p,User u where p.user=u";<br>
          //String hql="select p from Profile p,User u where p.user.id=u.id";<br>
          Query query=session.createQuery(hql);<br>
          List&lt;Profile&gt; list=query.list();<br>
          for(Profile p:list){<br>
           System.out.println("ID:"+p.getUser().getId()+"   Username: "+p.getUser().getUsername()+"   Email: "+p.getEmail()+",   Address: "+p.getAddress());<br>
          }<br>
          session.getTransaction().commit();<br>
          }</p>
<p>
         public void QBC(){//QBC中实现内连接查询<br>
          Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession();<br>
          session.beginTransaction();<br>
          Criteria criteria=session.createCriteria(Profile.class).createCriteria("user");<br>
          List&lt;Profile&gt; list=criteria.list();<br><br>
          for(Profile p:list){<br>
           System.out.println("ID:"+p.getUser().getId()+"   Username: "+p.getUser().getUsername()+"   Email: "+p.getEmail()+",   Address: "+p.getAddress());<br>
          }<br>
          //QBC中实现外连接<br>
          System.out.println("##################################################");<br>
          criteria=session.createCriteria(Profile.class).setFetchMode("user", FetchMode.JOIN);<br>
          List&lt;Profile&gt; listp=criteria.list();<br><br>
          for(Profile p:list){<br>
           System.out.println("ID:"+p.getUser().getId()+"   Username: "+p.getUser().getUsername()+"   Email: "+p.getEmail()+",   Address: "+p.getAddress());   <br>
          } <br>
          session.getTransaction().commit();<br>
         }</p>
<p>
         public void leftOuterJoin(){//左外连接<br>
          /**<br>
           * String hql="select p from Profile p left outer join p.user order by p.user.id";<br>
           * 在下面的hql语句中加入"fetch"后,此hql语句变为了"迫切HQL"语句,这样的查询效率要比上面的hql语句要高<br>
           * String hql="select p from Profile p left outer join fetch p.user order by p.user.id";<br>
           *<br>
           * String hqlu="select u from User u left outer join u.profiles";<br>
           *  在下面的hql语句中加入"fetch"后,此hql语句变为了"迫切HQL"语句,这样的查询效率要比上面的hql语句要高<br>
           * String hqlu="select u from User u left outer join fetch u.profiles";<br>
           */<br>
          Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();<br>
          session.beginTransaction();<br>
          String hql="select p from Profile p left outer join fetch p.user order by p.user.id";<br>
          Query query=session.createQuery(hql);<br><br>
          List&lt;Profile&gt; list=query.list();<br>
          for(Profile p:list){<br>
           System.out.println("ID:"+p.getUser().getId()+"   Username: "+p.getUser().getUsername()+"   Email: "+p.getEmail()+",   Address: "+p.getAddress());<br>
          }<br><br>
          System.out.println("-------------------------------------");<br>
          String hqlu="select u from User u left outer join fetch u.profiles";<br>
          query=session.createQuery(hqlu);<br><br>
          List&lt;User&gt; listu=query.list();<br>
          for(User u:listu){<br>
           System.out.println(u.getId()+u.getUsername()+u.getProfiles());<br>
          }<br>
          session.getTransaction().commit();<br><br>
         }<br><br>
         public void rightOuterJoin(){//右外连接<br>
          Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();<br>
          session.beginTransaction();<br>
          String hql="select u from User u right outer join u.profiles order by u.id";<br>
          Query query=session.createQuery(hql);<br><br>
          List&lt;User&gt; listu=query.list();<br>
          for(User user:listu){<br>
           System.out.println(user.getId()+user.getUsername()+user.getProfiles());<br>
          }<br><br>
          session.getTransaction().commit();<br><br>
         }<br><br>
        }</p>
<p>
        结果:</p>
<p>
        log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).<br>
        log4j:WARN Please initialize the log4j system properly.<br>
        Hibernate:<br>
            select<br>
                user0_.id as id0_,<br>
                user0_.username as username0_,<br>
                user0_.password as password0_<br>
            from<br>
                users.user user0_<br>
            where<br>
                user0_.id not between 200 and 2000<br>
        1hongten<br>
        2hanyuan<br>
        3hongwei<br>
        4mingliu<br>
        5shouzhang<br>
        Hibernate:<br>
            select<br>
                user0_.username as col_0_0_<br>
            from<br>
                users.user user0_<br>
            where<br>
                user0_.id=2<br>
        hanyuan<br>
        Hibernate:<br>
            select<br>
                user0_.id as id0_,<br>
                user0_.username as username0_,<br>
                user0_.password as password0_<br>
            from<br>
                users.user user0_<br>
            where<br>
                user0_.username in (<br>
                    'Hongten' , 'Hanyuan' , 'dfgd'<br>
                )<br>
        1hongten<br>
        2hanyuan<br>
        Hibernate:<br>
            select<br>
                user0_.id as id0_,<br>
                user0_.username as username0_,<br>
                user0_.password as password0_<br>
            from<br>
                users.user user0_<br>
            where<br>
                user0_.username not like 'Hon%'<br>
        2hanyuan<br>
        4mingliu<br>
        5shouzhang<br>
        Hibernate:<br>
            select<br>
                user0_.id as id0_,<br>
                user0_.username as username0_,<br>
                user0_.password as password0_<br>
            from<br>
                users.user user0_<br>
            where<br>
                user0_.password is null<br>
        Hibernate:<br>
            select<br>
                user0_.id as id0_,<br>
                user0_.username as username0_,<br>
                user0_.password as password0_<br>
            from<br>
                users.user user0_<br>
            where<br>
                (<br>
                    user0_.password is not null<br>
                )<br>
                and user0_.id&lt;5<br>
        1hongten123<br>
        2hanyuan5645645<br>
        3hongwei5645645<br>
        4mingliu5645645<br>
        Hibernate:<br>
            select<br>
                user0_.id as id0_,<br>
                user0_.username as username0_,<br>
                user0_.password as password0_<br>
            from<br>
                users.user user0_<br>
            order by<br>
                user0_.username,<br>
                user0_.id desc<br>
        2hanyuan<br>
        1hongten<br>
        3hongwei<br>
        4mingliu<br>
        5shouzhang<br>
        Hibernate:<br>
            select<br>
                user0_.username as col_0_0_<br>
            from<br>
                users.user user0_<br>
            where<br>
                user0_.username=?<br>
        hongten<br>
        Hibernate:<br>
            select<br>
                user0_.username as col_0_0_<br>
            from<br>
                users.user user0_<br>
        hongten<br>
        hanyuan<br>
        hongwei<br>
        mingliu<br>
        shouzhang<br>
        -------------------------------------------<br>
        Hibernate:<br>
            select<br>
                lower(user0_.username) as col_0_0_<br>
            from<br>
                users.user user0_<br>
        hongten<br>
        hanyuan<br>
        hongwei<br>
        mingliu<br>
        shouzhang<br>
        Hibernate:<br>
            update<br>
                users.user<br>
            set<br>
                username='Hongwei1231'<br>
            where<br>
                id=?<br>
        1<br>
        Hibernate:<br>
            select<br>
                user0_.id as id0_0_,<br>
                profile1_.id as id1_1_,<br>
                user0_.username as username0_0_,<br>
                user0_.password as password0_0_,<br>
                profile1_.user_id as user2_1_1_,<br>
                profile1_.email as email1_1_,<br>
                profile1_.phone as phone1_1_,<br>
                profile1_.mobile as mobile1_1_,<br>
                profile1_.address as address1_1_,<br>
                profile1_.postcode as postcode1_1_<br>
            from<br>
                users.user user0_,<br>
                users.profile profile1_<br>
        ID :1,UserName:hongten,Password:123hongtenzone@foxmail.com45464Guangzhoushi65465<br>
        ID :1,UserName:hongten,Password:123hanyuan@foxmail.com45648255GuangzhoushiDianbian65465<br>
        ID :1,UserName:hongten,Password:123hanyuan@foxmail.com45648255GuangzhoushiDianbian65465<br>
        ID :2,UserName:hanyuan,Password:5645645hongtenzone@foxmail.com45464Guangzhoushi65465<br>
        ID :2,UserName:hanyuan,Password:5645645hanyuan@foxmail.com45648255GuangzhoushiDianbian65465<br>
        ID :2,UserName:hanyuan,Password:5645645hanyuan@foxmail.com45648255GuangzhoushiDianbian65465<br>
        ID :3,UserName:Hongwei1231,Password:5645645hongtenzone@foxmail.com45464Guangzhoushi65465<br>
        ID :3,UserName:Hongwei1231,Password:5645645hanyuan@foxmail.com45648255GuangzhoushiDianbian65465<br>
        ID :3,UserName:Hongwei1231,Password:5645645hanyuan@foxmail.com45648255GuangzhoushiDianbian65465<br>
        ID :4,UserName:mingliu,Password:5645645hongtenzone@foxmail.com45464Guangzhoushi65465<br>
        ID :4,UserName:mingliu,Password:5645645hanyuan@foxmail.com45648255GuangzhoushiDianbian65465<br>
        ID :4,UserName:mingliu,Password:5645645hanyuan@foxmail.com45648255GuangzhoushiDianbian65465<br>
        ID :5,UserName:shouzhang,Password:5645645hongtenzone@foxmail.com45464Guangzhoushi65465<br>
        ID :5,UserName:shouzhang,Password:5645645hanyuan@foxmail.com45648255GuangzhoushiDianbian65465<br>
        ID :5,UserName:shouzhang,Password:5645645hanyuan@foxmail.com45648255GuangzhoushiDianbian65465<br>
        Hibernate:<br>
            select<br>
                profile0_.id as id1_0_,<br>
                user1_.id as id0_1_,<br>
                profile0_.user_id as user2_1_0_,<br>
                profile0_.email as email1_0_,<br>
                profile0_.phone as phone1_0_,<br>
                profile0_.mobile as mobile1_0_,<br>
                profile0_.address as address1_0_,<br>
                profile0_.postcode as postcode1_0_,<br>
                user1_.username as username0_1_,<br>
                user1_.password as password0_1_<br>
            from<br>
                users.profile profile0_<br>
            inner join<br>
                users.user user1_<br>
                    on profile0_.user_id=user1_.id<br>
        ID:1   Username: hongten   Email: hongtenzone@foxmail.com,   Address: Guangzhoushi<br>
        ID:2   Username: hanyuan   Email: hanyuan@foxmail.com,   Address: GuangzhoushiDianbian<br>
        ID:3   Username:Hongwei1231   Email: hanyuan@foxmail.com,   Address: GuangzhoushiDianbian<br>
        Hibernate:<br>
            select<br>
                this_.id as id1_1_,<br>
                this_.user_id as user2_1_1_,<br>
                this_.email as email1_1_,<br>
                this_.phone as phone1_1_,<br>
                this_.mobile as mobile1_1_,<br>
                this_.address as address1_1_,<br>
                this_.postcode as postcode1_1_,<br>
                user1_.id as id0_0_,<br>
                user1_.username as username0_0_,<br>
                user1_.password as password0_0_<br>
            from<br>
                users.profile this_<br>
            inner join<br>
                users.user user1_<br>
                    on this_.user_id=user1_.id<br>
        ID:1   Username: hongten   Email: hongtenzone@foxmail.com,   Address: Guangzhoushi<br>
        ID:2   Username: hanyuan   Email: hanyuan@foxmail.com,   Address: GuangzhoushiDianbian<br>
        ID:3   Username: Hongwei1231   Email: hanyuan@foxmail.com,   Address: GuangzhoushiDianbian<br>
        ##################################################<br>
        Hibernate:<br>
            select<br>
                this_.id as id1_1_,<br>
                this_.user_id as user2_1_1_,<br>
                this_.email as email1_1_,<br>
                this_.phone as phone1_1_,<br>
                this_.mobile as mobile1_1_,<br>
                this_.address as address1_1_,<br>
                this_.postcode as postcode1_1_,<br>
                user2_.id as id0_0_,<br>
                user2_.username as username0_0_,<br>
                user2_.password as password0_0_<br>
            from<br>
                users.profile this_<br>
            left outer join<br>
                users.user user2_<br>
                    on this_.user_id=user2_.id<br>
        ID:1   Username: hongten   Email: hongtenzone@foxmail.com,   Address: Guangzhoushi<br>
        ID:2   Username: hanyuan   Email: hanyuan@foxmail.com,   Address: GuangzhoushiDianbian<br>
        ID:3   Username: 洪伟1231   Email: hanyuan@foxmail.com,   Address: GuangzhoushiDianbian<br>
        Hibernate:<br>
            select<br>
                profile0_.id as id1_0_,<br>
                user1_.id as id0_1_,<br>
                profile0_.user_id as user2_1_0_,<br>
                profile0_.email as email1_0_,<br>
                profile0_.phone as phone1_0_,<br>
                profile0_.mobile as mobile1_0_,<br>
                profile0_.address as address1_0_,<br>
                profile0_.postcode as postcode1_0_,<br>
                user1_.username as username0_1_,<br>
                user1_.password as password0_1_<br>
            from<br>
                users.profile profile0_<br>
            left outer join<br>
                users.user user1_<br>
                    on profile0_.user_id=user1_.id<br>
            order by<br>
                profile0_.user_id<br>
        ID:1   Username: hongten   Email: hongtenzone@foxmail.com,   Address: Guangzhoushi<br>
        ID:2   Username: hanyuan   Email: hanyuan@foxmail.com,   Address: GuangzhoushiDianbian<br>
        ID:3   Username: 洪伟1231   Email: hanyuan@foxmail.com,   Address: GuangzhoushiDianbian<br>
        -------------------------------------<br>
        Hibernate:<br>
            select<br>
                user0_.id as id0_0_,<br>
                profiles1_.id as id1_1_,<br>
                user0_.username as username0_0_,<br>
                user0_.password as password0_0_,<br>
                profiles1_.user_id as user2_1_1_,<br>
                profiles1_.email as email1_1_,<br>
                profiles1_.phone as phone1_1_,<br>
                profiles1_.mobile as mobile1_1_,<br>
                profiles1_.address as address1_1_,<br>
                profiles1_.postcode as postcode1_1_,<br>
                profiles1_.user_id as user2_0__,<br>
                profiles1_.id as id0__<br>
            from<br>
                users.user user0_<br>
            left outer join<br>
                users.profile profiles1_<br>
                    on user0_.id=profiles1_.user_id<br>
        1hongten<br>
        2hanyuan<br>
        3Hongwei1231<br>
        4mingliu[]<br>
        5shouzhang[]<br>
        Hibernate:<br>
            select<br>
                user0_.id as id0_,<br>
                user0_.username as username0_,<br>
                user0_.password as password0_<br>
            from<br>
                users.user user0_<br>
            right outer join<br>
                users.profile profiles1_<br>
                    on user0_.id=profiles1_.user_id<br>
            order by<br>
                user0_.id<br>
        Hibernate:<br>
            select<br>
                profiles0_.user_id as user2_1_,<br>
                profiles0_.id as id1_,<br>
                profiles0_.id as id1_0_,<br>
                profiles0_.user_id as user2_1_0_,<br>
                profiles0_.email as email1_0_,<br>
                profiles0_.phone as phone1_0_,<br>
                profiles0_.mobile as mobile1_0_,<br>
                profiles0_.address as address1_0_,<br>
                profiles0_.postcode as postcode1_0_<br>
            from<br>
                users.profile profiles0_<br>
            where<br>
                profiles0_.user_id=?<br>
        1hongten<br>
        Hibernate:<br>
            select<br>
                profiles0_.user_id as user2_1_,<br>
                profiles0_.id as id1_,<br>
                profiles0_.id as id1_0_,<br>
                profiles0_.user_id as user2_1_0_,<br>
                profiles0_.email as email1_0_,<br>
                profiles0_.phone as phone1_0_,<br>
                profiles0_.mobile as mobile1_0_,<br>
                profiles0_.address as address1_0_,<br>
                profiles0_.postcode as postcode1_0_<br>
            from<br>
                users.profile profiles0_<br>
            where<br>
                profiles0_.user_id=?<br>
        2hanyuan<br>
        Hibernate:<br>
            select<br>
                profiles0_.user_id as user2_1_,<br>
                profiles0_.id as id1_,<br>
                profiles0_.id as id1_0_,<br>
                profiles0_.user_id as user2_1_0_,<br>
                profiles0_.email as email1_0_,<br>
                profiles0_.phone as phone1_0_,<br>
                profiles0_.mobile as mobile1_0_,<br>
                profiles0_.address as address1_0_,<br>
                profiles0_.postcode as postcode1_0_<br>
            from<br>
                users.profile profiles0_<br>
            where<br>
                profiles0_.user_id=?<br>
        3Hongwei1231</p>
頁: [1]
查看完整版本: HQL查询语言的使用介绍