当前位置:网站首页>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 topic [array] -39- combined sum
- 明天就是PMP考试了(6月25日),这些大家都了解了吗?
- Hello C (VII) - structure
- 节奏快?压力大?VR全景客栈带你体验安逸生活
- Scala IO serialization and deserialization
- JS listens for page or element scroll events, scrolling to the bottom or top
- First person singular reading notes
- 教程详解|在酷雷曼系统中如何编辑设置导览功能?
- openGauss内核:简单查询的执行
猜你喜欢

Hello C (III) - pointer

Go language pointer, value reference and pointer reference

VR全景怎么赚钱?结合市场从两个方面客观分析下

Helix distance of point

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

机器学习自学成才的十条戒律

∞ symbol line animation canvasjs special effect

MySQL problem points

Quickly build KVM virtual machine on # yyds dry goods inventory # physical machine

svg+js键盘控制路径
随机推荐
Global and Chinese 3-Chlorobenzaldehyde industry operation mode and future development trend report 2022 ~ 2028
Spark's wide dependence and narrow dependence yyds dry goods inventory
今天睡眠质量记录79分
Daily calculation (vowel case conversion)
创意SVG环形时钟js特效
What are the advantages of VR panoramic production? Why is it favored?
∞ symbol line animation canvasjs special effect
Investment analysis and prospect forecast report of global and Chinese propargyl chloride industry from 2022 to 2028
Start QT program
Design and practice of vivo server monitoring architecture
Annual salary of millions, 7 years of testing experience: stay at a fairly good track, accumulate slowly, wait for the wind to come
7-9 treasure hunt route
Hibernate学习3 - 自定义SQL
How to resolve the 35 year old crisis? Sharing of 20 years' technical experience of chief architect of Huawei cloud database
教程详解|在酷雷曼系统中如何编辑设置导览功能?
Morris traversal
SAP PA certificate for no birds, which can be tested by new peers
Tremblement de terre réel ~ projet associé unicloud
一文理解OpenStack网络
中学校园IP网络广播系统解决方案-校园数字IP广播系统方案设计指南