当前位置:网站首页>Hibernate learning 3 - custom SQL

Hibernate learning 3 - custom SQL

2022-06-24 23:52:00 Uh huh**

HQL - Hibernate Query Language

Hibernate Built in SQL Query statement = Java Code level – It can only be done SQL Delete, modify and check , Can't add

Inquire about

Ordinary

from Keyword cannot be followed by table name , Must be the corresponding entity class name or class fully qualified name , If there is a class with the same name in the project , It is recommended to write the fully qualified name of the class directly , prevent hibernate Identification error

Add global where To configure , Only HQL Statement
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- package: similar mybatis Of Bean Alias type-aliases-package This attribute , Used for some attributes without writing fully qualified names -->
<!-- default-lazy: Lazy loading is enabled by default , One to many , Many to many , Use on many to one relationships -->
<!-- auto-import: Default true, Whether the partially qualified name can be used in the query statement , If there are two projects with the same name Bean, It is best to set to... In both mapping files false-->
<hibernate-mapping package="top.linruchang.entity" default-lazy="true" auto-import="false" >

    <!-- dynamic-insert dynamic-update: The default is false.true Similar to  mybatisplus Code insert for Java sentence , As long as the attribute is empty update、insert Of SQL Statement, the column will not appear  -->
    
    
    <!--  This defines global customization SQL It will be carried when where The condition of attribute ,session.createQuery These take effect , image session.get Will not enter into force -->
    <class name="Article" table="article" dynamic-insert="true" dynamic-update="true" where="title = 'fdsfds'">
        <id name="id" type="java.lang.String">
            <column name="id" ></column>
            <!-- Insertion time , If you don't have settings ID, Will help you automatically add ID-->
            <generator class="uuid"></generator>
        </id>


        <!--<property name="userId" type="java.lang.String">-->
        <!-- <column name="user_id"></column>-->
        <!--</property>-->


        <property name="title" type="java.lang.String">
            <column name="title"></column>
        </property>

        <property name="content" type="java.lang.String">
            <column name="content"></column>
        </property>


        <property name="likeNum" type="java.lang.Integer">
            <column name="like_num"></column>
        </property>

        <many-to-one lazy="no-proxy" name="sysUser" column="user_id" class="SysUser" />

    </class>
</hibernate-mapping>

    @Test
    public void test7() {
    

        Configuration configure = new Configuration().configure();
        SessionFactory sessionFactory = configure.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        Query<Article> query = session.createQuery("from top.linruchang.entity.Article", Article.class);
        List resultList = query.list();
        resultList.stream().forEach(System.out::println);

        transaction.commit();
        session.close();
    }

 Insert picture description here

Pagination

The way 1 == SQL Pagination

Map the file's where Configuration removed

Query Set up
setFirstResult: The starting coordinates
setMaxResults: Number of intercepted data
    @Test
    public void test10() {
    

        Configuration configure = new Configuration().configure();
        SessionFactory sessionFactory = configure.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        Query<SysUser> query = session.createQuery("from SysUser", SysUser.class);
        query.setFirstResult(2);
        query.setMaxResults(3);

        query.list().stream()
                .forEach(System.out::println);

        transaction.commit();
        session.close();

    }

 Insert picture description here

where

    @Test
    public void test10() {
    

        Configuration configure = new Configuration().configure();
        SessionFactory sessionFactory = configure.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        
        // Particular attention  where Later properties , You must be in this SysUserBean There are configurations in the mapping file of , Otherwise it will go wrong 
        Query<SysUser> query = session.createQuery("from SysUser where password = 'user'", SysUser.class);
        query.setFirstResult(2);
        query.setMaxResults(3);

        query.list().stream()
                .forEach(System.out::println);

        transaction.commit();
        session.close();

    }

 Insert picture description here

Place holder

    @Test
    public void test7() throws InterruptedException {
    

        Configuration configure = new Configuration().configure();
        // obtain sessionFactory
        SessionFactory sessionFactory = configure.buildSessionFactory();
        // Get database connection session
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        Query<Article> query = session.createQuery("from Article where title = :title", Article.class);
        query.setParameter("title", " Technical interview ");

        query.list().stream()
                .forEach(System.out::println);

        transaction.commit();
        session.close();

    }

 Insert picture description here

Cascade query


    @Test
    public void test8() throws InterruptedException {
    

        Configuration configure = new Configuration().configure();
        // obtain sessionFactory
        SessionFactory sessionFactory = configure.buildSessionFactory();
        // Get database connection session
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();


        SysUser sysUser = session.get(SysUser.class, "6d72c93aa292cf2ca2e789919a5e7bdc");
        System.out.println(sysUser);
        
        // Be careful  sysUser Must have configuration in the mapping file , Otherwise, it directly reports that the mapping parameter exception cannot be found 
        Query<Article> query = session.createQuery("from Article where sysUser = :sysUser1", Article.class);
        query.setParameter("sysUser1", sysUser);
        query.list().stream()
                .forEach(System.out::println);



        transaction.commit();
        session.close();

    }

 Insert picture description here

原网站

版权声明
本文为[Uh huh**]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241844461298.html