当前位置:网站首页>Hibernate learning 3 - custom SQL
Hibernate learning 3 - custom SQL
2022-06-24 23:52:00 【Uh huh**】
List of articles
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();
}

Pagination
The way 1 == SQL Pagination
Map the file's where Configuration removed
@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();
}

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

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

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

边栏推荐
- MySQL problem points
- Design and practice of vivo server monitoring architecture
- Morris traverse
- 信号完整性(SI)电源完整性(PI)学习笔记(二十五)差分对与差分阻抗(五)
- Quickly build KVM virtual machine on # yyds dry goods inventory # physical machine
- Phprunner 10.7.0 PHP code generator
- 都2022年了,你还不了解什么是性能测试?
- Using ADC to control brushless motor source program STM32 library function
- Morris遍历
- 7-8 circular scheduling problem
猜你喜欢
随机推荐
Development status and prospect trend forecast report of humic acid sodium industry in the world and China from 2022 to 2028
基于三维GIS开发的水电工程建设方案
JS listens for page or element scroll events, scrolling to the bottom or top
How to use stm32subeide SWV function
throttle-debounce. JS: a small anti shake throttling function library
Yyds dry goods counting uses xshell to implement agent function
throttle-debounce.js:一个小型的防抖节流函数库
怎么把wps表格里某一列有重复项的整行删掉
Tiktok actual combat ~ sorting out the short video release process
libnum库简单使用(进制字符串转换)
canvas线条的动态效果
ArcGIS加载免费在线历史影像作为底图(不需要插件)
Hello C (VI) -- pointer and string
7-8 ladder cloud vertical
MySQL semi sync replication
Ethernet ARP Protocol
Morris遍历
Record a Webflux application memory leak troubleshooting
Global and Chinese 3-Chlorobenzaldehyde industry operation mode and future development trend report 2022 ~ 2028
Sword finger offer merges two sorted lists








