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

边栏推荐
- Leetcode No.10 regular expression matching
- Hello C (I) -- basics of C language
- 年薪百万,7年测试经验:守在一个还算不错的赛道,慢慢积累,等风来
- Tutorial details | how to edit and set the navigation function in the coolman system?
- 无鸟用的SAP PA证书,刚入行的同行可以考一考
- Analysis report on the development trend and Prospect of cetamide industry in the world and China from 2022 to 2028
- 教程详解|在酷雷曼系统中如何编辑设置导览功能?
- Global and Chinese 3-Chlorobenzaldehyde industry operation mode and future development trend report 2022 ~ 2028
- 企业级~uni-app网络请求封装
- Scala IO reads data from URLs and other data sources
猜你喜欢

Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference

Yyds dry goods counting uses xshell to implement agent function

年薪百万,7年测试经验:守在一个还算不错的赛道,慢慢积累,等风来

Spark's wide dependence and narrow dependence yyds dry goods inventory

svg线条动画背景js特效

Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it

抖音实战~项目关联UniCloud

ArcGIS加载免费在线历史影像作为底图(不需要插件)

canvas线条的动态效果

Tiktok actual combat ~ sorting out the short video release process
随机推荐
Yyds dry goods counting uses xshell to implement agent function
Uninstall hero League
js监听页面或元素scroll事件,滚动到底部或顶部
中学校园IP网络广播系统解决方案-校园数字IP广播系统方案设计指南
ArcGIS loads free online historical images as the base map (no plug-ins are required)
JPA学习1 - 概述、JPA、JPA核心注解、JPA核心对象
2021-2022 China's financial digitalization "new" insight Industry Research Report
ArcGIS加载免费在线历史影像作为底图(不需要插件)
Hyperledger Fabric 2. X dynamic update smart contract
Is there really something wrong with my behavior?
Using external Libpcap library on ARM platform
Enterprise data leakage prevention solution sharing
Hello C (VI) -- pointer and string
Classic interview questions and answers for embedded engineers
QT display RGB data
STM32CubeIDE SWV功能使用方法
svg+js键盘控制路径
7-9 treasure hunt route
Assessment of public finance of Jilin University in March of the 22nd spring -00091
Enterprise level ~uni app network request encapsulation