当前位置:网站首页>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();
}

边栏推荐
- 信号完整性(SI)电源完整性(PI)学习笔记(二十五)差分对与差分阻抗(五)
- What you must know about time series database!
- Modify stm32f030 clock source to internal crystal oscillator (HEI)
- Daily calculation (vowel case conversion)
- 信号完整性(SI)电源完整性(PI)学习笔记(一)信号完整性分析概论
- HMS core discovery Episode 13 live broadcast Preview - building the real world in mobile games
- throttle-debounce. JS: a small anti shake throttling function library
- 怎么把wps表格里某一列有重复项的整行删掉
- 7-9 treasure hunt route
- Scala IO writes data to a text file
猜你喜欢

Sword finger offer merges two sorted lists

Hibernate学习2 - 懒加载(延迟加载)、动态SQL参数、缓存

Hello C (III) - pointer

js监听页面或元素scroll事件,滚动到底部或顶部

Laravel framework knowledge

都2022年了,你还不了解什么是性能测试?

教程详解|在酷雷曼系统中如何编辑设置导览功能?

Annual salary of millions, 7 years of testing experience: stay at a fairly good track, accumulate slowly, wait for the wind to come

抖音实战~实现App端视频上传与发布

无鸟用的SAP PA证书,刚入行的同行可以考一考
随机推荐
Analysis report on operation mode and future development of global and Chinese methyl cyclopentanoate industry from 2022 to 2028
Hello C (VII) - structure
It's 2022, and you still don't know what performance testing is?
One way 和two way ANOVA分析的区别是啥,以及如何使用SPSS或者prism进行统计分析
Simple use of libnum Library (hexadecimal string conversion)
China CAE industry investment strategic planning and future development analysis report 2022 ~ 2028
抖音实战~项目关联UniCloud
Actipro WPF Controls 2022.1.2
DO280OpenShift访问控制--加密和ConfigMap
Andersen Global借助巴勒斯坦成员公司加强中东平台
QT cannot be edited with UTF-8
Tomorrow is the PMP Exam (June 25). Have you understood all this?
ArcGIS loads free online historical images as the base map (no plug-ins are required)
Ethernet ARP Protocol
2021-2022 China's financial digitalization "new" insight Industry Research Report
Tape SVG animation JS effect
都2022年了,你还不了解什么是性能测试?
使用网络摄像头进行眼睛注视估计
Ultra vires vulnerability & Logic vulnerability (hot) (VIII)
Why do more and more physical stores use VR panorama? What are the advantages?